Files
orgweb/static/sw.js

54 lines
1.2 KiB
JavaScript

const CACHE = "orgweb-v1";
const PRECACHE = [
"/",
"/static/style.css",
"/static/manifest.webmanifest",
"/static/icon-192.png",
"/static/icon-512.png",
"/static/icon-512-maskable.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE)
.then((cache) => cache.addAll(PRECACHE))
.then(() => self.skipWaiting())
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
)
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
if (url.origin !== location.origin) return;
if (event.request.mode === "navigate") {
event.respondWith(
fetch(event.request).catch(() => caches.match("/"))
);
return;
}
event.respondWith(
caches.match(event.request).then(
(cached) =>
cached ||
fetch(event.request).then((resp) => {
const copy = resp.clone();
caches.open(CACHE).then((cache) => cache.put(event.request, copy));
return resp;
})
)
);
});