Intermittent failure with large request bodies in middleware requires deep understanding of Next.js internals.
The issue involves intermittent failures when processing large request bodies (>350MB) in Next.js middleware, leading to a fatal error. The problem appears to be related to Node.js runtime and Next.js internals, with a hardware-dependent threshold. A fix would require deep understanding of both Next.js and Node.js request handling mechanisms.
https://github.com/platformshape/nextjs-body-bug-repro
npm install && npm run devchmod +x test.sh && ./test.sh 500 && ./test.sh 500The issue is intermittent and never fails on the first request after launching the server. It does however fail after a few tries.
Next.js v15.5.2 throws a fatal error when using Node.js runtime middleware with large request bodies (>350MB):
TypeError: Response body object should not be disturbed or locked
at extractBody (node:internal/deps/undici/undici:5574:17)
at new Request (node:internal/deps/undici/undici:9801:48)
at new NextRequest (/path/to/next/server/chunks/node_modules_next_dist_*.js)
This results in a 500 Internal Server Error and prevents processing of large file uploads through middleware.
Expected: Large file uploads processed successfully through middleware to API route.
Actual:
| File Size | Status | Notes |
|---|---|---|
| 100MB | ✅ Works | No issues |
| 300MB | ✅ Works | No issues |
| 350MB | ✅ Works | Threshold |
| 375MB | ❌ Fails | Consistent failure |
| 400MB+ | ❌ Fails | Always fails |
Console Error:
TypeError: Response body object should not be disturbed or locked
at extractBody (node:internal/deps/undici/undici:5574:17)
at new Request (node:internal/deps/undici/undici:9801:48)
at new NextRequest (/private/tmp/my-app/.next/server/chunks/node_modules_next_dist_eb0c80da._.js:5368:14)
at NextRequestAdapter.fromNodeNextRequest (/private/tmp/my-app/.next/server/chunks/node_modules_next_dist_eb0c80da._.js:5539:16)
at handler (/private/tmp/my-app/.next/server/chunks/node_modules_next_dist_eb0c80da._.js:8127:246)
Server Logs:
Middleware: POST http://localhost:3000/api/upload
Claim this issue to let others know you're working on it. You'll earn 35 points when you complete it!