/* Simple offline-capable shell for nomadro PWA */ const CACHE = "nomadro-shell-v1"; const PRECACHE = ["/", "/plan", "/compare", "/tools", "/manifest.webmanifest", "/icons/icon.svg"]; 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 req = event.request; if (req.method !== "GET") return; const url = new URL(req.url); if (url.origin !== self.location.origin) return; // API: network only if (url.pathname.startsWith("/api/")) return; // Navigations: network first, fall back to cache / home if (req.mode === "navigate") { event.respondWith( fetch(req) .then((res) => { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(req, copy)); return res; }) .catch(() => caches.match(req).then((r) => r || caches.match("/"))) ); return; } // Static assets: cache first event.respondWith( caches.match(req).then( (cached) => cached || fetch(req).then((res) => { if (res.ok && (url.pathname.startsWith("/_next/") || url.pathname.startsWith("/icons/"))) { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(req, copy)); } return res; }) ) ); });