const CACHE_NAME = "nido-groundwork-pages-v1";
const SHELL = [
"./",
"./index.html",
"./styles.css",
"./app.js",
"./groundwork.json",
"./manifest.webmanifest",
"./icons/icon-192.png",
"./icons/icon-512.png",
"./icons/apple-touch-icon.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL)));
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((names) => Promise.all(names.filter((name) => name !== CACHE_NAME).map((name) => caches.delete(name)))),
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET") return;
if (request.mode === "navigate") {
event.respondWith(fetch(request).catch(() => caches.match("./index.html")));
return;
}
event.respondWith(
caches.match(request).then((cached) => {
const refreshed = fetch(request)
.then((response) => {
if (response.ok) {
const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
}
return response;
})
.catch(() => cached);
return cached || refreshed;
}),
);
});