v0.6.3: immediate update on new version (skipping 24h service worker cooldown)

This commit is contained in:
kritzl 2024-06-10 13:55:39 +02:00
parent 12048f3a49
commit 2a0b6dbfe6
6 changed files with 35 additions and 16 deletions

21
sw.js
View file

@ -1,4 +1,4 @@
const version = '0.6.2'
const version = '0.6.3'
const consolePrefix = `[SW v${version}] `
const cacheName = `hvvstuhl-${version}`;
@ -36,13 +36,16 @@ const contentToCache = [
// Service worker Install: Cache all files
self.addEventListener("install", (e) => {
console.log("[Service Worker] Wird installiert....");
console.log(`${consolePrefix}Wird installiert....`);
e.waitUntil(
(async () => {
const cache = await caches.open(cacheName);
console.log("[Service Worker] Cache wird aufgebaut...");
console.log(`${consolePrefix} Cache wird aufgebaut...`);
await cache.addAll(contentToCache);
console.log("[Service Worker] FERTIG");
console.log(`${consolePrefix} > FERTIG`);
console.log(`${consolePrefix} Versuche Wartezeit zu überspringen...`);
await self.skipWaiting();
console.log(`${consolePrefix} > Erfolgreich`);
})(),
);
});
@ -60,8 +63,10 @@ const deleteOldCaches = async () => {
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
await deleteOldCaches()
await clients.claim();
await deleteOldCaches();
console.log(`${consolePrefix}Versuche Clients zu beanspruchen...`);
await self.clients.claim();
console.log(`${consolePrefix} > Erfolgreich`);
})(),
);
});
@ -79,12 +84,12 @@ self.addEventListener("fetch", (e) => {
const r = await caches.match(e.request);
if (r) {
console.log(`${consolePrefix} Im Cache gefunden.`);
console.log(`${consolePrefix} > Im Cache gefunden.`);
return r;
}
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
console.log(`${consolePrefix} Nicht gefunden. Aktualisiere Cache: ${path}`);
console.log(`${consolePrefix} > Nicht gefunden. Aktualisiere Cache: ${path}`);
await cache.put(e.request, response.clone());
return response;
}else{