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
@@ -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