1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-07 02:28:54 +08:00

fix service worker registration #12

This commit is contained in:
lana-k
2021-04-30 14:14:41 +02:00
parent 5ded99e89f
commit 7f4b167dc2
3 changed files with 10 additions and 5 deletions

View File

@@ -1,8 +1,11 @@
let refresh = false
function invokeServiceWorkerUpdateFlow (registration) {
const agree = confirm('New version of the app is available. Refresh now?')
if (agree) {
if (registration.waiting) {
// let waiting Service Worker know it should became active
refresh = true
registration.waiting.postMessage({ type: 'SKIP_WAITING' })
}
}
@@ -23,19 +26,18 @@ if ('serviceWorker' in navigator) {
if (newRegestration) {
// wait until the new Service worker is actually installed (ready to take over)
newRegestration.addEventListener('statechange', () => {
if (registration.waiting) {
if (registration.waiting && navigator.serviceWorker.controller) {
invokeServiceWorkerUpdateFlow(registration)
}
})
}
})
let refreshing = false
// detect controller change and refresh the page
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (!refreshing) {
if (refresh) {
window.location.reload()
refreshing = true
refresh = false
}
})
})