Files
homelab/chat/public/sw.js

59 lines
2.1 KiB
JavaScript

const cachePrefix = "xiteng-chat-";
const cacheName = `${cachePrefix}shell-v8`;
const basePath = "/chat";
const shellAssets = [
`${basePath}/`,
`${basePath}/manifest.webmanifest`,
"/favicon.svg",
"/icons/favicon-32.png",
"/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.startsWith(cachePrefix) && 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(`${basePath}/api/`) || url.pathname.startsWith("/outpost.goauthentik.io/")) return;
if (request.mode === "navigate") {
event.respondWith(fetch(request).then((response) => {
if (response.ok) {
const cacheable = response.clone();
void caches.open(cacheName).then((cache) => cache.put(`${basePath}/`, cacheable)).catch(() => {});
}
return response;
}).catch(async () => (await caches.match(`${basePath}/`)) || 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) {
const cacheable = response.clone();
void caches.open(cacheName).then((cache) => cache.put(request, cacheable)).catch(() => {});
}
return response;
})));
}
});