const CACHE = "orgweb-v2"; const PRECACHE = [ "/", "/static/style.css", "/static/manifest.webmanifest", "/static/icon-192.png", "/static/icon-512.png", "/static/icon-512-maskable.png", ]; self.addEventListener("install", (event) => { event.waitUntil( caches .open(CACHE) .then((cache) => cache.addAll(PRECACHE)) .then(() => self.skipWaiting()) ); }); self.addEventListener("activate", (event) => { event.waitUntil( caches .keys() .then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))) ) .then(() => self.clients.claim()) ); }); self.addEventListener("fetch", (event) => { const url = new URL(event.request.url); if (url.origin !== location.origin) return; if (event.request.mode === "navigate") { event.respondWith( fetch(event.request).catch(() => caches.match("/")) ); return; } if (url.pathname === "/static/style.css") { event.respondWith( fetch(event.request) .then((resp) => { const copy = resp.clone(); caches.open(CACHE).then((cache) => cache.put(event.request, copy)); return resp; }) .catch(() => caches.match(event.request)) ); return; } event.respondWith( caches.match(event.request).then( (cached) => cached || fetch(event.request).then((resp) => { const copy = resp.clone(); caches.open(CACHE).then((cache) => cache.put(event.request, copy)); return resp; }) ) ); });