const CACHE = "if-viewer-static-v7"; const ASSETS = [ "/static/style.css", "/static/favicon.svg", "/static/icon-192.png", "/static/icon-512.png", "/static/icon-monochrome.png", "/static/i18n.js", "/static/locales/en.json", "/static/locales/de.json", ]; const NETWORK_FIRST = new Set([ "/static/pwa.js", "/static/style.css", "/static/i18n.js", "/static/locales/en.json", "/static/locales/de.json", "/static/app.js", ]); self.addEventListener("install", (event) => { event.waitUntil( caches.open(CACHE) .then((cache) => cache.addAll(ASSETS)) .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) => { if (event.request.method !== "GET") return; const url = new URL(event.request.url); if (url.pathname.includes("/api/")) return; if (!url.pathname.startsWith("/static/")) return; if (NETWORK_FIRST.has(url.pathname)) { event.respondWith( fetch(event.request) .then((response) => { if (response.ok) { const copy = response.clone(); caches.open(CACHE).then((cache) => cache.put(event.request, copy)); } return response; }) .catch(() => caches.match(event.request)) ); return; } event.respondWith( caches.match(event.request).then((cached) => cached || fetch(event.request)) ); });