fix: replace original request body after middleware execution (#77662)#7
Open
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
Open
fix: replace original request body after middleware execution (#77662)#7MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
Conversation
…#77662) In vercel#77553 we fixed reading the request body in middleware using the `nodejs` runtime. However, this caused issues with subsequent reads like in server actions. In sandbox, [we run](https://github.com/vercel/next.js/blob/1e62ce2c61048ddc0297f1a4f268894541975521/packages/next/src/server/web/sandbox/sandbox.ts#L146-L148) `.finalize()` after middleware is executed so we should do the same here. Fixes vercel#77646 --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where the original request body is not properly restored after middleware execution, specifically addressing GitHub issue vercel#56286. The fix ensures that middleware can read request bodies without interfering with downstream processing by cloning the body stream before passing it to middleware and finalizing it afterward.
Changes:
- Added request body cloning and finalization logic in the Next.js server adapter
- Enabled
nodeMiddleware: trueexperimental flag in test configurations - Created test variants specifically for node middleware runtime to ensure the fix works correctly
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/next/src/server/next-server.ts | Implements body stream cloning before middleware execution and finalization afterward |
| test/e2e/app-dir/actions/next.config.js | Enables nodeMiddleware experimental flag |
| test/e2e/app-dir/actions/middleware.js | Removes config matcher that was preventing middleware from running on certain paths |
| test/e2e/app-dir/actions/middleware-node.js | Adds new middleware file configured for nodejs runtime |
| test/e2e/app-dir/actions/app-action.test.ts | Adds conditional middleware file override based on TEST_NODE_MIDDLEWARE env var |
| test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts | Adds conditional middleware override and enables nodeMiddleware flag in test configs |
| test/e2e/app-dir/actions/app-action-size-limit-invalid-node-middleware.test.ts | New test entry point that sets TEST_NODE_MIDDLEWARE env var |
| test/e2e/app-dir/actions/app-action-node-middleware.test.ts | New test entry point for node middleware variant of main action tests |
| test/e2e/app-dir/actions/app-action-form-state.test.ts | Adds conditional middleware file override |
| test/e2e/app-dir/actions/app-action-form-state-node-middleware.test.ts | New test entry point for node middleware variant of form state tests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR_037