Unverified Commit aa3e043b authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Add unique search query for RSC requests to be cacable on CDN (#50970)

Adding a `_rsc` query for RSC payload requests so that they can be
differentiated on resources level for CDN cache for the ones that didn't
fully respect to VARY header.

Also stripped them for node/edge servers so that they won't show up in
the url

x-ref:
https://github.com/vercel/next.js/pull/49140#issuecomment-1549405009

Closes #49140
Closes NEXT-1268
parent 109e6cb3
...@@ -14,3 +14,5 @@ export const FLIGHT_PARAMETERS = [ ...@@ -14,3 +14,5 @@ export const FLIGHT_PARAMETERS = [
[NEXT_ROUTER_STATE_TREE], [NEXT_ROUTER_STATE_TREE],
[NEXT_ROUTER_PREFETCH], [NEXT_ROUTER_PREFETCH],
] as const ] as const
export const NEXT_RSC_UNION_QUERY = '_rsc' as const
...@@ -49,6 +49,7 @@ import { RedirectBoundary } from './redirect-boundary' ...@@ -49,6 +49,7 @@ import { RedirectBoundary } from './redirect-boundary'
import { NotFoundBoundary } from './not-found-boundary' import { NotFoundBoundary } from './not-found-boundary'
import { findHeadInCache } from './router-reducer/reducers/find-head-in-cache' import { findHeadInCache } from './router-reducer/reducers/find-head-in-cache'
import { createInfinitePromise } from './infinite-promise' import { createInfinitePromise } from './infinite-promise'
import { NEXT_RSC_UNION_QUERY } from './app-router-headers'
const isServer = typeof window === 'undefined' const isServer = typeof window === 'undefined'
...@@ -65,7 +66,7 @@ export function getServerActionDispatcher() { ...@@ -65,7 +66,7 @@ export function getServerActionDispatcher() {
export function urlToUrlWithoutFlightMarker(url: string): URL { export function urlToUrlWithoutFlightMarker(url: string): URL {
const urlWithoutFlightParameters = new URL(url, location.origin) const urlWithoutFlightParameters = new URL(url, location.origin)
// TODO-APP: handle .rsc for static export case urlWithoutFlightParameters.searchParams.delete(NEXT_RSC_UNION_QUERY)
return urlWithoutFlightParameters return urlWithoutFlightParameters
} }
......
...@@ -10,6 +10,7 @@ import type { ...@@ -10,6 +10,7 @@ import type {
import { import {
NEXT_ROUTER_PREFETCH, NEXT_ROUTER_PREFETCH,
NEXT_ROUTER_STATE_TREE, NEXT_ROUTER_STATE_TREE,
NEXT_RSC_UNION_QUERY,
NEXT_URL, NEXT_URL,
RSC, RSC,
RSC_CONTENT_TYPE_HEADER, RSC_CONTENT_TYPE_HEADER,
...@@ -17,6 +18,7 @@ import { ...@@ -17,6 +18,7 @@ import {
import { urlToUrlWithoutFlightMarker } from '../app-router' import { urlToUrlWithoutFlightMarker } from '../app-router'
import { callServer } from '../../app-call-server' import { callServer } from '../../app-call-server'
import { PrefetchKind } from './router-reducer-types' import { PrefetchKind } from './router-reducer-types'
import { hexHash } from '../../../shared/lib/hash'
/** /**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side. * Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
...@@ -56,11 +58,17 @@ export async function fetchServerResponse( ...@@ -56,11 +58,17 @@ export async function fetchServerResponse(
headers[NEXT_URL] = nextUrl headers[NEXT_URL] = nextUrl
} }
const uniqueCacheQuery = hexHash(
[
headers[NEXT_ROUTER_PREFETCH] || '0',
headers[NEXT_ROUTER_STATE_TREE],
].join(',')
)
try { try {
let fetchUrl = url let fetchUrl = new URL(url)
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
if (process.env.__NEXT_CONFIG_OUTPUT === 'export') { if (process.env.__NEXT_CONFIG_OUTPUT === 'export') {
fetchUrl = new URL(url) // clone
if (fetchUrl.pathname.endsWith('/')) { if (fetchUrl.pathname.endsWith('/')) {
fetchUrl.pathname += 'index.txt' fetchUrl.pathname += 'index.txt'
} else { } else {
...@@ -68,6 +76,10 @@ export async function fetchServerResponse( ...@@ -68,6 +76,10 @@ export async function fetchServerResponse(
} }
} }
} }
// Add unique cache query to avoid caching conflicts on CDN which don't respect to Vary header
fetchUrl.searchParams.set(NEXT_RSC_UNION_QUERY, uniqueCacheQuery)
const res = await fetch(fetchUrl, { const res = await fetch(fetchUrl, {
// Backwards compat for older browsers. `same-origin` is the default in modern browsers. // Backwards compat for older browsers. `same-origin` is the default in modern browsers.
credentials: 'same-origin', credentials: 'same-origin',
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
} from '../router-reducer-types' } from '../router-reducer-types'
import { createRecordFromThenable } from '../create-record-from-thenable' import { createRecordFromThenable } from '../create-record-from-thenable'
import { prunePrefetchCache } from './prune-prefetch-cache' import { prunePrefetchCache } from './prune-prefetch-cache'
import { NEXT_RSC_UNION_QUERY } from '../../app-router-headers'
export function prefetchReducer( export function prefetchReducer(
state: ReadonlyReducerState, state: ReadonlyReducerState,
...@@ -17,6 +18,8 @@ export function prefetchReducer( ...@@ -17,6 +18,8 @@ export function prefetchReducer(
prunePrefetchCache(state.prefetchCache) prunePrefetchCache(state.prefetchCache)
const { url } = action const { url } = action
url.searchParams.delete(NEXT_RSC_UNION_QUERY)
const href = createHrefFromUrl( const href = createHrefFromUrl(
url, url,
// Ensures the hash is not part of the cache key as it does not affect fetching the server // Ensures the hash is not part of the cache key as it does not affect fetching the server
......
...@@ -81,6 +81,7 @@ import { ...@@ -81,6 +81,7 @@ import {
RSC, RSC,
RSC_VARY_HEADER, RSC_VARY_HEADER,
FLIGHT_PARAMETERS, FLIGHT_PARAMETERS,
NEXT_RSC_UNION_QUERY,
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
import { import {
MatchOptions, MatchOptions,
...@@ -2218,6 +2219,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2218,6 +2219,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const { res, query, pathname } = ctx const { res, query, pathname } = ctx
let page = pathname let page = pathname
const bubbleNoFallback = !!query._nextBubbleNoFallback const bubbleNoFallback = !!query._nextBubbleNoFallback
delete query[NEXT_RSC_UNION_QUERY]
delete query._nextBubbleNoFallback delete query._nextBubbleNoFallback
const options: MatchOptions = { const options: MatchOptions = {
......
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'
import type { NextParsedUrlQuery } from './request-meta' import type { NextParsedUrlQuery } from './request-meta'
const INTERNAL_QUERY_NAMES = [ const INTERNAL_QUERY_NAMES = [
...@@ -6,9 +7,10 @@ const INTERNAL_QUERY_NAMES = [ ...@@ -6,9 +7,10 @@ const INTERNAL_QUERY_NAMES = [
'__nextInferredLocaleFromDefault', '__nextInferredLocaleFromDefault',
'__nextDefaultLocale', '__nextDefaultLocale',
'__nextIsNotFound', '__nextIsNotFound',
NEXT_RSC_UNION_QUERY,
] as const ] as const
const EXTENDED_INTERNAL_QUERY_NAMES = ['__nextDataReq'] as const const EDGE_EXTENDED_INTERNAL_QUERY_NAMES = ['__nextDataReq'] as const
export function stripInternalQueries(query: NextParsedUrlQuery) { export function stripInternalQueries(query: NextParsedUrlQuery) {
for (const name of INTERNAL_QUERY_NAMES) { for (const name of INTERNAL_QUERY_NAMES) {
...@@ -16,19 +18,21 @@ export function stripInternalQueries(query: NextParsedUrlQuery) { ...@@ -16,19 +18,21 @@ export function stripInternalQueries(query: NextParsedUrlQuery) {
} }
} }
export function stripInternalSearchParams( export function stripInternalSearchParams<T extends string | URL>(
searchParams: URLSearchParams, url: T,
extended?: boolean isEdge: boolean
) { ): T {
const isStringUrl = typeof url === 'string'
const instance = isStringUrl ? new URL(url) : (url as URL)
for (const name of INTERNAL_QUERY_NAMES) { for (const name of INTERNAL_QUERY_NAMES) {
searchParams.delete(name) instance.searchParams.delete(name)
} }
if (extended) { if (isEdge) {
for (const name of EXTENDED_INTERNAL_QUERY_NAMES) { for (const name of EDGE_EXTENDED_INTERNAL_QUERY_NAMES) {
searchParams.delete(name) instance.searchParams.delete(name)
} }
} }
return searchParams return (isStringUrl ? instance.toString() : instance) as T
} }
...@@ -110,6 +110,7 @@ import { invokeRequest } from './lib/server-ipc/invoke-request' ...@@ -110,6 +110,7 @@ import { invokeRequest } from './lib/server-ipc/invoke-request'
import { filterReqHeaders } from './lib/server-ipc/utils' import { filterReqHeaders } from './lib/server-ipc/utils'
import { createRequestResponseMocks } from './lib/mock-request' import { createRequestResponseMocks } from './lib/mock-request'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'
export * from './base-server' export * from './base-server'
...@@ -1560,6 +1561,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1560,6 +1561,7 @@ export default class NextNodeServer extends BaseServer {
return { finished: true } return { finished: true }
} }
delete query._nextBubbleNoFallback delete query._nextBubbleNoFallback
delete query[NEXT_RSC_UNION_QUERY]
const handledAsEdgeFunction = await this.runEdgeFunction({ const handledAsEdgeFunction = await this.runEdgeFunction({
req, req,
......
...@@ -5,6 +5,7 @@ import type { UrlWithParsedQuery } from 'url' ...@@ -5,6 +5,7 @@ import type { UrlWithParsedQuery } from 'url'
import type { BaseNextRequest } from './base-http' import type { BaseNextRequest } from './base-http'
import type { CloneableBody } from './body-streams' import type { CloneableBody } from './body-streams'
import { RouteMatch } from './future/route-matches/route-match' import { RouteMatch } from './future/route-matches/route-match'
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'
// FIXME: (wyattjoh) this is a temporary solution to allow us to pass data between bundled modules // FIXME: (wyattjoh) this is a temporary solution to allow us to pass data between bundled modules
export const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta') export const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta')
...@@ -97,6 +98,7 @@ type NextQueryMetadata = { ...@@ -97,6 +98,7 @@ type NextQueryMetadata = {
_nextBubbleNoFallback?: '1' _nextBubbleNoFallback?: '1'
__nextDataReq?: '1' __nextDataReq?: '1'
__nextCustomErrorRender?: '1' __nextCustomErrorRender?: '1'
[NEXT_RSC_UNION_QUERY]?: string
} }
export type NextParsedUrlQuery = ParsedUrlQuery & export type NextParsedUrlQuery = ParsedUrlQuery &
......
...@@ -117,14 +117,14 @@ export async function adapter( ...@@ -117,14 +117,14 @@ export async function adapter(
} }
} }
// Strip internal query parameters off the request. const normalizeUrl = process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
stripInternalSearchParams(requestUrl.searchParams, true) ? new URL(params.request.url)
: requestUrl
const request = new NextRequestHint({ const request = new NextRequestHint({
page: params.page, page: params.page,
input: process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE // Strip internal query parameters off the request.
? params.request.url input: stripInternalSearchParams(normalizeUrl, true).toString(),
: String(requestUrl),
init: { init: {
body: params.request.body, body: params.request.body,
geo: params.request.geo, geo: params.request.geo,
......
...@@ -4,6 +4,7 @@ import { getModuleContext } from './context' ...@@ -4,6 +4,7 @@ import { getModuleContext } from './context'
import { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin' import { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin'
import { requestToBodyStream } from '../../body-streams' import { requestToBodyStream } from '../../body-streams'
import type { EdgeRuntime } from 'next/dist/compiled/edge-runtime' import type { EdgeRuntime } from 'next/dist/compiled/edge-runtime'
import { NEXT_RSC_UNION_QUERY } from '../../../client/components/app-router-headers'
export const ErrorSource = Symbol('SandboxError') export const ErrorSource = Symbol('SandboxError')
...@@ -96,6 +97,10 @@ export const run = withTaggedErrors(async function runWithTaggedErrors(params) { ...@@ -96,6 +97,10 @@ export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
: undefined : undefined
const KUint8Array = runtime.evaluate('Uint8Array') const KUint8Array = runtime.evaluate('Uint8Array')
const urlInstance = new URL(params.request.url)
urlInstance.searchParams.delete(NEXT_RSC_UNION_QUERY)
params.request.url = urlInstance.toString()
try { try {
const result = await edgeFunction({ const result = await edgeFunction({
......
...@@ -7,3 +7,7 @@ export function djb2Hash(str: string) { ...@@ -7,3 +7,7 @@ export function djb2Hash(str: string) {
} }
return Math.abs(hash) return Math.abs(hash)
} }
export function hexHash(str: string) {
return djb2Hash(str).toString(16).slice(0, 7)
}
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { check, waitFor } from 'next-test-utils' import { check, waitFor } from 'next-test-utils'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
const browserConfigWithFixedTime = { const browserConfigWithFixedTime = {
beforePageLoad: (page) => { beforePageLoad: (page) => {
page.addInitScript(() => { page.addInitScript(() => {
...@@ -38,6 +41,10 @@ createNextDescribe( ...@@ -38,6 +41,10 @@ createNextDescribe(
return return
} }
it('NEXT_RSC_UNION_QUERY query name is _rsc', async () => {
expect(NEXT_RSC_UNION_QUERY).toBe('_rsc')
})
it('should show layout eagerly when prefetched with loading one level down', async () => { it('should show layout eagerly when prefetched with loading one level down', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime) const browser = await next.browser('/', browserConfigWithFixedTime)
// Ensure the page is prefetched // Ensure the page is prefetched
...@@ -85,8 +92,12 @@ createNextDescribe( ...@@ -85,8 +92,12 @@ createNextDescribe(
await browser.eval( await browser.eval(
'window.nd.router.prefetch("/static-page", {kind: "auto"})' 'window.nd.router.prefetch("/static-page", {kind: "auto"})'
) )
await check(() => { await check(() => {
return requests.some((req) => req.includes('static-page')) return requests.some(
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success' ? 'success'
: JSON.stringify(requests) : JSON.stringify(requests)
}, 'success') }, 'success')
...@@ -114,7 +125,10 @@ createNextDescribe( ...@@ -114,7 +125,10 @@ createNextDescribe(
`window.nd.router.prefetch("/static-page", {kind: "auto"})` `window.nd.router.prefetch("/static-page", {kind: "auto"})`
) )
await check(() => { await check(() => {
return requests.some((req) => req.includes('static-page')) return requests.some(
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success' ? 'success'
: JSON.stringify(requests) : JSON.stringify(requests)
}, 'success') }, 'success')
...@@ -136,7 +150,10 @@ createNextDescribe( ...@@ -136,7 +150,10 @@ createNextDescribe(
.waitForElementByCss('#static-page') .waitForElementByCss('#static-page')
expect( expect(
requests.filter((request) => request === '/static-page').length requests.filter(
(request) =>
request === '/static-page' || request.includes(NEXT_RSC_UNION_QUERY)
).length
).toBe(1) ).toBe(1)
}) })
...@@ -159,7 +176,11 @@ createNextDescribe( ...@@ -159,7 +176,11 @@ createNextDescribe(
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
await waitFor(500) await waitFor(500)
expect( expect(
requests.filter((request) => request === '/static-page').length requests.filter(
(request) =>
request === '/static-page' ||
request.includes(NEXT_RSC_UNION_QUERY)
).length
).toBe(0) ).toBe(0)
} }
}) })
......
import { strict as assert } from 'node:assert'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
export default function Page({ searchParams }) {
assert(searchParams[NEXT_RSC_UNION_QUERY] === undefined)
return <p>no rsc query page</p>
}
import { strict as assert } from 'node:assert'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
export function GET(request) {
assert(request.nextUrl.searchParams.get(NEXT_RSC_UNION_QUERY) === null)
return new Response('no rsc query route')
}
// @ts-check // @ts-check
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
if (NEXT_RSC_UNION_QUERY !== '_rsc') {
throw new Error(`NEXT_RSC_UNION_QUERY should be _rsc`)
}
/** /**
* @param {import('next/server').NextRequest} request * @param {import('next/server').NextRequest} request
* @returns {NextResponse | undefined} * @returns {NextResponse | undefined}
*/ */
export function middleware(request) { export function middleware(request) {
const rscQuery = request.nextUrl.searchParams.get(NEXT_RSC_UNION_QUERY)
// Test that the RSC query is not present in the middleware
if (rscQuery) {
throw new Error('RSC query should not be present in the middleware')
}
if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') { if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
return NextResponse.redirect(new URL('/redirect-dest', request.url)) return NextResponse.redirect(new URL('/redirect-dest', request.url))
} }
......
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