Skip to content

Commit

Permalink
try to add autoupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-42 committed Sep 21, 2023
1 parent 3a7576f commit b252ac6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
23 changes: 22 additions & 1 deletion service-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// service-worker.js

const CACHE_VERSION = 'v7'; //todo UPDATE?
const CACHE_VERSION = '8'; //todo UPDATE?
const CACHE_NAME = `matrix-game-cache-${CACHE_VERSION}`;
const urlsToCache = [
'/',
Expand Down Expand Up @@ -58,3 +58,24 @@ self.addEventListener('fetch', event => {
})
);
});

// try to fetch version.js from server, update cache if it's newer and do not crash if it's not available
self.addEventListener('message', event => {
if (event.data === 'updateCache') {
fetch('version.js')
.then(response => response.json())
.then(data => {
if (data.version > CACHE_VERSION) {
caches.open(CACHE_NAME)
.then(cache => cache.addAll(urlsToCache))
.then(() => self.skipWaiting());
}
})
.catch(() => {
console.log('version.js not available');
});
}
});



1 change: 1 addition & 0 deletions version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const version = 8; //remember to change also in service-worker.js

0 comments on commit b252ac6

Please sign in to comment.