From 2b3f3baca58df679e9c2274b997c8eb2987b190b Mon Sep 17 00:00:00 2001 From: Miloslav Nenadal Date: Sat, 6 Jan 2024 14:23:37 +0100 Subject: [PATCH] Service worker fixes - fix cache when multiple apps use same domain - fix index.html version check --- resources/private/worker.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/resources/private/worker.js b/resources/private/worker.js index 5f07636..23df3d6 100644 --- a/resources/private/worker.js +++ b/resources/private/worker.js @@ -1,10 +1,11 @@ const relatedAppVersion = '1'; // prop:relatedAppVersion const urlsToCache = ["/", "index.html", "js/app.js", "css/styles.css", "img/icon.svg", "manifest.json"]; // prop:urlsToCache -const cacheKey = `resources.${relatedAppVersion}`; +const cacheKeyPrefix = 'nenadalm.backgammon.'; +const cacheKey = `${cacheKeyPrefix}resources.${relatedAppVersion}`; function ensureHtmlVersionMatches(cache) { - return cache.match(new Request('/index.html')) + return cache.match(new Request('index.html')) .then(response => response.text()) .then(html => html.match(//)[1]) .then(version => { @@ -23,6 +24,7 @@ self.addEventListener('install', event => { self.addEventListener('activate', event => { event.waitUntil( caches.keys() + .then(keys => keys.filter(key => key.startsWith(cacheKeyPrefix))) .then(keys => keys.filter(key => key !== cacheKey)) .then(oldKeys => Promise.all(oldKeys.map(key => caches.delete(key)))) );