SPB Git
1.3 KB · 19 lines javascript
1/* Chat SPBoucher AI service worker — static assets only. API responses and pages carrying private data are NEVER cached. */2const VERSION = "csa-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("message", e => { if (e.data === "logout") caches.keys().then(ks => ks.forEach(k => caches.delete(k))); });7self.addEventListener("fetch", e => {8  const url = new URL(e.request.url);9  if (e.request.method !== "GET" || url.origin !== location.origin) return;10  if (url.pathname.startsWith("/api/")) return; // never cache API11  if (url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/icons/")) {12    e.respondWith(caches.match(e.request).then(hit => hit || fetch(e.request).then(res => { const copy = res.clone(); caches.open(VERSION).then(c => c.put(e.request, copy)); return res; })));13    return;14  }15  if (e.request.mode === "navigate") {16    e.respondWith(fetch(e.request).catch(() => caches.match("/offline")));17  }18});19
Files · main
Menu

Theme