Files
vercel__next.js/test/e2e/app-dir/actions/middleware-node.js
Lucas Felicio Adriano 35d6fdf074 fix(nodejs-middleware): await for body cloning to be properly finalized (#85418)
Fixes https://github.com/vercel/next.js/issues/85416.

This is a follow up to https://github.com/vercel/next.js/pull/77662,
where the Node.js middleware was fixed to also support reading (and
duplicating) the body's request before being passed to the server
action.

However, in that PR, an `await` was missed while calling the
[.finalize()](https://github.com/vercel/next.js/blob/1b45163bce3dab30d7a79dff95238b84016b54c2/packages/next/src/server/body-streams.ts#L39)
method, which returns a Promise. That missing `await` would sometimes
cause a bug, where the server action would be invoked before the body
was completely cloned, causing the bug mentioned in the issue above.

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2025-11-13 18:11:21 -08:00

28 lines
695 B
JavaScript

// Ensure that https://github.com/vercel/next.js/issues/56286 is fixed.
import { NextResponse } from 'next/server'
export async function middleware(req) {
if (req.nextUrl.pathname.includes('rewrite-to-static-first')) {
req.nextUrl.pathname = '/static/first'
return NextResponse.rewrite(req.nextUrl)
}
if (req.method === 'POST' && req.nextUrl.pathname.includes('body-finalize')) {
const body = await req.json()
console.log(
'Middleware - Body length: %d bytes',
new TextEncoder().encode(JSON.stringify(body)).length
)
}
return NextResponse.next()
}
/**
* @type {import('next/server').ProxyConfig}
*/
export const config = {
runtime: 'nodejs',
}