Skip to content

Commit d7636d2

Browse files
committed
overwrite redirect SSG meta.status to 200 for RSC requests
1 parent 737d319 commit d7636d2

File tree

7 files changed

+36
-5
lines changed

7 files changed

+36
-5
lines changed

packages/next/src/build/templates/pages.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'
4343
import { removeTrailingSlash } from '../../shared/lib/router/utils/remove-trailing-slash'
4444
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix'
4545
import { normalizeLocalePath } from '../../shared/lib/i18n/normalize-locale-path'
46+
import { RSC_HEADER } from '../../client/components/app-router-headers'
4647

4748
// Re-export the component (should be the default export).
4849
export default hoist(userland, 'default')
@@ -499,6 +500,10 @@ export async function handler(
499500
}
500501

501502
if (isIsrFallback) {
503+
const isRSCRequest =
504+
getRequestMeta(req, 'isRSCRequest') ??
505+
Boolean(req.headers[RSC_HEADER])
506+
502507
const fallbackResponse = await routeModule.getResponseCache(req).get(
503508
routeModule.isDev
504509
? null
@@ -509,12 +514,15 @@ export async function handler(
509514
previousCacheEntry: previousFallbackCacheEntry = null,
510515
}) => {
511516
if (!routeModule.isDev) {
512-
return toResponseCacheEntry(previousFallbackCacheEntry)
517+
return toResponseCacheEntry(previousFallbackCacheEntry, {
518+
isRscRequest: isRSCRequest,
519+
})
513520
}
514521
return doRender()
515522
},
516523
{
517524
routeKind: RouteKind.PAGES,
525+
isRscRequest: isRSCRequest,
518526
isFallback: true,
519527
isRoutePPREnabled: false,
520528
isOnDemandRevalidate: false,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,9 @@ export default abstract class Server<
30303030
// the previous fallback cache entry. This preserves the previous
30313031
// behavior.
30323032
if (isProduction) {
3033-
return toResponseCacheEntry(previousFallbackCacheEntry)
3033+
return toResponseCacheEntry(previousFallbackCacheEntry, {
3034+
isRscRequest: isRSCRequest,
3035+
})
30343036
}
30353037

30363038
// We pass `undefined` and `null` as it doesn't apply to the pages
@@ -3046,6 +3048,7 @@ export default abstract class Server<
30463048
},
30473049
{
30483050
routeKind: RouteKind.PAGES,
3051+
isRscRequest: isRSCRequest,
30493052
incrementalCache,
30503053
isRoutePPREnabled,
30513054
isFallback: true,
@@ -3080,6 +3083,7 @@ export default abstract class Server<
30803083
}),
30813084
{
30823085
routeKind: RouteKind.APP_PAGE,
3086+
isRscRequest: isRSCRequest,
30833087
incrementalCache,
30843088
isRoutePPREnabled,
30853089
isFallback: true,
@@ -3180,6 +3184,7 @@ export default abstract class Server<
31803184
// rendered and thus check isAppPath.
31813185
routeModule?.definition.kind ??
31823186
(isAppPath ? RouteKind.APP_PAGE : RouteKind.PAGES),
3187+
isRscRequest: isRSCRequest,
31833188
incrementalCache,
31843189
isOnDemandRevalidate,
31853190
isPrefetch: req.headers.purpose === 'prefetch',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ export default class NextNodeServer extends BaseServer<
10491049
},
10501050
{
10511051
routeKind: RouteKind.IMAGE,
1052+
isRscRequest: false,
10521053
incrementalCache: imageOptimizerCache,
10531054
isFallback: false,
10541055
}

packages/next/src/server/response-cache/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default class ResponseCache implements ResponseCacheBase {
5353
responseGenerator: ResponseGenerator,
5454
context: {
5555
routeKind: RouteKind
56+
isRscRequest: boolean
5657
isOnDemandRevalidate?: boolean
5758
isPrefetch?: boolean
5859
incrementalCache: IncrementalResponseCache
@@ -209,6 +210,8 @@ export default class ResponseCache implements ResponseCacheBase {
209210
}
210211
)
211212

212-
return toResponseCacheEntry(response)
213+
return toResponseCacheEntry(response, {
214+
isRscRequest: context.isRscRequest,
215+
})
213216
}
214217
}

packages/next/src/server/response-cache/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ResponseCacheBase {
1717
* provided it will test the filesystem to check.
1818
*/
1919
routeKind: RouteKind
20+
isRscRequest: boolean
2021

2122
/**
2223
* True if this is a fallback request.

packages/next/src/server/response-cache/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import RenderResult from '../render-result'
1111
import { RouteKind } from '../route-kind'
12+
import { RedirectStatusCode } from '../../client/components/redirect-status-code'
1213

1314
export async function fromResponseCacheEntry(
1415
cacheEntry: ResponseCacheEntry
@@ -39,7 +40,8 @@ export async function fromResponseCacheEntry(
3940
}
4041

4142
export async function toResponseCacheEntry(
42-
response: IncrementalResponseCacheEntry | null
43+
response: IncrementalResponseCacheEntry | null,
44+
{ isRscRequest }: { isRscRequest: boolean }
4345
): Promise<ResponseCacheEntry | null> {
4446
if (!response) return null
4547

@@ -62,7 +64,14 @@ export async function toResponseCacheEntry(
6264
html: RenderResult.fromStatic(response.value.html),
6365
rscData: response.value.rscData,
6466
headers: response.value.headers,
65-
status: response.value.status,
67+
status:
68+
// RSC redirect is embedded in the payload itself and handled by client router directly
69+
// This makes it consistent with how it's handled in minimalMode
70+
isRscRequest &&
71+
response.value.status &&
72+
RedirectStatusCode[response.value.status]
73+
? 200
74+
: response.value.status,
6675
postponed: response.value.postponed,
6776
segmentData: response.value.segmentData,
6877
} satisfies CachedAppPageValue)

packages/next/src/server/route-modules/route-module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
routerServerGlobal,
5656
type RouterServerContext,
5757
} from '../lib/router-utils/router-server-context'
58+
import { RSC_HEADER } from '../../client/components/app-router-headers'
5859

5960
/**
6061
* RouteModuleOptions is the options that are passed to the route module, other
@@ -722,7 +723,10 @@ export abstract class RouteModule<
722723
waitUntil?: (prom: Promise<any>) => void
723724
}) {
724725
const responseCache = this.getResponseCache(req)
726+
const isRSCRequest =
727+
getRequestMeta(req, 'isRSCRequest') ?? Boolean(req.headers[RSC_HEADER])
725728
const cacheEntry = await responseCache.get(cacheKey, responseGenerator, {
729+
isRscRequest: isRSCRequest,
726730
routeKind,
727731
isFallback,
728732
isRoutePPREnabled,

0 commit comments

Comments
 (0)