mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 21:10:59 +00:00
refactor(pwa): defer push subscription to the app-wide registration
The Web-Push opt-in registered its own `/sw.js` before subscribing. With the app-wide registrar now installing the worker for every user, that second `register` call is a redundant source of truth at the same scope. Replace it with `navigator.serviceWorker.ready`, which resolves with the active registration once the global worker is controlling the page.
This commit is contained in:
1 parent
0c4b6ab8ba
commit
083410473c
1 file changed
+6
-2
@@ -81,8 +81,12 @@ export function WebPushCard() {
|
||||
const vapidData = await vapidRes.json();
|
||||
const vapidPublicKey = vapidData.data.publicKey;
|
||||
|
||||
const registration = await navigator.serviceWorker.register("/sw.js");
|
||||
await navigator.serviceWorker.ready;
|
||||
// Defer to the app-wide registration (`<ServiceWorkerRegistrar>` in
|
||||
// the root providers) rather than re-registering here — same scope,
|
||||
// so a second `register("/sw.js")` would only return the existing
|
||||
// worker. `ready` resolves with the active registration once it is
|
||||
// controlling the page.
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
|
||||
const subscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
|
||||
Reference in new issue
Block a user