summaryrefslogtreecommitdiff
path: root/web/app.js
diff options
context:
space:
mode:
authorPeter Stone <thepeterstone@gmail.com>2026-03-16 22:32:49 +0000
committerPeter Stone <thepeterstone@gmail.com>2026-03-16 22:32:49 +0000
commit95ea6932e8cc7b49556498d6c896fe6f17270aa2 (patch)
treeac5db2568dce4678ec790303fc0c9bd74f36743b /web/app.js
parentb0b79966e957bd5e4fc014978dbf7e9dbebeac61 (diff)
fix: wait for service worker to activate before subscribing to push
navigator.serviceWorker.register() returns before the SW is active. Use navigator.serviceWorker.ready which resolves only once a SW is controlling the page, so pushManager.subscribe() always has an active SW. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'web/app.js')
-rw-r--r--web/app.js9
1 files changed, 3 insertions, 6 deletions
diff --git a/web/app.js b/web/app.js
index 929b45d..eff8054 100644
--- a/web/app.js
+++ b/web/app.js
@@ -2634,12 +2634,9 @@ async function enableNotifications(btn) {
if (!keyRes.ok) throw new Error(`Failed to get VAPID key: HTTP ${keyRes.status}`);
const { public_key: vapidKey } = await keyRes.json();
- // Register service worker.
- const registration = await registerServiceWorker();
- if (!registration) {
- alert('Service worker registration failed.');
- return;
- }
+ // Register service worker and wait for it to become active.
+ await registerServiceWorker();
+ const registration = await navigator.serviceWorker.ready;
// Subscribe via PushManager.
const subscription = await registration.pushManager.subscribe({