2022-12-18 08:29:16 +09:00
|
|
|
import { useRegisterSW } from 'virtual:pwa-register/vue'
|
|
|
|
|
2022-12-28 23:08:03 +09:00
|
|
|
export default defineNuxtPlugin(() => {
|
2022-12-18 08:29:16 +09:00
|
|
|
const online = useOnline()
|
|
|
|
|
|
|
|
const {
|
2022-12-28 10:06:54 +09:00
|
|
|
needRefresh, updateServiceWorker,
|
2022-12-18 08:29:16 +09:00
|
|
|
} = useRegisterSW({
|
|
|
|
immediate: true,
|
|
|
|
onRegisteredSW(swUrl, r) {
|
|
|
|
if (!r || r.installing)
|
|
|
|
return
|
|
|
|
|
|
|
|
setInterval(async () => {
|
|
|
|
if (!online.value)
|
|
|
|
return
|
|
|
|
|
|
|
|
const resp = await fetch(swUrl, {
|
|
|
|
cache: 'no-store',
|
|
|
|
headers: {
|
|
|
|
'cache': 'no-store',
|
|
|
|
'cache-control': 'no-cache',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (resp?.status === 200)
|
|
|
|
await r.update()
|
|
|
|
}, 60 * 60 * 1000 /* 1 hour */)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const close = async () => {
|
|
|
|
needRefresh.value = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-12-28 23:08:03 +09:00
|
|
|
provide: {
|
|
|
|
pwa: reactive({
|
|
|
|
needRefresh,
|
|
|
|
updateServiceWorker,
|
|
|
|
close,
|
|
|
|
}),
|
|
|
|
},
|
2022-12-18 08:29:16 +09:00
|
|
|
}
|
2022-12-28 23:08:03 +09:00
|
|
|
})
|