Aggiornamento del service worker senza restare con il guscio vecchio
Dopo il rilascio 3.0.0 i browser con il worker v4 servivano il guscio HTML vecchio dalla cache insieme agli script nuovi, e l'app si fermava (app.rpigroup.net). Ora: pagine e guscio prima dalla rete (cache solo offline), aggiornamento del worker forzato a ogni apertura con ricarica quando prende il controllo, e auto-riparazione se lo script trova un guscio di una versione precedente. Cache v6. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
bb3dee5b43
commit
4d5e325ed9
3 files changed
+67
-3
No files matched your search
@@ -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();
|
||||
|
||||
+16
-1
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user