From be69dd97e79b479218c4dfdcc47fe776aba684c6 Mon Sep 17 00:00:00 2001 From: Dorian Date: Fri, 15 May 2026 14:31:55 -0500 Subject: [PATCH] Force PWA updates before push registration --- server/server.js | 6 +++++- src/main.js | 11 ++++++++++- src/services/notifications.js | 5 ++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/server/server.js b/server/server.js index f699fb7..105ce7e 100644 --- a/server/server.js +++ b/server/server.js @@ -469,7 +469,11 @@ const serveStatic = (req, res) => { '.json': 'application/json', '.webmanifest': 'application/manifest+json', } - res.writeHead(200, { 'Content-Type': types[ext] || 'application/octet-stream' }) + const headers = { 'Content-Type': types[ext] || 'application/octet-stream' } + if (path.basename(safePath) === 'sw.js' || ext === '.html' || ext === '.webmanifest') { + headers['Cache-Control'] = 'no-store' + } + res.writeHead(200, headers) createReadStream(safePath).pipe(res) } diff --git a/src/main.js b/src/main.js index d8e0c89..0976974 100644 --- a/src/main.js +++ b/src/main.js @@ -6,8 +6,17 @@ createApp(App).mount('#app') if ('serviceWorker' in navigator) { window.addEventListener('load', () => { - navigator.serviceWorker.register('/sw.js').catch((error) => { + navigator.serviceWorker.register('/sw.js', { updateViaCache: 'none' }).then((registration) => { + registration.update().catch(() => {}) + }).catch((error) => { console.warn('Service worker registration failed:', error) }) }) + + let refreshing = false + navigator.serviceWorker.addEventListener('controllerchange', () => { + if (refreshing) return + refreshing = true + window.location.reload() + }) } diff --git a/src/services/notifications.js b/src/services/notifications.js index 67da23b..3076d35 100644 --- a/src/services/notifications.js +++ b/src/services/notifications.js @@ -26,6 +26,9 @@ const explainSubscribeError = async (error) => { 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.' } + 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 `Push registration failed: ${message}. Fully close and reopen the installed app, then try again.` } @@ -77,7 +80,7 @@ export const subscribeToNotifications = async () => { permission.value = requested if (requested !== 'granted') throw new Error('Notification permission was not granted.') - const registered = await navigator.serviceWorker.register('/sw.js', { scope: '/' }) + const registered = await navigator.serviceWorker.register('/sw.js', { scope: '/', updateViaCache: 'none' }) await registered.update().catch(() => {}) const readyRegistration = await navigator.serviceWorker.ready const existing = await readyRegistration.pushManager.getSubscription()