diff --git a/serviceworker.js b/serviceworker.js index e0ee6ee4..bcca3fb2 100644 --- a/serviceworker.js +++ b/serviceworker.js @@ -1,22 +1,20 @@ -var staticCacheName = "pwa-v" + new Date().getTime(); +var staticCacheName = "pwa-v2"; var filesToCache = [ - 'public/img/pwa/icon-192x192.png', - 'public/img/pwa/icon-512x512.png' + '/public/img/pwa/icon-192x192.png', + '/public/img/pwa/icon-512x512.png' ]; -/* // Cache on install self.addEventListener("install", event => { - this.skipWaiting(); event.waitUntil( caches.open(staticCacheName) .then(cache => { return cache.addAll(filesToCache); }) + .then(() => self.skipWaiting()) ) }); -*/ -/* + // Clear cache on activate self.addEventListener('activate', event => { event.waitUntil( @@ -27,19 +25,29 @@ self.addEventListener('activate', event => { .filter(cacheName => (cacheName !== staticCacheName)) .map(cacheName => caches.delete(cacheName)) ); - }) + }).then(() => self.clients.claim()) ); -});*/ +}); -// Serve from Cache +// Navigation must prefer the network so cached redirects cannot mask auth state. self.addEventListener("fetch", event => { + if (event.request.method !== 'GET') { + return; + } + + if (event.request.mode === 'navigate') { + event.respondWith( + fetch(event.request) + .catch(() => caches.match(event.request)) + ); + + return; + } + event.respondWith( caches.match(event.request) .then(response => { return response || fetch(event.request); }) - .catch(() => { - return caches.match('offline'); - }) ) });