/* Note SPBoucher AI service worker: app-shell + static asset cache, network-first for pages/API, offline fallback. */ const VERSION = "note-sw-v1"; const STATIC = ["/offline", "/manifest.webmanifest", "/icons/icon-192.png", "/icons/icon-512.png"]; self.addEventListener("install", (e) => { e.waitUntil(caches.open(VERSION).then((c) => c.addAll(STATIC)).then(() => self.skipWaiting())); }); self.addEventListener("activate", (e) => { e.waitUntil(caches.keys().then((ks) => Promise.all(ks.filter((k) => k !== VERSION).map((k) => caches.delete(k)))).then(() => self.clients.claim())); }); self.addEventListener("fetch", (e) => { const req = e.request; const url = new URL(req.url); if (req.method !== "GET" || url.origin !== location.origin) return; if (url.pathname.startsWith("/api/")) return; // never cache private API responses if (url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/icons/")) { e.respondWith(caches.match(req).then((hit) => hit || fetch(req).then((res) => { const copy = res.clone(); caches.open(VERSION).then((c) => c.put(req, copy)); return res; }))); return; } if (req.mode === "navigate") { e.respondWith(fetch(req).catch(() => caches.match("/offline"))); } });