34 lines
999 B
JavaScript
34 lines
999 B
JavaScript
const CACHE = "grokboy-web-v1";
|
|
const PRECACHE = ["/", "/styles.css", "/app.js", "/manifest.webmanifest", "/icon.svg"];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(PRECACHE)));
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) =>
|
|
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
|
|
)
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const url = new URL(event.request.url);
|
|
if (url.pathname.startsWith("/api/") || url.pathname.startsWith("/novnc")) {
|
|
return;
|
|
}
|
|
if (event.request.method !== "GET") return;
|
|
event.respondWith(
|
|
fetch(event.request)
|
|
.then((response) => {
|
|
const copy = response.clone();
|
|
caches.open(CACHE).then((cache) => cache.put(event.request, copy));
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(event.request))
|
|
);
|
|
});
|