Unverified Commit 77f8889b authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

use 303 status code for redirects in fetch actions (#59017)

### What?
A `redirect` that occurs during a fetch action will get a status code of
200, while the redirection logic is handled client-side.

### Why?
In this scenario, the redirect is handled by the client router, so no
`Location` is set on the action response. However for debugging /
logging purposes, it'd be useful to still return the same status code
used in other cases (see #58885)

### How?
Rather than selectively setting the status to 303 in the non-fetch
action case, this always applies it.

Closes NEXT-1745
parent e8c02736
...@@ -577,13 +577,15 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc ...@@ -577,13 +577,15 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc
const redirectUrl = getURLFromRedirectError(err) const redirectUrl = getURLFromRedirectError(err)
const statusCode = getRedirectStatusCodeFromError(err) const statusCode = getRedirectStatusCodeFromError(err)
// if it's a fetch action, we don't want to mess with the status code
// and we'll handle it on the client router
await addRevalidationHeader(res, { await addRevalidationHeader(res, {
staticGenerationStore, staticGenerationStore,
requestStore, requestStore,
}) })
// 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 = statusCode
if (isFetchAction) { if (isFetchAction) {
return { return {
type: 'done', type: 'done',
...@@ -607,7 +609,6 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc ...@@ -607,7 +609,6 @@ To configure the body size limit for Server Actions, see: https://nextjs.org/doc
} }
res.setHeader('Location', redirectUrl) res.setHeader('Location', redirectUrl)
res.statusCode = statusCode
return { return {
type: 'done', type: 'done',
result: RenderResult.fromStatic(''), result: RenderResult.fromStatic(''),
......
...@@ -592,7 +592,17 @@ createNextDescribe( ...@@ -592,7 +592,17 @@ createNextDescribe(
}) })
it('should handle redirect to a relative URL in a single pass', async () => { it('should handle redirect to a relative URL in a single pass', async () => {
const browser = await next.browser('/client') let responseCode: number
const browser = await next.browser('/client', {
beforePageLoad(page) {
page.on('response', async (res: Response) => {
const headers = await res.allHeaders()
if (headers['x-action-redirect']) {
responseCode = res.status()
}
})
},
})
await waitFor(3000) await waitFor(3000)
...@@ -606,6 +616,7 @@ createNextDescribe( ...@@ -606,6 +616,7 @@ createNextDescribe(
// no other requests should be made // no other requests should be made
expect(requests).toEqual(['/client']) expect(requests).toEqual(['/client'])
await check(() => responseCode, 303)
}) })
it('should handle regular redirects', async () => { it('should handle regular redirects', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment