Skip to content

fix: replace original request body after middleware execution (#77662)#7

Open
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
pr_037_after
Open

fix: replace original request body after middleware execution (#77662)#7
MitchLewis930 wants to merge 1 commit intopr_037_beforefrom
pr_037_after

Conversation

@MitchLewis930
Copy link

PR_037

…#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>
@MitchLewis930 MitchLewis930 requested a review from Copilot January 31, 2026 01:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: true experimental 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants