Add _rsc query param to earliest RSC request detection

The earliest RSC detection now checks both the `rsc` header AND the
`_rsc` URL search param. This handles CDN/proxy scenarios where the
`rsc` header may be stripped but the `_rsc` cache-busting param in
the URL is preserved.

Changes:
- adapter.ts: isRSCRequest is now true when either the `rsc` header is
  set or `_rsc` is present in the URL
- base-server.ts handleRSCRequest: also detects RSC via `_rsc` query
  param; sets `rsc` header for downstream compatibility when needed
- resolve-routes.ts: RSC rewrite headers are now set for requests with
  `_rsc` in the URL, even without the `rsc` header
Co-authored-by: 's avatarshuding <3676859+shuding@users.noreply.github.com>
parent 553d06af
......@@ -652,7 +652,20 @@ export default abstract class Server<
stripFlightHeaders(req.headers)
return false
} else if (req.headers[RSC_HEADER] === '1') {
} else if (
req.headers[RSC_HEADER] === '1' ||
parsedUrl.query[NEXT_RSC_UNION_QUERY] !== undefined
) {
// A request is an RSC request if the `rsc` header is set to '1', or if
// the `_rsc` search param is present in the URL. The latter handles the
// case where a CDN or proxy has stripped the `rsc` header but preserved
// the URL with the cache-busting `_rsc` search param.
if (req.headers[RSC_HEADER] !== '1') {
// The `_rsc` search param is present but the `rsc` header is missing.
// Set the `rsc` header for downstream code that checks it directly.
req.headers[RSC_HEADER] = '1'
}
addRequestMeta(req, 'isRSCRequest', true)
if (req.headers[NEXT_ROUTER_PREFETCH_HEADER] === '1') {
......
......@@ -42,6 +42,7 @@ import type { TLSSocket } from 'tls'
import {
NEXT_REWRITTEN_PATH_HEADER,
NEXT_REWRITTEN_QUERY_HEADER,
NEXT_RSC_UNION_QUERY,
RSC_HEADER,
} from '../../../client/components/app-router-headers'
......@@ -842,9 +843,14 @@ export function getResolveRoutes(
)
: false
// Set the rewrite headers only if this is a RSC request.
// Set the rewrite headers only if this is a RSC request. A request
// is considered an RSC request if the `rsc` header is set to '1',
// or if the `_rsc` search param is present in the URL. The latter
// handles the case where a CDN or proxy has stripped the `rsc`
// header but preserved the URL with the cache-busting `_rsc` param.
if (
req.headers[RSC_HEADER] === '1' &&
(req.headers[RSC_HEADER] === '1' ||
parsedUrl.query[NEXT_RSC_UNION_QUERY] !== undefined) &&
(!parsedDestination.origin || isAllowedOrigin)
) {
// We set the rewritten path and query headers on the response now
......
......@@ -150,7 +150,6 @@ export async function adapter(
const requestHeaders = fromNodeOutgoingHttpHeaders(params.request.headers)
const isNextDataRequest = requestHeaders.has('x-nextjs-data')
const isRSCRequest = requestHeaders.get(RSC_HEADER) === '1'
if (isNextDataRequest && requestURL.pathname === '/index') {
requestURL.pathname = '/'
......@@ -175,6 +174,12 @@ export async function adapter(
const rscHash = normalizeURL.searchParams.get(NEXT_RSC_UNION_QUERY)
// A request is an RSC request if the `rsc` header is set to '1', or if the
// `_rsc` search param is present in the URL. The latter handles the case
// where a CDN or proxy has stripped the `rsc` header but preserved the URL
// with the cache-busting `_rsc` search param.
const isRSCRequest = requestHeaders.get(RSC_HEADER) === '1' || rscHash !== null
const request = new NextRequestHint({
page: params.page,
// Strip internal query parameters off the request.
......
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