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>
Greptile OverviewGreptile SummaryFixed 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:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
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
|
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