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

Retrieves worker client from "ServiceWorkerGlobalScope" instead of "FetchEvent.target" #565

Merged
merged 1 commit into from
Jan 25, 2021

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jan 24, 2021

GitHub

Motivation

const client = await event.target.clients.get(clientId)

FetchEvent.target equals null in Safari only. In other browsers FetchEvent.target equals self (ServiceWorkerGlobalScope) instance. Here's a snapshot of the entire "fetch" event scope in Safari:

Screen Shot 2021-01-25 at 15 17 34

This pull request substitutes:

-event.target.clients.get(clientId)
+this.clients.get(clientId)

Since clientId comes from the FetchEvent.clientId and is persistent in all browsers, it matters little which ServiceWorker global scope resolves it.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 24, 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 b663d8c:

Sandbox Source
MSW React Configuration

@@ -97,9 +97,11 @@ self.addEventListener('fetch', function (event) {
return
}

const clientPromise = event.target.clients.get(clientId)
Copy link
Member

Choose a reason for hiding this comment

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

Because we don't have a way to test it, we can add a comment referring to the issue to remember in the future why this line has moved outside. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that reason can be looked up in Git commit history. Now I’m wondering if we can test this somehow... the issue happened only on Safari.

Copy link
Member

Choose a reason for hiding this comment

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

Yes I know and I think that we can't run puppeteer in Safari. I know that it can be found in git history but because we don't have a test for it, in case of change, it will not break anything. Of course, if we find a way to test it comment the line will be not very helpful

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try to analyze what exactly goes different in Safari and see if we can simulate that scenario in a test.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was unable to create a test suite for this. event.target is null in Safari only, I don't see how I can simulate that in a test. I've tried issuing a request from ClientA, then closing that client, and observing what happens in ClientB controlled by the same worker. I'd expect that since the client was closed, the event.target or at least evet.target.clients.get will be null, but it's not. At least, it doesn't throw an exception.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this morning I have tried to take a look at the issue but no found a way to test too. The only thing that I Have found is that the first time target is not null. It will become null after the first await event.target.clients.get(clientId)

@kettanaito kettanaito force-pushed the 558-sw-client branch 3 times, most recently from fe1a32c to a48175f Compare January 25, 2021 11:56
@kettanaito kettanaito changed the title Retrieves client on the root scope of the fetch event Retrieves worker client from "ServiceWorkerGlobalScope" instead of "FetchEvent.target" Jan 25, 2021
@kettanaito kettanaito added the BREAKING CHANGE Pull request introducing breaking changes. label Jan 25, 2021
Copy link
Member

@marcosvega91 marcosvega91 left a comment

Choose a reason for hiding this comment

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

is ready for me :)

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

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

This also LGTM.
I wonder if we can do the same at const client = await event.currentTarget.clients.get(clientId) when we receive a message. Because this could potentially also provide a fix for #536?
Since the linked issue (#536) now includes a reproduction, I'll try again to reproduce that behavior in our test suite.

@kettanaito
Copy link
Member Author

@marcosvega91, @timdeschryver thank you for the review, gentlemen!

@timdeschryver that'd be a great fix indeed. I suggest we merge this and see if your suggestion could eliminate the issue. Eager to hear more about your findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE Pull request introducing breaking changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fetchEvent.respondWith Receives an Error in Safari Only
3 participants