[Fix] Service Worker race condition, when a 2nd sw is in play, causing User not to register #1136
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
1 Line Summary
Fix Service Worker race condition, when a 2nd sw is in play, causing User not to register.
Details
If you attempt to get the service worker registration immediately after registering it the browser can give you a different registration than expected. This is due to the fact the new registration may not be ready yet and there might be sw in scope at a higher level.
The problem noted above cause the OneSignal SDK to register for a push token on the wrong service worker. This in turn causes the SDK to attempt to create a user with a push subscription but without a push token, which results in a
400
error onPOST /users
.To fix, instead of calling
navigator.serviceWorker.getRegistration
we are now simply using theServiceWorkerRegistration
give to us bynavigator.serviceWorker.register
to avoid this race condition completelyValidation
Tests
Test on Chrome 119 on Windows 11
Info
Checklist
Programming Checklist
Interfaces:
Functions:
Typescript:
Other:
elem of array
syntax. PreferforEach
or usemap
context
if possible. Instead, we can pass it to function/constructor so that we don't callOneSignal.context
Screenshots
Info
Checklist
Related Tickets
This change is