-
-
Notifications
You must be signed in to change notification settings - Fork 473
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
fix: add client guards in service worker #551
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 411c381:
|
1b714a1
to
411c381
Compare
@@ -49,6 +49,11 @@ export function setupWorker( | |||
navigator.serviceWorker, | |||
'message', | |||
(event: MessageEvent) => { | |||
// Avoid messages broadcasted from unrelated workers. | |||
if (event.source !== context.worker) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good enhancement described in this article.
Since we establish a global listener on navigator.serviceWorker
, it can receive messages from other workers that may control the current client. With this check we're proceeding with event handling only if the source of the event (message) is our MSW worker.
Thank you for tackling this, @timdeschryver 🎉 |
Closes #536
This PR adds guard clauses to prevent the service worker throwing errors when the client isn't found.
Making this a draft until we can reproduce the behavior in #536