next.js: Using Undici as node fetch polyfill causes fetch failed
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
N/A
Which area(s) of Next.js are affected? (leave empty if unsure)
Data fetching (gS(S)P, getInitialProps)
Link to the code that reproduces this issue
To Reproduce
Ensure project is using serverless functions that are from the frankfurt network (so far I know this one causes the problem, but maybe others do as well)

Below are my steps, but they should be entirely optional since replicating this is possible in different ways.
-
have a next api endpoint that fires a tRCP call using tRPC’s client
-
call the endpoint
-
observe that fetch failed because of undici (node fetch polyfill)
Describe the Bug
Big thanks to TillaTheHun0 for reporting this.
I want to make a bug report since I replicated and can confirm his thesis.
Next has a node polyfill for fetch, which uses either undici or node-fetch depending on an environment variable called __NEXT_USE_UNDICI. This env variable is likely set by Vercel depending on which network the Serverless Functions are configured to execute on.
I tested countless times, and the thing that solved my problems was indeed changing the network location of my serverless functions in my Project Settings on Vercel.
Undici currently has some issue (RequestContentLengthMismatchError), partially documented here. This issue is causing all sorts of problems from different places since this fetch polyfill is used by many other tools.
In my case it was tRPC complaining, with:
TRPCClientError: fetch failed
at TRPCClientError.from (file:///var/task/node_modules/@trpc/client/dist/transformResult-6fb67924.mjs:13:16)
at file:///var/task/node_modules/@trpc/client/dist/links/httpBatchLink.mjs:211:64 {
meta: undefined,
shape: undefined,
data: undefined,
[cause]: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:14062:11) {
cause: RequestContentLengthMismatchError: Request body length does not match content-length header
at AsyncWriter.end (node:internal/deps/undici/undici:9704:19)
at writeIterable (node:internal/deps/undici/undici:9614:16) {
code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}
}
}
though I read around different places people encountering the same problem in various ways.
We should either use only one polyfill (the working one) while waiting for undici to be patched, or we should allow people to explicitly choose which polyfill they’d like through configuring __NEXT_USE_UNDICI somewhere. Though I have a feeling that going with either option could be impossible if there’s unknown limitations to the machine running the Functions that make it so undici is chosen over node-fetch.
Regardless, it is currently a fact that the “Project Settings” > “Functions” page (on Vercel) does not have any information about this behaviour. At the very least we should inform users that there’s environment variables that could change depending on the chosen region for the Serverless Functions. Optimally we should provide a list with environment variables and their configuration for the different regions, and ideally also how these map to certain behaviours. At the very least this would speed up debugging and problem solving IMO. (this part might not be specific to the Next repo though)
Expected Behavior
fetch requests should not fail like this, regardless of which serverless function they are running on.
Which browser are you using? (if relevant)
N/A
How are you deploying your application? (if relevant)
N/A
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 19
- Comments: 34 (5 by maintainers)
I’ m also seeing this error, but it seems to be in the context of a server action failing to redirect.
I have a server action that does this:
Which throws:
I have an inbound PR that fixes it
https://github.com/vercel/next.js/pull/53843
I started to see this too in server actions
content-length is 2 on the response but body is empty
I ran into this same issue when using
redirect()inside a server action. I walked through a few Next versions to see where I would get the fix. I was finally able to solve this by downgrading Next as follows:I have tested this fix with the development server inside a
node:latest(v20.5.0) docker container and I am no longer runing into the error.Any known workaround for this?
This started for me today too on 13.4.13
I’m facing the same issue here
I am also seeing the same issue when redirecting to a page from a server action
I’m having the same issue as well.
I’m having the same issue with a redirect inside a server action
In my case this happened when the server’s content type header is not what actually is. For example the content type says it’s gzip but the content is without compression, thus causing the decompression failed.
To exclude/verify if this is your case, you can add this header temporarily and try again:
Seeing lots of undici issues across multiple projects on latest next and latest node 18:
Same issue here. Works on 13.4.7 but not on 13.4.13 (using signIn function from next-auth on a custom login page).
Also having this issue in the redirect. I’ve created a reproduction here https://github.com/wladiston/reproduction-next-fetch-bug
It seems like if I pin next to
13.4.7the error goes away.I wrote that, you’re welcome 😃)
Ran into similar issues - weirdly enough in
iad1 Washington. After some googling, I stumbled across this article which recommended usingfetch-ponyfill'. This fixed the issue for me.@julioxavierr using
node-fetchsolves this problem, but unfortunately it doesn’t fit well as next fetch do.+1 to this. I’m on
14.0.5-canaryand getting this error on Vercel during build.I’m using Apollo Client Nextjs for
useSuspenseQuery()/ rsc query in static pages and the error happens during build when pre-rendering the page.This error doesn’t happen in local dev env.