2022-12-17 23:29:16 +00:00
|
|
|
import { useRegisterSW } from 'virtual:pwa-register/vue'
|
2023-12-09 16:04:41 +00:00
|
|
|
import type { UnwrapNestedRefs } from 'vue'
|
|
|
|
import type { PwaInjection } from './types'
|
2023-01-26 20:30:39 +00:00
|
|
|
import { STORAGE_KEY_PWA_HIDE_INSTALL } from '~/constants'
|
2022-12-17 23:29:16 +00:00
|
|
|
|
2022-12-28 14:08:03 +00:00
|
|
|
export default defineNuxtPlugin(() => {
|
2022-12-17 23:29:16 +00:00
|
|
|
const online = useOnline()
|
2023-01-08 14:12:25 +00:00
|
|
|
const registrationError = ref(false)
|
|
|
|
const swActivated = ref(false)
|
2023-01-25 15:26:51 +00:00
|
|
|
const showInstallPrompt = ref(false)
|
2023-01-26 20:30:39 +00:00
|
|
|
const hideInstall = useLocalStorage(STORAGE_KEY_PWA_HIDE_INSTALL, false)
|
2023-01-08 14:12:25 +00:00
|
|
|
|
2023-01-14 20:58:52 +00:00
|
|
|
// https://thomashunter.name/posts/2021-12-11-detecting-if-pwa-twa-is-installed
|
|
|
|
const ua = navigator.userAgent
|
|
|
|
const ios = ua.match(/iPhone|iPad|iPod/)
|
2023-06-23 20:40:21 +01:00
|
|
|
const standalone = window.matchMedia('(display-mode: window-controls-overlay)').matches
|
|
|
|
|| window.matchMedia('(display-mode: standalone)').matches
|
2023-01-14 20:58:52 +00:00
|
|
|
const isInstalled = !!(standalone || (ios && !ua.match(/Safari/)))
|
|
|
|
|
2023-01-08 14:12:25 +00:00
|
|
|
const registerPeriodicSync = (swUrl: string, r: ServiceWorkerRegistration) => {
|
|
|
|
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 */)
|
|
|
|
}
|
2022-12-17 23:29:16 +00:00
|
|
|
|
|
|
|
const {
|
2024-03-05 14:48:58 +00:00
|
|
|
needRefresh,
|
|
|
|
updateServiceWorker,
|
2022-12-17 23:29:16 +00:00
|
|
|
} = useRegisterSW({
|
|
|
|
immediate: true,
|
2023-01-08 14:12:25 +00:00
|
|
|
onRegisterError() {
|
|
|
|
registrationError.value = true
|
|
|
|
},
|
2022-12-17 23:29:16 +00:00
|
|
|
onRegisteredSW(swUrl, r) {
|
2023-01-08 14:12:25 +00:00
|
|
|
// should add support in pwa plugin
|
|
|
|
if (r?.active?.state === 'activated') {
|
|
|
|
swActivated.value = true
|
|
|
|
registerPeriodicSync(swUrl, r)
|
|
|
|
}
|
|
|
|
else if (r?.installing) {
|
|
|
|
r.installing.addEventListener('statechange', (e) => {
|
|
|
|
const sw = e.target as ServiceWorker
|
|
|
|
swActivated.value = sw.state === 'activated'
|
|
|
|
if (swActivated.value)
|
|
|
|
registerPeriodicSync(swUrl, r)
|
2022-12-17 23:29:16 +00:00
|
|
|
})
|
2023-01-08 14:12:25 +00:00
|
|
|
}
|
2022-12-17 23:29:16 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const close = async () => {
|
|
|
|
needRefresh.value = false
|
|
|
|
}
|
|
|
|
|
2023-01-26 20:30:39 +00:00
|
|
|
let install: () => Promise<void> = () => Promise.resolve()
|
|
|
|
let cancelInstall: () => void = noop
|
2023-01-25 15:26:51 +00:00
|
|
|
|
2023-01-26 20:30:39 +00:00
|
|
|
if (!hideInstall.value) {
|
|
|
|
type InstallPromptEvent = Event & {
|
|
|
|
prompt: () => void
|
|
|
|
userChoice: Promise<{ outcome: 'dismissed' | 'accepted' }>
|
|
|
|
}
|
2023-01-25 15:26:51 +00:00
|
|
|
|
2023-01-26 20:30:39 +00:00
|
|
|
let deferredPrompt: InstallPromptEvent | undefined
|
2023-01-25 15:26:51 +00:00
|
|
|
|
2023-01-26 20:30:39 +00:00
|
|
|
const beforeInstallPrompt = (e: Event) => {
|
|
|
|
e.preventDefault()
|
|
|
|
deferredPrompt = e as InstallPromptEvent
|
|
|
|
showInstallPrompt.value = true
|
|
|
|
}
|
|
|
|
window.addEventListener('beforeinstallprompt', beforeInstallPrompt)
|
|
|
|
window.addEventListener('appinstalled', () => {
|
|
|
|
deferredPrompt = undefined
|
|
|
|
showInstallPrompt.value = false
|
|
|
|
})
|
2023-01-25 15:26:51 +00:00
|
|
|
|
2023-01-26 20:30:39 +00:00
|
|
|
cancelInstall = () => {
|
|
|
|
deferredPrompt = undefined
|
2023-01-25 15:26:51 +00:00
|
|
|
showInstallPrompt.value = false
|
2023-01-26 20:30:39 +00:00
|
|
|
window.removeEventListener('beforeinstallprompt', beforeInstallPrompt)
|
|
|
|
hideInstall.value = true
|
2023-01-25 15:26:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 20:30:39 +00:00
|
|
|
install = async () => {
|
|
|
|
if (!showInstallPrompt.value || !deferredPrompt) {
|
|
|
|
showInstallPrompt.value = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
showInstallPrompt.value = false
|
|
|
|
await nextTick()
|
|
|
|
deferredPrompt.prompt()
|
|
|
|
await deferredPrompt.userChoice
|
|
|
|
}
|
2023-01-25 15:26:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 23:29:16 +00:00
|
|
|
return {
|
2022-12-28 14:08:03 +00:00
|
|
|
provide: {
|
|
|
|
pwa: reactive({
|
2023-01-14 20:58:52 +00:00
|
|
|
isInstalled,
|
2023-01-25 15:26:51 +00:00
|
|
|
showInstallPrompt,
|
|
|
|
cancelInstall,
|
|
|
|
install,
|
2023-01-08 14:12:25 +00:00
|
|
|
swActivated,
|
|
|
|
registrationError,
|
2022-12-28 14:08:03 +00:00
|
|
|
needRefresh,
|
|
|
|
updateServiceWorker,
|
|
|
|
close,
|
2023-12-09 16:04:41 +00:00
|
|
|
}) satisfies UnwrapNestedRefs<PwaInjection>,
|
2022-12-28 14:08:03 +00:00
|
|
|
},
|
2022-12-17 23:29:16 +00:00
|
|
|
}
|
2022-12-28 14:08:03 +00:00
|
|
|
})
|