Support Edge Runtime (no http module) #1429
Replies: 9 comments 15 replies
-
From a brief inspection, this looks like it would be supported by creating a third startup option: |
Beta Was this translation helpful? Give feedback.
-
I wonder if we should use interceptors based on the availability of those modules. setupServer([
typeof XMLHttpRequest !== 'undefined' && XMLHttpRequestInterceptor
]) Will be hard to figure out Node.js built-ins like Overall, edge support would be great but I don't see it as a priority anytime soon. If someone would like to make this happen without reworking much of the library, I'd be open to releasing it. I think the conceptual issue here is that such Edge runtimes are still a custom thing. They do respect the standards but |
Beta Was this translation helpful? Give feedback.
-
To make things a little more complex, there's no such thing as a standardized Edge environment. There's the browser and there's Node.js. I suppose edge frameworks try to emulate Node.js but may be implemented in different languages and the standard library is usually limited and incomplete. This makes it not an actual environment, really, but an infinite list of subsets of an environment. Indeed, things may get a little easier with Edge frameworks adopting web standards, which some of the most popular ones already do, and MSW adopting them as well. But both those things need to happen for us to claim Edge support, for "Edge" is not really the same thing when referred to by different people. I may be terribly wrong on these things so do not hesitate to correct me. |
Beta Was this translation helpful? Give feedback.
-
I will also move this to a discussion because, as I've said, there's no intention on working on this any time soon. |
Beta Was this translation helpful? Give feedback.
-
I think your read on this seems correct @kettanaito. I was hopeful that using |
Beta Was this translation helpful? Give feedback.
-
Hi @kettanaito I want to briefly discuss what @Toxiapo and I are thinking for this. Our tentative plan is currently:
Does that sound about right? Do you imaging that a generic |
Beta Was this translation helpful? Give feedback.
-
Hi @kettanaito, thanks again for shipping this new API! I was able to successfully set up an MVP with the new API for the Edge environment, but I am running into an issue that I would love to hear your thoughts on.
const useFetch: (input: RequestInfo, init?: RequestInit) => Promise<Response> =
isNodeProcess()
? (input, init) =>
import('node-fetch').then(({ default: nodeFetch }) =>
(nodeFetch as unknown as typeof window.fetch)(input, init),
)
: globalThis.fetch
? globalThis.fetch
: window.fetch if this is something that you think is ok to do, I can create a quick PR for this. Happy to hear any other suggestions as well! |
Beta Was this translation helpful? Give feedback.
-
Hey @kettanaito, do you have any news on this? |
Beta Was this translation helpful? Give feedback.
-
Scope
Improves an existing behavior
Compatibility
Feature description
NextJS has introduced an Edge Runtime (more docs here, which is a stricter node.js runtime based on web standards to run "on the edge". This is used, for example, but their middleware.
I am performing user authentication in this middleware, and would like to mock the
fetch
requests I make in my middleware. This means I need MSW to be compatible with the edge runtime and support intercepting fetch requests made there.I have cloned the with-msw example and added a
middleware.ts
file which makes a simplefetch
request. The failure can be seen in my repository: with-msw-appThe error I see when trying to run this is:
Beta Was this translation helpful? Give feedback.
All reactions