2022-12-17 23:29:16 +00:00
|
|
|
import { useRegisterSW } from 'virtual:pwa-register/vue'
|
|
|
|
|
|
|
|
export const usePWA = () => {
|
|
|
|
const online = useOnline()
|
|
|
|
|
2022-12-19 22:37:23 +00:00
|
|
|
useHead({
|
|
|
|
meta: [{ id: 'theme-color', name: 'theme-color', content: computed(() => isDark.value ? '#111111' : '#ffffff') }],
|
|
|
|
})
|
|
|
|
|
2022-12-17 23:29:16 +00:00
|
|
|
const {
|
|
|
|
needRefresh,
|
|
|
|
updateServiceWorker,
|
|
|
|
} = 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 {
|
|
|
|
needRefresh,
|
|
|
|
updateServiceWorker,
|
|
|
|
close,
|
|
|
|
}
|
|
|
|
}
|