Skip to content

Commit

Permalink
Service worker fixes
Browse files Browse the repository at this point in the history
- fix cache when multiple apps use same domain
- fix index.html version check
  • Loading branch information
nenadalm committed Jan 6, 2024
1 parent 7ab606b commit 2b3f3ba
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions resources/private/worker.js
Original file line number Diff line number Diff line change
@@ -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(/<meta name="app-version" content="(.*?)">/)[1])
.then(version => {
Expand All @@ -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))))
);
Expand Down

0 comments on commit 2b3f3ba

Please sign in to comment.