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)); });