Skip to content

Commit 66466a5

Browse files
committed
always use 200 status for rsc redirect
1 parent fc99399 commit 66466a5

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

packages/next/src/build/templates/app-page.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const __next_app__ = {
8383
}
8484

8585
import * as entryBase from '../../server/app-render/entry-base' with { 'turbopack-transition': 'next-server-utility' }
86+
import { RedirectStatusCode } from '../../client/components/redirect-status-code'
8687

8788
export * from '../../server/app-render/entry-base' with { 'turbopack-transition': 'next-server-utility' }
8889

@@ -999,6 +1000,16 @@ export async function handler(
9991000
res.statusCode = cachedData.status
10001001
}
10011002

1003+
// Redirect information is encoded in RSC payload, so we don't need to use redirect status codes
1004+
if (
1005+
!minimalMode &&
1006+
cachedData.status &&
1007+
RedirectStatusCode[cachedData.status] &&
1008+
isRSCRequest
1009+
) {
1010+
res.statusCode = 200
1011+
}
1012+
10021013
// Mark that the request did postpone.
10031014
if (didPostpone) {
10041015
res.setHeader(NEXT_DID_POSTPONE_HEADER, '1')

packages/next/src/server/base-server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
170170
import { getCacheHandlers } from './use-cache/handlers'
171171
import { fixMojibake } from './lib/fix-mojibake'
172172
import { computeCacheBustingSearchParam } from '../shared/lib/router/utils/cache-busting-search-param'
173+
import { RedirectStatusCode } from '../client/components/redirect-status-code'
173174

174175
export type FindComponentsResult = {
175176
components: LoadComponentsReturnType
@@ -3505,6 +3506,16 @@ export default abstract class Server<
35053506
res.statusCode = cachedData.status
35063507
}
35073508

3509+
// Redirect information is encoded in RSC payload, so we don't need to use redirect status codes
3510+
if (
3511+
!this.minimalMode &&
3512+
cachedData.status &&
3513+
RedirectStatusCode[cachedData.status] &&
3514+
isRSCRequest
3515+
) {
3516+
res.statusCode = 200
3517+
}
3518+
35083519
// Mark that the request did postpone.
35093520
if (didPostpone) {
35103521
res.setHeader(NEXT_DID_POSTPONE_HEADER, '1')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
3+
export default function Page() {
4+
return (
5+
<div>
6+
<h1>Destination</h1>
7+
</div>
8+
)
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React, { ReactNode } from 'react'
2+
3+
export default function Root({ children }: { children: ReactNode }) {
4+
return (
5+
<html>
6+
<body>{children}</body>
7+
</html>
8+
)
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { redirect } from 'next/navigation'
2+
3+
export default function Page() {
4+
redirect('/dest')
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
import Link from 'next/link'
3+
4+
export default function Page() {
5+
return (
6+
<div>
7+
<h1>Home</h1>
8+
<Link href="/origin">Go to Origin</Link>
9+
</div>
10+
)
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
import { fetchViaHTTP } from 'next-test-utils'
3+
4+
describe('rsc-redirect', () => {
5+
const { next } = nextTestSetup({
6+
files: __dirname,
7+
})
8+
9+
it('should get 307 status code for document request', async () => {
10+
const response = await fetchViaHTTP(next.url, '/origin', undefined, {
11+
redirect: 'manual',
12+
})
13+
expect(response.status).toBe(307)
14+
})
15+
16+
it('should get 200 status code for rsc request', async () => {
17+
// TODO: add RSC cache busting query param
18+
const response = await fetchViaHTTP(next.url, '/origin', undefined, {
19+
redirect: 'manual',
20+
headers: {
21+
RSC: '1',
22+
},
23+
})
24+
expect(response.status).toBe(200)
25+
})
26+
})

0 commit comments

Comments
 (0)