spb/note-spboucher-ai
Public
1/* Note SPBoucher AI service worker: app-shell + static asset cache, network-first for pages/API, offline fallback. */2const VERSION = "note-sw-v1";3const STATIC = ["/offline", "/manifest.webmanifest", "/icons/icon-192.png", "/icons/icon-512.png"];4self.addEventListener("install", (e) => { e.waitUntil(caches.open(VERSION).then((c) => c.addAll(STATIC)).then(() => self.skipWaiting())); });5self.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())); });6self.addEventListener("fetch", (e) => {7 const req = e.request; const url = new URL(req.url);8 if (req.method !== "GET" || url.origin !== location.origin) return;9 if (url.pathname.startsWith("/api/")) return; // never cache private API responses10 if (url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/icons/")) {11 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; })));12 return;13 }14 if (req.mode === "navigate") {15 e.respondWith(fetch(req).catch(() => caches.match("/offline")));16 }17});18