/* Service worker minimale: l'app deve aprirsi anche in fondo all'orto senza campo. - guscio dell'app: cache-first, aggiornata in sottofondo - meteo: rete per prima, con l'ultima risposta buona come rete di sicurezza */ const CACHE = "pollice-verde-v1" const GUSCIO = ["/", "/index.html", "/manifest.webmanifest", "/icon.svg"] self.addEventListener("install", (e) => { e.waitUntil(caches.open(CACHE).then((c) => c.addAll(GUSCIO)).then(() => self.skipWaiting())) }) self.addEventListener("activate", (e) => { e.waitUntil( caches.keys().then((chiavi) => Promise.all(chiavi.filter((k) => k !== CACHE).map((k) => caches.delete(k)))) .then(() => self.clients.claim()) ) }) self.addEventListener("fetch", (e) => { const req = e.request if (req.method !== "GET") return const url = new URL(req.url) if (url.hostname.endsWith("open-meteo.com")) { e.respondWith( fetch(req) .then((r) => { const copia = r.clone() caches.open(CACHE).then((c) => c.put(req, copia)) return r }) .catch(() => caches.match(req)) ) return } if (url.origin !== location.origin) return e.respondWith( caches.match(req).then((colpo) => { const rete = fetch(req) .then((r) => { if (r.ok) { const copia = r.clone() caches.open(CACHE).then((c) => c.put(req, copia)) } return r }) .catch(() => colpo || caches.match("/index.html")) return colpo || rete }) ) })