From 0db05b0fa6de318f164a1d73ddc55db9c59f1fc3 Mon Sep 17 00:00:00 2001 From: Claudomator Agent Date: Tue, 17 Mar 2026 08:04:52 +0000 Subject: fix: unsubscribe stale push subscription before re-subscribing When the VAPID key changes (e.g. after the key-swap fix), the browser's cached PushSubscription was created with the old key. Calling PushManager.subscribe() with a different applicationServerKey then throws "The provided applicationServerKey is not valid". Fix by calling getSubscription()/unsubscribe() before subscribe() so any stale subscription is cleared. Adds web test covering both the stale and fresh subscription paths. Co-Authored-By: Claude Sonnet 4.6 --- web/app.js | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'web/app.js') diff --git a/web/app.js b/web/app.js index eff8054..f785605 100644 --- a/web/app.js +++ b/web/app.js @@ -2638,6 +2638,14 @@ async function enableNotifications(btn) { await registerServiceWorker(); const registration = await navigator.serviceWorker.ready; + // Unsubscribe any stale subscription (e.g. from a VAPID key rotation). + // PushManager.subscribe() throws "applicationServerKey is not valid" if the + // existing subscription was created with a different key. + const existingSub = await registration.pushManager.getSubscription(); + if (existingSub) { + await existingSub.unsubscribe(); + } + // Subscribe via PushManager. const subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, -- cgit v1.2.3