-
-
Notifications
You must be signed in to change notification settings - Fork 519
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
Conditionally disable SSE in mockServiceWorker #1882
Comments
Hi. This is an interesting use case, I would like to learn more about it. The thing is, the fetch listener must be synchronous. But awaiting for the potential mocked response is async (messaging is async by design + response resolvers can be async). The only way to achieve asynchronicity in the fetch listener is to operate in a promise passed to respondWith. Hope this clarifies why it's structured that way. Passthrough should not result in an extra request though. I believe if we return in a promise, the worker will continue with executing that request as-is. Can you please verify that? |
Based on the source code, Lines 186 to 195 in 657edd8
I wonder if simply returning (resolving the |
Based on the This means that MSW cannot short-circuit the request faster in any way—it's not allowed by the platform. I'm afraid there's not much we can do here. |
I can see that any client communication (or even getting the relevant client), requires async logic, so it's not possible to do that in the handler. Would this behaviour (that EventSource is requiring CORS as soon as you install the service worker) something that should be documented, so others won't be surprised by it in the future? Purely as a train of thought, a potential solution could be something like:
Unsure if this is desired, since it could result in race conditions (request being executed before the configuration is available in the worker), will add more code to the worker file (which you might want to keep as lean as possible). But maybe you would like to think it over :) |
Scope
Improves an existing behavior
Compatibility
Feature description
This is related to #156 and #834.
Note that i'm using https://github.com/mswjs/msw-storybook-addon, but I believe the core "issue" lies in this library.
I'm also using version 2 of MSW currently.
This request comes from the following scenario:
self.addEventListener('fetch'
, even when configuring globally asbypass
, or in an handler aspassthrough
, there is afetch
request executed inside the Service Worker.fetch
request inside the Service Worker cause the browser to first do an OPTIONS preflight, which fails.I presume that the philosophy of MSW is to make this as transparent as possible, and not require extra server configuration when MSW is turned on.
I could patch the
mockServiceWorker.js
with something like suggested in the other issue:However, even if this was added as "configuration option for msw", it would completely disallow mocking some connections in some handlers. Especially in storybook, where MSW is inited from a global location, these options could not be changed for different stories (that can only update the current active handlers).
It seems like that as soon as
event.respondWith
is called, no matter what happens in the registered handlers, the normal browser fetch will NOT execute.For this "feature", I would like to say in a handler – case by case – wether to use
respondWith
, or to doreturn
and let the original browser request happen.If the handler execution is done before the
respondWith
, this might be possible?So the handler can return something specific (like
ignore
instead ofpassthrough
).I do see the issue here, that
handleRequest
can be async, andevent.respondWith
must be called synchronously in the event handler (i presume), and that supporting this would require some re-architecture.Happy to hear your thoughts on this!
The text was updated successfully, but these errors were encountered: