18 lines
1.0 KiB
JavaScript
18 lines
1.0 KiB
JavaScript
const CACHE = "cet6-shell-v2";
|
|
const APP_SHELL = ["/", "/manifest.webmanifest", "/favicon.svg", "/icon-192.png", "/icon-512.png"];
|
|
self.addEventListener("install", event => event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(APP_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;
|
|
if (request.method !== "GET" || new URL(request.url).origin !== location.origin) return;
|
|
if (request.url.includes("/api/")) return;
|
|
event.respondWith(fetch(request).then(response => {
|
|
if (response.ok && new URL(request.url).pathname.startsWith("/_next/")) {
|
|
const copy = response.clone(); caches.open(CACHE).then(cache => cache.put(request, copy));
|
|
}
|
|
return response;
|
|
}).catch(() => caches.match(request).then(found => found || caches.match("/"))));
|
|
});
|