-
Couldn't load subscription status.
- Fork 29.7k
Description
Link to the code that reproduces this issue
https://github.com/lucasadrianof/nextjs-middleware-bug
To Reproduce
pnpm installfrom the project folder.pnpm build. This will build the project and add a random delay to the body cloning process to make the bug happen more consistently.pnpm start.- Open http://localhost:3000/ in your browser.
- Click the
Send Large Payloadbutton. - The request will fail. You should see a
SyntaxError: Unexpected end of JSON inputin your terminal.
~/Code/Tests/nextjs-middleware-bug main* ⇡
❯ pnpm start
> [email protected] start /Users/lucas/Code/Tests/nextjs-middleware-bug
> node .next/standalone/server.js
▲ Next.js 16.0.1-canary.3
- Local: http://localhost:3000
- Network: http://0.0.0.0:3000
✓ Starting...
✓ Ready in 37ms
Proxy - body length: 50333 bytes
⨯ SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>) {
digest: '3042448573'
}
Current vs. Expected behavior
Current behavior:
Expected behavior:
The request body cloning should be properly awaited before invoking the server action handler. This is the output I get once I run the script to apply the bugfix:
To reproduce it, you can follow the steps from the previous section, but run pnpm bugfix before pnpm start. That will patch the file with the missing await, fixing the previous error.
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:26 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6041
Available memory (MB): 24576
Available CPU cores: 14
Binaries:
Node: 22.13.0
npm: 11.2.0
Yarn: N/A
pnpm: 10.7.1
Relevant Packages:
next: 16.0.0 // Latest available version is detected (16.0.0).
eslint-config-next: N/A
react: 19.2.0
react-dom: 19.2.0
typescript: N/A
Next.js Config:
output: standaloneWhich area(s) are affected? (Select all that apply)
Middleware, Server Actions
Which stage(s) are affected? (Select all that apply)
next start (local)
Additional context
Hello!
We noticed this bug in our production app, where we're running Next.js with the output: standalone option with Docker.
This bug is really hard to be reproduced locally, since it happens in a race condition, when there's some sort of delay between cloning the request body. It also depends on the request body being relatively large. It was specially tricky to reproduce it while trying to debug it, since the await in the debugging would invalidate the race condition 100% of the times.
So to make this consistently reproducible locally, I added a random delay in that specific function.
This error basically happens because we're not awaiting the body to be fully cloned before invoking the server action, in here:
next.js/packages/next/src/server/next-server.ts
Line 1757 in 1b45163
| requestData.body.finalize() |
That method returns a Promise, as defined in here:
| finalize(): Promise<void> |