Skip to content

Wait for service worker registration to become active before any operations #8661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-beans-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/messaging': patch
---

Fix an issue where PushManager.subscribe() is called too soon after registering the default service worker.
31 changes: 31 additions & 0 deletions packages/messaging/src/helpers/registerDefaultSw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,40 @@ export async function registerDefaultSw(
messaging.swRegistration.update().catch(() => {
/* it is non blocking and we don't care if it failed */
});
await waitForRegistrationActive(messaging.swRegistration);
} catch (e) {
throw ERROR_FACTORY.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, {
browserErrorMessage: (e as Error)?.message
});
}
}

/**
* Waits for registration to become active. MDN documentation claims that
* a service worker registration should be ready to use after awaiting
* navigator.serviceWorker.register() but that doesn't seem to be the case in
* practice, causing the SDK to throw errors when calling
* swRegistration.pushManager.subscribe() too soon after register(). The only
* solution seems to be waiting for the service worker registration `state`
* to become "active".
*/
async function waitForRegistrationActive(
registration: ServiceWorkerRegistration
): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (registration.active) {
resolve();
}
const incomingSw = registration.installing || registration.waiting;
if (incomingSw) {
incomingSw.onstatechange = ev => {
if ((ev.target as ServiceWorker)?.state === 'activated') {
incomingSw.onstatechange = null;
resolve();
}
};
} else {
reject(new Error('No incoming service worker found.'));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be extremely unlikely, but if the registration transitions to the active state after if(registration.active) and before const incomingSw = registration.installing || registration.waiting, I think we would reject and the registration would fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to an if/else but not sure if this affects the timing at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a tricky timing issue. I think now the SW could become active before we add the event listener, causing us to miss the activated event. For example:

  1. registration is waiting
  2. registration is not yet active, so we don't resolve immediately on line 79.
  3. registration becomes active, and the 'activated' event fires
  4. attach registration state event listener
  5. event listener never fires because we missed the 'activated' event and we reject the promise after 10s

This is just me speculating, I'm not sure if this order of events is possible.

});
}
Loading