diff --git a/js/app.js b/js/app.js index 6005a0b..ada4179 100644 --- a/js/app.js +++ b/js/app.js @@ -786,8 +786,36 @@ Avvio ===================================================================== */ + /** + * Guscio vecchio con script nuovo (un service worker di una versione precedente ha servito l'HTML dalla cache): + * si svuotano cache e worker e si ricarica, una volta sola. + */ + function gusciVecchio() { + if (document.getElementById('player-dock')) return false; + var chiave = 'rpi-riparazione'; + try { + if (sessionStorage.getItem(chiave)) return true; + sessionStorage.setItem(chiave, '1'); + } catch (e) { /* senza archivio si tenta comunque una volta */ } + var pulizia = []; + if ('serviceWorker' in navigator) { + pulizia.push(navigator.serviceWorker.getRegistrations().then(function (regs) { + return Promise.all(regs.map(function (r) { return r.unregister(); })); + })); + } + if ('caches' in window) { + pulizia.push(caches.keys().then(function (keys) { + return Promise.all(keys.map(function (k) { return caches.delete(k); })); + })); + } + Promise.all(pulizia).catch(function () { /* si ricarica comunque */ }).then(function () { + window.location.reload(); + }); + return true; + } + function init() { - if (!content) return; + if (!content || gusciVecchio()) return; Offline.init(); Player.init(); Podcast.init(); diff --git a/js/pwa-install.js b/js/pwa-install.js index 8acdbec..042e1c8 100644 --- a/js/pwa-install.js +++ b/js/pwa-install.js @@ -12,11 +12,26 @@ var BASE = window.BASE_PATH || ''; if ('serviceWorker' in navigator) { + // C'era già un worker a governare la pagina? Allora un cambio di worker è un aggiornamento dell'app + var avevaWorker = !!navigator.serviceWorker.controller; + var ricaricando = false; + window.addEventListener('load', function () { - navigator.serviceWorker.register(BASE + '/sw.js').catch(function (error) { + navigator.serviceWorker.register(BASE + '/sw.js', { updateViaCache: 'none' }).then(function (reg) { + // Controllo esplicito a ogni apertura: il browser da solo lo fa al massimo ogni 24 ore + reg.update().catch(function () { /* offline: si riprova alla prossima apertura */ }); + }).catch(function (error) { console.error('[PWA] Registrazione Service Worker fallita:', error); }); }); + + // Il worker nuovo ha preso il controllo (skipWaiting + clients.claim): si ricarica una volta, + // così guscio e script sono della stessa versione + navigator.serviceWorker.addEventListener('controllerchange', function () { + if (!avevaWorker || ricaricando) return; + ricaricando = true; + window.location.reload(); + }); } var isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone === true; diff --git a/sw.js b/sw.js index e006ea4..0022f9a 100644 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -const CACHE_NAME = 'rpigroupplay-v5'; +const CACHE_NAME = 'rpigroupplay-v6'; const ASSETS = [ './', './index.php', @@ -75,6 +75,27 @@ self.addEventListener('fetch', (e) => { const isStatic = STATIC_RE.test(new URL(url).pathname); const matchOptions = isStatic ? { ignoreSearch: true } : undefined; + // Pagine (guscio dell'app e contenuti PHP): prima la rete, la cache solo se la rete manca. + // Così dopo un aggiornamento nessuno resta con il guscio vecchio e gli script nuovi. + if (!isStatic) { + e.respondWith( + fetch(e.request).then((response) => { + if (response.status === 200) { + const responseClone = response.clone(); + caches.open(CACHE_NAME).then((cache) => cache.put(e.request, responseClone)); + } + return response; + }).catch(() => { + return caches.match(e.request).then((cached) => { + if (cached) return cached; + if (e.request.mode === 'navigate') return caches.match('./index.php'); + }); + }) + ); + return; + } + + // File statici (css, js, font, immagini): subito dalla cache, aggiornati in sottofondo e.respondWith( caches.match(e.request, matchOptions).then((cachedResponse) => { if (cachedResponse) {