From c2f8df40f879b8d1b7c3b63609c079ffaa61f6fa Mon Sep 17 00:00:00 2001 From: max-42 Date: Wed, 20 Sep 2023 23:08:17 +0200 Subject: [PATCH] add an autoupdate --- service-worker.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/service-worker.js b/service-worker.js index 0209e5c..e896eb5 100644 --- a/service-worker.js +++ b/service-worker.js @@ -1,6 +1,7 @@ // service-worker.js -const CACHE_NAME = 'matrix-game-cache-v4'; +const CACHE_VERSION = 'v5'; //todo UPDATE? +const CACHE_NAME = `matrix-game-cache-${CACHE_VERSION}`; const urlsToCache = [ '/', 'index.html', @@ -29,9 +30,28 @@ self.addEventListener('install', event => { ); }); +self.addEventListener('activate', event => { + event.waitUntil( + caches.keys().then(cacheNames => { + return Promise.all( + cacheNames.map(cache => { + if (cache !== CACHE_NAME) { + return caches.delete(cache); + } + }) + ); + }) + ); +}); + self.addEventListener('fetch', event => { event.respondWith( caches.match(event.request) - .then(response => response || fetch(event.request)) + .then(response => { + if (response) { + return response; + } + return fetch(event.request); + }) ); });