const CACHE_NAME = "homarr-site-v2"; const SHELL_PATHS = ["/", "/workshop/", "/docs/management/workshop/", "/img/logo.png"]; self.addEventListener("install", (event) => { event.waitUntil( caches.open(CACHE_NAME).then(async (cache) => { const manifest = await fetch("/workshop-assets.json") .then((response) => response.json()) .catch(() => []); await Promise.allSettled([...SHELL_PATHS, ...manifest].map((path) => cache.add(path))); }), ); self.skipWaiting(); }); self.addEventListener("activate", (event) => { event.waitUntil( caches .keys() .then((keys) => Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)))) .then(() => self.clients.claim()), ); }); self.addEventListener("fetch", (event) => { const request = event.request; const url = new URL(request.url); if (request.method !== "GET" || url.origin !== self.location.origin || url.pathname.startsWith("/api/")) return; event.respondWith( fetch(request) .then((response) => { if (response.ok) { const copy = response.clone(); event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.put(request, copy))); } return response; }) .catch(async () => { const cached = await caches.match(request); if (cached) return cached; if (request.mode === "navigate") return (await caches.match("/workshop/")) ?? Response.error(); return Response.error(); }), ); });