// 爱马仕 Hermes · 轻量 Service Worker // 静态壳走 network-first(拿不到再回退缓存),API 直通 const CACHE = "hermes-ui-v16"; const ASSETS = [ "./", "./index.html", "./styles.css", "./app.js", "./manifest.webmanifest", "./icon.svg", ]; self.addEventListener("install", (e) => { e.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS))); self.skipWaiting(); }); self.addEventListener("activate", (e) => { e.waitUntil( caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))) ) ); self.clients.claim(); }); self.addEventListener("fetch", (e) => { const url = new URL(e.request.url); // API / 鉴权 / skill 索引等动态资源全部直通 if (url.pathname.startsWith("/api/")) return; if (url.pathname.startsWith("/_auth/")) return; if (url.pathname.startsWith("/feishu/")) return; if (url.pathname.startsWith("/hermes-skills/")) return; if (e.request.method !== "GET") return; // 静态壳:network-first,离线再回退到缓存 e.respondWith( fetch(e.request) .then((res) => { if (res && res.ok) { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(e.request, copy)).catch(() => {}); } return res; }) .catch(() => caches.match(e.request).then((hit) => hit || Response.error())) ); });