11 lines
820 B
JavaScript
11 lines
820 B
JavaScript
const CACHE = "cet6-shell-v1";
|
|
const APP_SHELL = ["/", "/tasks", "/stats", "/review", "/manifest.webmanifest", "/favicon.svg"];
|
|
self.addEventListener("install", event => event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(APP_SHELL)).then(() => self.skipWaiting())));
|
|
self.addEventListener("activate", event => event.waitUntil(self.clients.claim()));
|
|
self.addEventListener("fetch", event => {
|
|
const request = event.request;
|
|
if (request.method !== "GET" || new URL(request.url).origin !== location.origin) return;
|
|
if (request.url.includes("/api/")) return;
|
|
event.respondWith(fetch(request).then(response => { const copy = response.clone(); caches.open(CACHE).then(cache => cache.put(request, copy)); return response; }).catch(() => caches.match(request).then(found => found || caches.match("/"))));
|
|
});
|