Files
hermes-glass-ui-personal/src/sw.js
2026-05-09 19:21:09 +08:00

49 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 爱马仕 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()))
);
});