Repair push registration before failing

This commit is contained in:
Dorian
2026-05-15 14:47:12 -05:00
parent 5cdadb35db
commit 85ff628926

View File

@@ -24,14 +24,27 @@ const explainSubscribeError = async (error) => {
const message = error instanceof Error ? error.message : 'push service error'
const brave = await isBraveBrowser()
if (brave && /push service|registration failed|not available|permission/i.test(message)) {
return 'Brave is blocking Web Push. In Brave settings, enable "Use Google services for push messaging", then fully close and reopen the installed L484 app.'
return 'Brave still cannot reach its Web Push service. Fully quit Brave, reopen the installed L484 app, and try again. If it still fails, remove and reinstall the L484 app after enabling Brave push messaging.'
}
if (/push service|registration failed|not available/i.test(message)) {
return 'The browser push service is unavailable. In Brave, enable "Use Google services for push messaging"; otherwise fully close and reopen the installed app, then try again.'
return 'The browser push service is unavailable. Fully quit the browser, reopen the installed L484 app, and try again.'
}
return `Push registration failed: ${message}. Fully close and reopen the installed app, then try again.`
}
const repairServiceWorkerRegistration = async () => {
const registrations = await navigator.serviceWorker.getRegistrations()
await Promise.all(registrations.map((registration) => registration.unregister().catch(() => false)))
if ('caches' in window) {
const keys = await caches.keys()
await Promise.all(keys.filter((key) => key.startsWith('l484-')).map((key) => caches.delete(key)))
}
await new Promise((resolve) => window.setTimeout(resolve, 500))
const repaired = await navigator.serviceWorker.register('/sw.js', { scope: '/', updateViaCache: 'none' })
await repaired.update().catch(() => {})
return navigator.serviceWorker.ready
}
export const notificationPermission = permission
export const notificationSupport = () => ({
@@ -60,7 +73,12 @@ const subscribeWithRetry = async (registration, applicationServerKey) => {
try {
return await activeRegistration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey })
} catch (retryError) {
throw new Error(await explainSubscribeError(retryError || error))
try {
const repairedRegistration = await repairServiceWorkerRegistration()
return await repairedRegistration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey })
} catch (repairError) {
throw new Error(await explainSubscribeError(repairError || retryError || error))
}
}
}
}