diff --git a/app.py b/app.py index a3756c9..4ae4d8b 100755 --- a/app.py +++ b/app.py @@ -238,6 +238,7 @@ def custom_logo(filename): response.headers['Cache-Control'] = 'public, max-age=86400' return response + @app.route('/sw.js') def serve_sw(): return send_from_directory(os.path.join(app.root_path, 'static'), 'sw.js', mimetype='application/javascript') diff --git a/static/sw.js b/static/sw.js index baf4cee..6ed70ce 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,30 +1,56 @@ -const cacheName = 'gottesdienste-v1.11'; +const VERSION = '1.18'; +const CACHE_NAME = `gottesdienste-v${VERSION}`; const assets = [ - '/', - '/static/app.css', - '/static/app.js', - '/static/gallery.css', - '/static/gallery.js', - '/static/audioplayer.css', - '/static/audioplayer.js', + '/', + `/static/app.css?v=${VERSION}`, + `/static/app.js?v=${VERSION}`, + `/static/gallery.css?v=${VERSION}`, + `/static/gallery.js?v=${VERSION}`, + `/static/audioplayer.css?v=${VERSION}`, + `/static/audioplayer.js?v=${VERSION}`, '/icon/logo-192x192.png', + '/icon/logo-300x300.png', '/icon/logo-512x512.png', '/custom_logo/logoB.png', '/custom_logo/logoW.png' ]; -self.addEventListener('install', e => { - e.waitUntil( - caches.open(cacheName).then(cache => { - return cache.addAll(assets); +self.addEventListener('install', evt => { + self.skipWaiting(); + evt.waitUntil( + caches.open(CACHE_NAME) + .then(cache => cache.addAll(assets)) + ); +}); + +self.addEventListener('activate', evt => { + self.clients.claim(); + evt.waitUntil( + caches.keys().then(keys => + Promise.all( + keys + .filter(k => k !== CACHE_NAME) + .map(k => caches.delete(k)) + ) + ) + .then(() => { + // Reload to use new files + return self.clients.matchAll({ type: 'window' }) + .then(clients => + clients.forEach(client => client.navigate(client.url)) + ); }) ); }); -self.addEventListener('fetch', e => { - e.respondWith( - caches.match(e.request).then(response => { - return response || fetch(e.request); - }) +self.addEventListener('fetch', evt => { + if (evt.request.mode === 'navigate') { + evt.respondWith( + fetch(evt.request).catch(() => caches.match('/app.html')) + ); + return; + } + evt.respondWith( + caches.match(evt.request).then(cached => cached || fetch(evt.request)) ); -}); \ No newline at end of file +}); diff --git a/templates/app.html b/templates/app.html index 5a582a6..a98e78d 100644 --- a/templates/app.html +++ b/templates/app.html @@ -8,7 +8,7 @@ - + @@ -23,7 +23,7 @@ - + @@ -253,8 +253,6 @@ if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('{{ url_for("static", filename="sw.js") }}') - .then(reg => console.log('Service worker registered.', reg)) - .catch(err => console.error('Service worker not registered.', err)); }); }