From 9352f37e4f5ce311e12099d3667863561df01570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Fri, 5 May 2023 00:33:39 +0200 Subject: [PATCH] Fix #1173 Use BASE_URI to cache the sw files --- public/scripts/movim_base.js | 46 ++++++++--------- public/sw.js | 96 ++++++++++++++++++------------------ 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/public/scripts/movim_base.js b/public/scripts/movim_base.js index 192f2dc2f..9968f8e84 100644 --- a/public/scripts/movim_base.js +++ b/public/scripts/movim_base.js @@ -14,7 +14,7 @@ var isTouch = false; * @param function func */ function movimAddOnload(func) { - if (typeof(func) === "function") { + if (typeof (func) === "function") { onloaders.push(func); } } @@ -24,7 +24,7 @@ function movimAddOnload(func) { * @param function func */ function movimAddFocus(func) { - if (typeof(func) === "function") { + if (typeof (func) === "function") { onfocused.push(func); } } @@ -48,7 +48,7 @@ function movimLaunchOnload() { /** * The focus event doesn't seems to be triggered all the time ¯\_(ツ)_/¯ */ -var onFocusedFunction = function() { +var onFocusedFunction = function () { if (isFocused) return; isFocused = true; @@ -59,8 +59,8 @@ var onFocusedFunction = function() { window.addEventListener('mouseover', onFocusedFunction); window.addEventListener('focus', onFocusedFunction); -window.addEventListener('blur', function() { isFocused = false; }); -window.addEventListener('touchstart', function() { isTouch = true; }, { once: true }); +window.addEventListener('blur', function () { isFocused = false; }); +window.addEventListener('touchstart', function () { isTouch = true; }, { once: true }); /** * Register a service worker for the Progressive Web App @@ -68,33 +68,35 @@ window.addEventListener('touchstart', function() { isTouch = true; }, { once: tr if ('serviceWorker' in navigator) { navigator.serviceWorker .register('sw.js') - .then(() => { + .then(e => navigator.serviceWorker.ready) + .then((r) => { + r.active.postMessage({ base_uri: BASE_URI }); console.log('Service Worker Registered'); }); } -movimAddOnload(function() { +movimAddOnload(function () { /** * Handle the PWA install button */ - const pwaButton = document.querySelector('#pwa'); + const pwaButton = document.querySelector('#pwa'); - window.addEventListener('beforeinstallprompt', (e) => { - e.preventDefault(); - deferredPrompt = e; - pwaButton.style.display = 'initial'; + window.addEventListener('beforeinstallprompt', (e) => { + e.preventDefault(); + deferredPrompt = e; + pwaButton.style.display = 'initial'; - pwaButton.addEventListener('click', () => { - deferredPrompt.prompt(); + pwaButton.addEventListener('click', () => { + deferredPrompt.prompt(); - deferredPrompt.userChoice.then((choiceResult) => { - if (choiceResult.outcome === 'accepted') { - console.log('Movim App installed'); - } + deferredPrompt.userChoice.then((choiceResult) => { + if (choiceResult.outcome === 'accepted') { + console.log('Movim App installed'); + } - deferredPrompt = null; - }); - }); - }); + deferredPrompt = null; + }); + }); + }); }); \ No newline at end of file diff --git a/public/sw.js b/public/sw.js index ab4fedd6c..6ad4936ea 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,66 +1,64 @@ -self.addEventListener('install', (e) => { - e.waitUntil( - caches.open('movim').then((cache) => cache.addAll([ - // Audio - '/theme/audio/call.ogg', - '/theme/audio/message.ogg', +self.addEventListener('message', (e) => { + caches.open('movim').then((cache) => cache.addAll([ + // Audio + e.data.base_uri + '/theme/audio/call.ogg', + e.data.base_uri + '/theme/audio/message.ogg', - // Fonts - '/theme/fonts/MaterialIcons/font.css', - '/theme/fonts/MaterialIcons/MaterialIcons-Regular.woff2', - '/theme/fonts/Roboto/font.css', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fCRc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fABc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fCBc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fBxc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fCxc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fChc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fBBc4.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu72xKOzY.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu5mxKOzY.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu7mxKOzY.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu4WxKOzY.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu7WxKOzY.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu7GxKOzY.woff2', - '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu4mxK.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fCRc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fABc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fCBc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fBxc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fCxc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fChc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fBBc4.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfCRc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfABc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfCBc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfBxc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfCxc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfChc4EsA.woff2', - '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfBBc4.woff2', - ])), - ); + // Fonts + e.data.base_uri + '/theme/fonts/MaterialIcons/font.css', + e.data.base_uri + '/theme/fonts/MaterialIcons/MaterialIcons-Regular.woff2', + e.data.base_uri + '/theme/fonts/Roboto/font.css', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fCRc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fABc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fCBc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fBxc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fCxc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fChc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmSU5fBBc4.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu72xKOzY.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu5mxKOzY.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu7mxKOzY.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu4WxKOzY.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu7WxKOzY.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu7GxKOzY.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOmCnqEu92Fr1Mu4mxK.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fCRc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fABc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fCBc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fBxc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fCxc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fChc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmEU9fBBc4.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfCRc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfABc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfCBc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfBxc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfCxc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfChc4EsA.woff2', + e.data.base_uri + '/theme/fonts/Roboto/KFOlCnqEu92Fr1MmWUlfBBc4.woff2', + ])); }); -self.addEventListener('push', function(e) { +self.addEventListener('push', function (e) { var json = e.data.json(); var options = { - body: json.body, - icon: json.picture, - vibrate: [100, 50, 100], - data: { url: json.action }, - actions: [{action: "action", title: json.button}] + body: json.body, + icon: json.picture, + vibrate: [100, 50, 100], + data: { url: json.action }, + actions: [{ action: "action", title: json.button }] }; e.waitUntil( self.registration.showNotification(json.title, options) ); }); -self.addEventListener('notificationclick', function(e) { +self.addEventListener('notificationclick', function (e) { e.notification.close(); e.waitUntil(clients.matchAll({ type: 'window' - }).then(function(clientList) { + }).then(function (clientList) { for (var i = 0; i < clientList.length; i++) { var client = clientList[i]; @@ -74,7 +72,7 @@ self.addEventListener('notificationclick', function(e) { } })); } -, false); + , false); self.addEventListener('fetch', (e) => { e.respondWith(