Skip to content

Comments

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>
@greptile-apps
Copy link

greptile-apps bot commented Jan 29, 2026

Greptile Overview

Greptile Summary

Fixed issue vercel#56286 where request bodies were consumed during Node.js middleware execution, making them unavailable for subsequent handlers. The solution clones the request body stream before passing it to middleware, then restores the original body after middleware completes, matching the existing behavior in the edge runtime.

Key Changes:

  • Implemented body stream cloning in next-server.ts for Node.js middleware (lines 1690-1709)
  • Added try-finally block to ensure body restoration even if middleware errors
  • Pattern mirrors existing edge runtime implementation in sandbox.ts
  • Comprehensive test coverage added with Node.js middleware variants
  • Removed restrictive middleware matcher that was working around this issue

Confidence Score: 5/5

  • Safe to merge - well-tested fix that aligns Node.js middleware with edge runtime behavior
  • Implementation follows established pattern from edge runtime, comprehensive test coverage added, properly handles edge cases with HEAD/GET methods, uses try-finally for cleanup guarantees
  • No files require special attention

Important Files Changed

Filename Overview
packages/next/src/server/next-server.ts Added request body cloning and finalization for Node.js middleware, matching edge runtime behavior
test/e2e/app-dir/actions/middleware-node.js New Node.js runtime middleware for testing issue vercel#56286 fix
test/e2e/app-dir/actions/middleware.js Removed restrictive matcher to allow middleware to run on all routes including decode-req-body

Sequence Diagram

sequenceDiagram
    participant Client
    participant NextServer as Next.js Server
    participant MW as Node Middleware
    participant Body as CloneableBody
    participant Handler as Request Handler

    Client->>NextServer: POST Request with Body
    NextServer->>NextServer: Check if request has body<br/>(non-HEAD/GET + body present)
    
    alt Request has body
        NextServer->>Body: cloneBodyStream()
        Body->>Body: Create PassThrough streams (p1, p2)
        Body->>Body: Buffer original stream to p2
        Body-->>NextServer: Return p1 clone
        
        NextServer->>MW: Execute middleware with cloned body
        MW->>MW: Process request
        MW-->>NextServer: Return result
        
        NextServer->>Body: finalize()
        Body->>Body: Wait for stream end
        Body->>Body: Replace original request body with buffered p2
        Body-->>NextServer: Body restored
    else Request has no body
        NextServer->>MW: Execute middleware (no body)
        MW->>MW: Process request
        MW-->>NextServer: Return result
    end
    
    NextServer->>Handler: Forward request with original body
    Handler-->>NextServer: Response
    NextServer-->>Client: Response
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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.

2 participants