47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const CACHE = "steambase-shell-v1";
|
|
const SHELL = [
|
|
"/",
|
|
"/static/styles.css",
|
|
"/static/app.js",
|
|
"/static/icon-192.png",
|
|
"/static/site.webmanifest",
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches.keys()
|
|
.then((keys) => Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key))))
|
|
.then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const request = event.request;
|
|
const url = new URL(request.url);
|
|
if (request.method !== "GET" || url.origin !== self.location.origin) return;
|
|
if (url.pathname.startsWith("/api/")) return;
|
|
|
|
if (request.mode === "navigate") {
|
|
event.respondWith(fetch(request).catch(() => caches.match("/")));
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
fetch(request)
|
|
.then((response) => {
|
|
if (response.ok) {
|
|
const copy = response.clone();
|
|
caches.open(CACHE).then((cache) => cache.put(request, copy));
|
|
}
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(request))
|
|
);
|
|
});
|