-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
RFC: Fallback mode #663
Comments
Hi @kettanaito, Another condition could be to check |
Hey, @otaciliolacerda. I didn't know that |
+1 to this. Using the implementation for node as a base, I was able to set up the XHR and fetch interceptors (removing the client interceptor) and this worked at least for the basics. I had to change the rollup config to prioritize the browser field over the main field, so that the browser version of the debug module was used. |
Hey @kettanaito is there a reason why we don't see the requests in the network tab when MSW is using fallback mode? |
Hi, @romaolucas. Yes, there is. The fallback mode activates automatically in a browser that cannot run Service Worker for one reason or another. Since we cannot run the worker, we cannot intercept requests on the network level. Thus, you don't see the requests there. Instead, we employ a more conventional interception by stubbing |
If i understood it correctly it falls into fallback mode also in non-secure context? |
Hey, @catch99. Yes, you've understood correctly. Insecure contexts, such as serving an app over HTTP or an untrusted HTTPS, are where a Service Worker cannot register per spec. Instead of leaving you with a broken state, MSW falls back to a more conventional fetch/XHR patching to still keep the mocking functional. |
Hey @kettanaito! I appreciate this one is hard to completely answer without a reproduction library and I'm working on it, but question about fallback mode. I'm mocking an image response with the instructions in https://mswjs.io/docs/recipes/responding-with-binary/ in my Storybook entry (using Can you suggest any other way to mock an image request so that it still works in fallback mode? Thank you! |
Hi, @ahayes91. That's interesting. So, the fallback mode only ever kicks in if (1) the Service Worker API is not available in the current context; (2) your application is server over non-HTTP protocol (like Regarding the image binary mocking, I can't think of a reason it wouldn't work. The fallback mode relies on our Fetch and XHR interceptors, and those are rather well-tested. Can you please provide your request handlers and the request for the image itself? Of course, a full reproduction repo would be best. |
Thank you @kettanaito for the speedy response! I think this one will definitely be best answered with a reproduction repo, so let me get back to you in a few hours (hopefully, but it could be a few days 😅). |
@kettanaito I've got something here at https://github.com/ahayes91/msw-cypress-docker-storybook if you could have a look 🙇♀️ and thank you! Edit - raised #2225 for this, figured that's easier to keep track of! |
Description
Fallback mode is an alternative request interception mechanism applied in a browser. Fallback mode is automatically enabled by the library when either of the following conditions is met:
navigator.cookieEnabled
set tofalse
that forbids access to the worker;file://
protocol.Motivation
Fallback mode is aimed at improving the browser support and enabling the library in static builds of various tools (i.e. Storybook).
Statements
Technical details
Fallback mode is powered by the recent addition of the fetch and XMLHttpRequest interceptors to the
@mswjs/interceptors
library. Under the hood, fallback mode patcheswindow.fetch
andwindow.XMLHttpRequest
to provision the interception of requests.Pre-requisites
Public API
There are no changes to the public API related to this feature. Fallback mode is enabled automatically, when necessary.
The only user-facing change would be the change of the library's activation message:
In addition to the activation message change, the library should print the warning message indicating that the worker couldn't be registered in the current environment:
The text was updated successfully, but these errors were encountered: