add primitive cache busting, update service worker

This commit is contained in:
Ondrej Synacek 2019-10-20 22:29:00 +02:00
parent 4b7000e8f4
commit e4200690d9
3 changed files with 32 additions and 2 deletions

View File

@ -223,9 +223,29 @@
setTimeout(() => { setTimeout(() => {
setServiceWorkerStatus('') setServiceWorkerStatus('')
}, 4500) }, 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) => { }).catch((err) => {
setServiceWorkerStatus(`Service worker error: ${err.toString()}`) setServiceWorkerStatus(`Service worker error: ${err.toString()}`)
}) })
let refreshing
serviceWorker.addEventListener('controllerchange', () => {
if (refreshing) {
return
}
window.location.reload()
refreshing = true
})
} }
if (!noJS()) { if (!noJS()) {

View File

@ -1,9 +1,13 @@
// Worker v1
self.addEventListener('install', (event) => { self.addEventListener('install', (event) => {
event.waitUntil( event.waitUntil(
caches.open('fb-to-ical').then((cache) => { caches.open('fb-to-ical').then((cache) => {
return cache.addAll([ return cache.addAll([
'/', '/',
'/favicon.ico', '/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()
}
})

View File

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="/manifest.json"> <link rel="manifest" href="/manifest.json">
<link rel="apple-touch-icon" sizes="180x180" href="/icon-180.png"> <link rel="apple-touch-icon" sizes="180x180" href="/icon-180.png">
<link rel="stylesheet" type="text/css" href="/style.css"> <link rel="stylesheet" type="text/css" href="/style.css?1">
<title>Facebook Event to iCal Converter</title> <title>Facebook Event to iCal Converter</title>
</head> </head>
@ -71,6 +71,6 @@
Created by <a href="http://ondrejsynacek.com">Ondrej Synacek</a> Created by <a href="http://ondrejsynacek.com">Ondrej Synacek</a>
</footer> </footer>
<script src="/scripts.js"></script> <script src="/scripts.js?1"></script>
</body> </body>
</html> </html>