-
Notifications
You must be signed in to change notification settings - Fork 937
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
This is just me speculating, I'm not sure if this order of events is possible. |
||
}); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.