Skip to content

Commit e92a983

Browse files
authored
fix(pwa): handle sw registration error and status (#858)
1 parent 5f07fd2 commit e92a983

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

composables/users.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ export function getUsersIndexByUserId(userId: string) {
180180
}
181181

182182
export async function removePushNotificationData(user: UserLogin, fromSWPushManager = true) {
183-
if (!user.pushSubscription)
184-
return
185-
186183
// clear push subscription
187184
user.pushSubscription = undefined
188185
const { acct } = user.account
@@ -192,9 +189,12 @@ export async function removePushNotificationData(user: UserLogin, fromSWPushMana
192189
delete useLocalStorage<PushNotificationPolicy>(STORAGE_KEY_NOTIFICATION_POLICY, {}).value[acct]
193190

194191
const pwaEnabled = useRuntimeConfig().public.pwaEnabled
192+
const pwa = useNuxtApp().$pwa
193+
const registrationError = pwa?.registrationError === true
194+
const unregister = pwaEnabled && !registrationError && pwa?.registrationError === true && fromSWPushManager
195195

196196
// we remove the sw push manager if required and there are no more accounts with subscriptions
197-
if (pwaEnabled && fromSWPushManager && (users.value.length === 0 || users.value.every(u => !u.pushSubscription))) {
197+
if (unregister && (users.value.length === 0 || users.value.every(u => !u.pushSubscription))) {
198198
// clear sw push subscription
199199
try {
200200
const registration = await navigator.serviceWorker.ready

plugins/pwa.client.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,48 @@ import { useRegisterSW } from 'virtual:pwa-register/vue'
22

33
export default defineNuxtPlugin(() => {
44
const online = useOnline()
5+
const registrationError = ref(false)
6+
const swActivated = ref(false)
7+
8+
const registerPeriodicSync = (swUrl: string, r: ServiceWorkerRegistration) => {
9+
setInterval(async () => {
10+
if (!online.value)
11+
return
12+
13+
const resp = await fetch(swUrl, {
14+
cache: 'no-store',
15+
headers: {
16+
'cache': 'no-store',
17+
'cache-control': 'no-cache',
18+
},
19+
})
20+
21+
if (resp?.status === 200)
22+
await r.update()
23+
}, 60 * 60 * 1000 /* 1 hour */)
24+
}
525

626
const {
727
needRefresh, updateServiceWorker,
828
} = useRegisterSW({
929
immediate: true,
30+
onRegisterError() {
31+
registrationError.value = true
32+
},
1033
onRegisteredSW(swUrl, r) {
11-
if (!r || r.installing)
12-
return
13-
14-
setInterval(async () => {
15-
if (!online.value)
16-
return
17-
18-
const resp = await fetch(swUrl, {
19-
cache: 'no-store',
20-
headers: {
21-
'cache': 'no-store',
22-
'cache-control': 'no-cache',
23-
},
34+
// should add support in pwa plugin
35+
if (r?.active?.state === 'activated') {
36+
swActivated.value = true
37+
registerPeriodicSync(swUrl, r)
38+
}
39+
else if (r?.installing) {
40+
r.installing.addEventListener('statechange', (e) => {
41+
const sw = e.target as ServiceWorker
42+
swActivated.value = sw.state === 'activated'
43+
if (swActivated.value)
44+
registerPeriodicSync(swUrl, r)
2445
})
25-
26-
if (resp?.status === 200)
27-
await r.update()
28-
}, 60 * 60 * 1000 /* 1 hour */)
46+
}
2947
},
3048
})
3149

@@ -36,6 +54,8 @@ export default defineNuxtPlugin(() => {
3654
return {
3755
provide: {
3856
pwa: reactive({
57+
swActivated,
58+
registrationError,
3959
needRefresh,
4060
updateServiceWorker,
4161
close,

0 commit comments

Comments
 (0)