cert-study-app / static /service-worker.js
Kentlo's picture
Sync from GitHub edfda2fe3caaf276f7ceb84b32224675a62772de
3921b3c verified
Raw
History Blame Contribute Delete
1.26 kB
const CACHE_NAME = "cert-study-static-v1";
const STATIC_ASSETS = [
"/app/static/manifest.webmanifest",
"/app/static/icons/icon.svg",
"/app/static/icons/icon-maskable.svg",
"/app/static/icons/icon-192.png",
"/app/static/icons/icon-512.png",
"/app/static/icons/icon-maskable-192.png",
"/app/static/icons/icon-maskable-512.png"
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)))
)
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET" || !request.url.includes("/app/static/")) {
return;
}
event.respondWith(
caches.match(request).then((cached) => {
if (cached) {
return cached;
}
return fetch(request).then((response) => {
const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
return response;
});
})
);
});