diff --git a/packages/next/src/client/components/redirect.ts b/packages/next/src/client/components/redirect.ts index 19d9409cf5e6f1be6de4a2db8996580011ec2d73..1068bd4c11e85b21415303b2e70e279791916757 100644 --- a/packages/next/src/client/components/redirect.ts +++ b/packages/next/src/client/components/redirect.ts @@ -24,7 +24,8 @@ export function getRedirectError( * [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations). * * - In a Server Component, this will insert a meta tag to redirect the user to the target page. - * - In a Route Handler or Server Action, it will serve a 307/303 to the caller. + * - In a Route Handler, it will serve a 307 to the caller. + * - In a Server Action, it will perform a client-side navigation when JavaScript is available or serve a 303 for a progressive enhancement form submission. * - In a Server Action, type defaults to 'push' and 'replace' elsewhere. * * Read more: [Next.js Docs: `redirect`](https://nextjs.org/docs/app/api-reference/functions/redirect) @@ -46,7 +47,8 @@ export function redirect( * [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations). * * - In a Server Component, this will insert a meta tag to redirect the user to the target page. - * - In a Route Handler or Server Action, it will serve a 308/303 to the caller. + * - In a Route Handler, it will serve a 308 to the caller. + * - In a Server Action, it will perform a client-side navigation when JavaScript is available or serve a 303 for a progressive enhancement form submission. * * Read more: [Next.js Docs: `redirect`](https://nextjs.org/docs/app/api-reference/functions/redirect) */ diff --git a/packages/next/src/server/app-render/action-handler.ts b/packages/next/src/server/app-render/action-handler.ts index 4166b97d4f4c1e562320e3af516838f5610e2dfd..9cc3433fb07907621c49f915a4e6217c1f9227f4 100644 --- a/packages/next/src/server/app-render/action-handler.ts +++ b/packages/next/src/server/app-render/action-handler.ts @@ -1272,12 +1272,13 @@ export async function handleAction({ const redirectUrl = getURLFromRedirectError(err) const redirectType = getRedirectTypeFromError(err) - // if it's a fetch action, we'll set the status code for logging/debugging purposes - // but we won't set a Location header, as the redirect will be handled by the client router - res.statusCode = RedirectStatusCode.SeeOther - metadata.statusCode = RedirectStatusCode.SeeOther - if (isFetchAction) { + // Fetch actions communicate redirects through `x-action-redirect` and + // can include the redirect target's Flight response in the body. Since + // this is not an HTTP redirect, keep the response successful. + res.statusCode = 200 + metadata.statusCode = 200 + return { type: 'done', result: await createRedirectRenderResult( @@ -1294,6 +1295,8 @@ export async function handleAction({ } // For an MPA action, the redirect doesn't need a body, just a Location header. + res.statusCode = RedirectStatusCode.SeeOther + metadata.statusCode = RedirectStatusCode.SeeOther res.setHeader('Location', redirectUrl) return { type: 'done', diff --git a/packages/next/src/server/lib/router-utils/typegen.ts b/packages/next/src/server/lib/router-utils/typegen.ts index e162021d063e4662f01aaa28c30e91eee97b0d56..664532cfd02224cec9ab403de757b443e09c350d 100644 --- a/packages/next/src/server/lib/router-utils/typegen.ts +++ b/packages/next/src/server/lib/router-utils/typegen.ts @@ -378,7 +378,8 @@ declare module 'next/navigation' { * [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations). * * - In a Server Component, this will insert a meta tag to redirect the user to the target page. - * - In a Route Handler or Server Action, it will serve a 307/303 to the caller. + * - In a Route Handler, it will serve a 307 to the caller. + * - In a Server Action, it will perform a client-side navigation when JavaScript is available or serve a 303 for a progressive enhancement form submission. * - In a Server Action, type defaults to 'push' and 'replace' elsewhere. * * Read more: [Next.js Docs: redirect](https://nextjs.org/docs/app/api-reference/functions/redirect) @@ -396,7 +397,8 @@ declare module 'next/navigation' { * [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations). * * - In a Server Component, this will insert a meta tag to redirect the user to the target page. - * - In a Route Handler or Server Action, it will serve a 308/303 to the caller. + * - In a Route Handler, it will serve a 308 to the caller. + * - In a Server Action, it will perform a client-side navigation when JavaScript is available or serve a 303 for a progressive enhancement form submission. * * Read more: [Next.js Docs: redirect](https://nextjs.org/docs/app/api-reference/functions/redirect) */