Skip to content
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

Merged
merged 3 commits into from
Jan 26, 2021
Merged

fix: add client guards in service worker #551

merged 3 commits into from
Jan 26, 2021

Conversation

timdeschryver
Copy link
Member

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 20, 2021

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:

Sandbox Source
MSW React Configuration

@@ -49,6 +49,11 @@ export function setupWorker(
navigator.serviceWorker,
'message',
(event: MessageEvent) => {
// Avoid messages broadcasted from unrelated workers.
if (event.source !== context.worker) {
Copy link
Member

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.

@kettanaito
Copy link
Member

Thank you for tackling this, @timdeschryver 🎉

@kettanaito kettanaito marked this pull request as ready for review January 26, 2021 12:23
@kettanaito kettanaito merged commit 53169f6 into master Jan 26, 2021
@kettanaito kettanaito deleted the message-guards branch January 26, 2021 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not listen to all messages posted on broswer
2 participants