50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
const cacheName = "xiteng-chat-shell-v4";
|
|
const shellAssets = [
|
|
"/",
|
|
"/manifest.webmanifest",
|
|
"/favicon.svg",
|
|
"/icons/icon-192.png",
|
|
"/icons/icon-512.png",
|
|
"/icons/icon-maskable-512.png",
|
|
"/icons/apple-touch-icon.png"
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(caches.open(cacheName).then(async (cache) => {
|
|
for (const asset of shellAssets) {
|
|
try {
|
|
const response = await fetch(asset, {cache: "reload"});
|
|
if (response.ok) await cache.put(asset, response);
|
|
} catch {}
|
|
}
|
|
}).then(() => self.skipWaiting()));
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(caches.keys()
|
|
.then((keys) => Promise.all(keys.filter((key) => key !== cacheName).map((key) => caches.delete(key))))
|
|
.then(() => self.clients.claim()));
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const request = event.request;
|
|
if (request.method !== "GET") return;
|
|
const url = new URL(request.url);
|
|
if (url.origin !== self.location.origin || url.pathname.startsWith("/api/") || url.pathname.startsWith("/outpost.goauthentik.io/")) return;
|
|
|
|
if (request.mode === "navigate") {
|
|
event.respondWith(fetch(request).then((response) => {
|
|
if (response.ok) caches.open(cacheName).then((cache) => cache.put("/", response.clone()));
|
|
return response;
|
|
}).catch(async () => (await caches.match("/")) || Response.error()));
|
|
return;
|
|
}
|
|
|
|
if (shellAssets.includes(url.pathname) || /\.(?:js|css|png|svg|ico|webmanifest)$/.test(url.pathname)) {
|
|
event.respondWith(caches.match(request).then((cached) => cached || fetch(request).then((response) => {
|
|
if (response.ok) caches.open(cacheName).then((cache) => cache.put(request, response.clone()));
|
|
return response;
|
|
})));
|
|
}
|
|
});
|