Benzina Abruzzo: PWA completa (mappa PMTiles, lista a pannello, dettaglio impianto)
This commit is contained in:
commit
176e6acd6f
16 changed files with 3180 additions and 0 deletions
39
static/sw.js
Normal file
39
static/sw.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
const V = 'benzina-v17';
|
||||
const SHELL = [
|
||||
'/', '/static/app.css', '/static/app.js', '/static/manifest.webmanifest',
|
||||
'/static/icons/apple-touch-icon.png', '/static/icons/favicon-32.png',
|
||||
'/static/icons/icon-192.png', '/static/icons/icon-512.png'
|
||||
];
|
||||
|
||||
self.addEventListener('install', e => {
|
||||
e.waitUntil(caches.open(V).then(c => c.addAll(SHELL)).then(() => self.skipWaiting()));
|
||||
});
|
||||
|
||||
self.addEventListener('activate', e => {
|
||||
e.waitUntil(
|
||||
caches.keys().then(keys => Promise.all(keys.filter(k => k !== V).map(k => caches.delete(k))))
|
||||
.then(() => self.clients.claim())
|
||||
);
|
||||
});
|
||||
|
||||
// HTML, JS, CSS: prima la rete (cosi un deploy si vede subito), cache solo se offline.
|
||||
function networkFirst(req) {
|
||||
return fetch(req, { cache: 'no-store' }).then(r => {
|
||||
const copy = r.clone();
|
||||
caches.open(V).then(c => c.put(req, copy));
|
||||
return r;
|
||||
}).catch(() => caches.match(req).then(m => m || caches.match('/')));
|
||||
}
|
||||
|
||||
self.addEventListener('fetch', e => {
|
||||
const url = new URL(e.request.url);
|
||||
if (e.request.method !== 'GET') return;
|
||||
if (url.origin !== location.origin) return;
|
||||
|
||||
// Le API hanno la loro cache (server: 2h in memoria + ETag; client: snapshot
|
||||
// in localStorage con revalidation condizionale). Il SW non ci mette mano.
|
||||
if (url.pathname.startsWith('/api/')) return;
|
||||
if (url.pathname === '/static/sw.js') return;
|
||||
|
||||
e.respondWith(networkFirst(e.request));
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue