diff --git a/lib/public/scripts.js b/lib/public/scripts.js index 5226248..3e604f6 100644 --- a/lib/public/scripts.js +++ b/lib/public/scripts.js @@ -223,9 +223,29 @@ setTimeout(() => { setServiceWorkerStatus('') }, 4500) + + registration.addEventListener('updatefound', () => { + console.info('Service worker will be updated...') + const newWorker = registration.installing + + newWorker.addEventListener('statechange', () => { + if (newWorker.state === 'installed') { + newWorker.postMessage({ action: 'skipWaiting' }) + } + }) + }) }).catch((err) => { setServiceWorkerStatus(`Service worker error: ${err.toString()}`) }) + + let refreshing + serviceWorker.addEventListener('controllerchange', () => { + if (refreshing) { + return + } + window.location.reload() + refreshing = true + }) } if (!noJS()) { diff --git a/lib/public/service-worker.js b/lib/public/service-worker.js index c452157..940854f 100644 --- a/lib/public/service-worker.js +++ b/lib/public/service-worker.js @@ -1,9 +1,13 @@ +// Worker v1 + self.addEventListener('install', (event) => { event.waitUntil( caches.open('fb-to-ical').then((cache) => { return cache.addAll([ '/', '/favicon.ico', + '/scripts.js?1', + '/style.css?1', ]) }) ) @@ -17,3 +21,9 @@ self.addEventListener('fetch', (event) => { }) ) }) + +self.addEventListener('message', (event) => { + if (event.data.action === 'skipWaiting') { + self.skipWaiting() + } +}) diff --git a/lib/views/index.ejs b/lib/views/index.ejs index 47e2fe7..b6ccfb7 100644 --- a/lib/views/index.ejs +++ b/lib/views/index.ejs @@ -5,7 +5,7 @@ - + Facebook Event to iCal Converter @@ -71,6 +71,6 @@ Created by Ondrej Synacek - +