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

add manifest and offline support #12

This commit is contained in:
lana-k
2021-04-29 16:19:25 +02:00
parent 92022f9083
commit 9b6aa3d6c7
10 changed files with 2954 additions and 1082 deletions

42
registerServiceWorker.js Normal file
View File

@@ -0,0 +1,42 @@
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
registration.waiting.postMessage({ type: 'SKIP_WAITING' })
}
}
}
if ('serviceWorker' in navigator) {
window.addEventListener('load', async () => {
const registration = await navigator.serviceWorker.register('/service-worker.js')
// ensure the case when the updatefound event was missed is also handled
// by re-invoking the prompt when there's a waiting Service Worker
if (registration.waiting) {
invokeServiceWorkerUpdateFlow(registration)
}
// detect Service Worker update available and wait for it to become installed
registration.addEventListener('updatefound', () => {
const newRegestration = registration.installing
if (newRegestration) {
// wait until the new Service worker is actually installed (ready to take over)
newRegestration.addEventListener('statechange', () => {
if (registration.waiting) {
invokeServiceWorkerUpdateFlow(registration)
}
})
}
})
let refreshing = false
// detect controller change and refresh the page
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (!refreshing) {
window.location.reload()
refreshing = true
}
})
})
}