From 26a8a33c8f853b84198a533a18fae2ed757066c3 Mon Sep 17 00:00:00 2001 From: Eli Mallon Date: Sat, 25 Jul 2026 12:06:08 -0700 Subject: [PATCH] fix: restore existing PushSubscription on page load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Greptile flagged that initPushNotifications didn't restore an existing PushSubscription into the store. The browser keeps subscriptions across reloads (and the server still has the row), but the Zustand store isn't persisted — so after a reload notificationToken was null, the toggle showed "off", and the user appeared unsubscribed despite still receiving pushes. Now initPushNotifications calls pushManager.getSubscription() after the service worker is ready and restores any existing subscription into the store. This mirrors the native path, where the FCM token is re-acquired on startup. enableWebNotifications remains safe to call on top of an existing subscription — pushManager.subscribe() is idempotent and returns the existing subscription rather than creating a duplicate. Co-Authored-By: Claude Opus 4.8 --- js/app/store/slices/platformSlice.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/js/app/store/slices/platformSlice.ts b/js/app/store/slices/platformSlice.ts index 80dc662d5..cd4d185b6 100644 --- a/js/app/store/slices/platformSlice.ts +++ b/js/app/store/slices/platformSlice.ts @@ -69,6 +69,17 @@ export const createPlatformSlice: StateCreator< } try { await navigator.serviceWorker.register("/sw.js"); + // Restore an existing PushSubscription into the store. The browser + // keeps subscriptions across reloads (and the server still has the + // row), but the Zustand store isn't persisted — so without this the + // toggle would show "off" after a reload even though the user is + // still subscribed. This mirrors the native path, where the FCM token + // is re-acquired on startup. + const reg = await navigator.serviceWorker.ready; + const existing = await reg.pushManager.getSubscription(); + if (existing) { + set({ notificationToken: JSON.stringify(existing) }); + } } catch (e) { console.log("service worker registration failed", e); } -- 2.51.2