Unverified Commit cae50238 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

fix: resolve fallback params in shared navigation responses

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent d50d2ce8
......@@ -502,6 +502,7 @@ async function navigateToUnknownRoute(
const {
transportData,
canonicalUrl,
renderedPathname,
renderedSearch,
couldBeIntercepted,
supportsPerSegmentPrefetching,
......@@ -525,9 +526,9 @@ async function navigateToUnknownRoute(
// their own, buffered, payloads).
null,
isResponsePartial,
// Navigation responses always include the param values in the tree, so
// there's no pathname to parse them from (nor a need to).
null,
// A response rendered from a fallback shell carries opaque placeholders
// for unknown params, which are parsed from the rendered pathname.
renderedPathname,
renderedSearch,
null,
dynamicStaleTime
......@@ -560,6 +561,7 @@ async function navigateToUnknownRoute(
isResponsePartial,
responseChunks,
currentFlightRouterState,
renderedPathname,
renderedSearch,
map
).catch(() => {
......
......@@ -1767,9 +1767,10 @@ async function fetchMissingDynamicData(
// can't be drained here; they decode as null.
null,
result.isResponsePartial,
// Navigation responses always include the param values in the tree, so
// there's no pathname to parse them from (nor a need to).
null,
// A response rendered from a fallback shell carries opaque
// placeholders for unknown params, which are parsed from the rendered
// pathname.
result.renderedPathname,
result.renderedSearch,
null,
result.dynamicStaleTime
......@@ -1789,6 +1790,7 @@ async function fetchMissingDynamicData(
result.isResponsePartial,
result.responseChunks,
dynamicRequestTree,
result.renderedPathname,
result.renderedSearch,
map
).catch(() => {
......
......@@ -148,6 +148,7 @@ export function createInitialRouterState({
initialRuntimeDataAccessed === undefined,
initialRSCPayloadChunks,
initialTree,
null, // renderedPathname — the initial payload has param values
initialRenderedSearch,
segmentCacheMap // hydration writes are bound to the shared map
).catch(() => {
......
......@@ -30,8 +30,15 @@ import { findSourceMapURL } from '../../app-find-source-map-url'
import { prepareFlightRouterStateForRequest } from '../../flight-data-helpers'
import type { PartialTransportData } from '../../../shared/lib/rsc-transport'
import { setCacheBustingSearchParam } from './set-cache-busting-search-param'
import { urlToUrlWithoutFlightMarker } from '../../route-params'
import type { NormalizedSearch } from '../segment-cache/cache-key'
import {
getPathnameFromRequestURL,
getRenderedPathname,
urlToUrlWithoutFlightMarker,
} from '../../route-params'
import type {
NormalizedPathname,
NormalizedSearch,
} from '../segment-cache/cache-key'
import { getDeploymentId } from '../../../shared/lib/deployment-id'
import { getNavigationBuildId } from '../../navigation-build-id'
import { NEXT_NAV_DEPLOYMENT_ID_HEADER } from '../../../lib/constants'
......@@ -60,6 +67,12 @@ export interface FetchServerResponseOptions {
type SpaFetchServerResponseResult = {
transportData: PartialTransportData | null
canonicalUrl: URL
/**
* The pathname the response was rendered for. Used to resolve dynamic
* segments without a concrete param value: a response rendered from a
* fallback shell carries opaque placeholders for unknown params.
*/
renderedPathname: NormalizedPathname
renderedSearch: NormalizedSearch
couldBeIntercepted: boolean
supportsPerSegmentPrefetching: boolean
......@@ -276,6 +289,13 @@ export async function fetchServerResponse(
return {
transportData: flightResponse.t ?? null,
canonicalUrl: canonicalUrl,
// In output: "export" mode the response URL has the `.txt` suffix
// appended, so derive the pathname from the page URL instead (rewrites
// don't exist on a static host).
renderedPathname:
process.env.__NEXT_CONFIG_OUTPUT === 'export'
? getPathnameFromRequestURL(originalUrl)
: getRenderedPathname(res),
// TODO: We should be able to read this from the rewrite header, not the
// Flight response. Theoretically they should always agree, but there are
// currently some cases where it's incorrect for interception routes. We
......
......@@ -2729,9 +2729,14 @@ export async function fetchSegmentPrefetchesUsingRuntimeRequest(
shellResponse,
dynamicRequestTree,
predictedFrom,
// Navigation responses always include the param values in the tree, so
// there's no pathname to parse them from (nor a need to).
null,
// A response rendered from a fallback shell carries opaque
// placeholders for unknown params, which are parsed from the rendered
// pathname. In output: "export" mode the response URL has the `.txt`
// suffix appended, so derive the pathname from the page URL (rewrites
// don't exist there).
isOutputExportMode
? getPathnameFromRequestURL(url)
: getRenderedPathname(response),
renderedSearch,
buildId,
staleAt,
......@@ -3113,8 +3118,10 @@ function writeServerResponseIntoCache(
// of whether the prediction matched.
predictedFrom: KnownRoutePart | null,
// The pathname the response was rendered for, used to resolve dynamic
// segments the server sent without a param value (`k: null`). Null for
// responses that always carry concrete values (navigation responses).
// segments the server sent without a param value (`k: null`, per-segment
// prefetch responses) or with an opaque fallback placeholder (responses
// rendered from a fallback shell). Null for responses that always carry
// concrete values (the initial payload and server action responses).
renderedPathname: string | null,
renderedSearch: string,
buildId: string | undefined,
......@@ -3929,6 +3936,11 @@ export async function bufferAndWriteNavigationResponse(
// them. Null otherwise.
responseChunks: Array<Uint8Array> | null,
baseTree: FlightRouterState,
// The pathname the response was rendered for, used to resolve dynamic
// segments that have no concrete param value (see
// writeServerResponseIntoCache). Null when the response always carries
// concrete values (the initial payload).
renderedPathname: string | null,
renderedSearch: string,
// The map the work that spawned this response's request is bound to. See
// writeServerResponseIntoCache.
......@@ -3994,9 +4006,7 @@ export async function bufferAndWriteNavigationResponse(
// The base tree is the navigation's current tree, not a prediction;
// divergence from it carries no signal.
null,
// Navigation responses always include the param values in the tree, so
// there's no pathname to parse them from (nor a need to).
null,
renderedPathname,
renderedSearch,
// A navigation to a different build is an MPA navigation, so there's no
// build to check.
......
import type {
PartialTransportNode,
TransportSegment,
} from '../../../shared/lib/rsc-transport'
import type { DynamicParamTypesShort } from '../../../shared/lib/app-router-types'
import { createOpaqueFallbackParamValue } from '../../../shared/lib/router/utils/opaque-fallback-param'
import { decodeTransportTreeIntoRouteTree } from './decode-server-response'
import type { NormalizedSearch } from './cache-key'
import type { RouteTree } from './cache'
function param(
name: string,
type: DynamicParamTypesShort,
key: string | null
): TransportSegment {
return { n: name, t: type, k: key, s: null }
}
function node(
segment: TransportSegment,
child?: PartialTransportNode
): PartialTransportNode {
return child === undefined
? { s: segment }
: { s: segment, c: new Map([['children', child]]) }
}
// Builds `/(group)/items/<param>/__PAGE__` under the root layout.
function routeWith(segment: TransportSegment): PartialTransportNode {
return node(
'',
node('(group)', node('items', node(segment, node('__PAGE__'))))
)
}
function decodedParamSegment(
transportTree: PartialTransportNode,
renderedPathname: string | null
) {
const tree = decodeTransportTreeIntoRouteTree(
transportTree,
null,
null,
false,
renderedPathname,
'' as NormalizedSearch,
{ metadataVaryPath: null, treeDivergedFromBase: false }
)
let current: RouteTree<unknown> = tree
for (let depth = 0; depth < 3; depth++) {
current = current.slots!.get('children')!
}
return current.segment
}
describe('decodeTransportTreeIntoRouteTree', () => {
const placeholder = (name: string) =>
createOpaqueFallbackParamValue(name, 'abc123')
it('resolves an opaque dynamic param key from the rendered pathname', () => {
expect(
decodedParamSegment(
routeWith(param('itemId', 'd', placeholder('itemId'))),
'/items/expected%20id'
)
).toEqual(['itemId', 'expected%20id', 'd', null])
})
it('resolves an opaque catch-all param key from the rendered pathname', () => {
expect(
decodedParamSegment(
routeWith(param('slug', 'c', placeholder('slug'))),
'/items/a/b'
)
).toEqual(['slug', 'a/b', 'c', null])
})
it('resolves an opaque optional catch-all param key without parts', () => {
expect(
decodedParamSegment(
routeWith(param('slug', 'oc', placeholder('slug'))),
'/items'
)
).toEqual(['slug', '', 'oc', null])
})
it('keeps concrete param keys', () => {
expect(
decodedParamSegment(
routeWith(param('itemId', 'd', 'concrete')),
'/items/other'
)
).toEqual(['itemId', 'concrete', 'd', null])
})
it('keeps an opaque key when there is no rendered pathname', () => {
expect(
decodedParamSegment(
routeWith(param('itemId', 'd', placeholder('itemId'))),
null
)
).toEqual(['itemId', placeholder('itemId'), 'd', null])
})
})
......@@ -33,6 +33,7 @@ import {
PAGE_SEGMENT_KEY,
} from '../../../shared/lib/segment'
import { InvariantError } from '../../../shared/lib/invariant-error'
import { isOpaqueFallbackParamKey } from '../../../shared/lib/router/utils/opaque-fallback-param'
import {
doesStaticSegmentAppearInURL,
getCacheKeyForDynamicParam,
......@@ -120,9 +121,10 @@ export function createNavigationSeed(
// written to the cache may pass the conservative value (true).
isResponsePartial: boolean,
// The pathname the response was rendered for. Required to resolve dynamic
// segments the server sent without a param value (`k: null`); see
// decodeTransportTreeIntoRouteTree. Callers whose responses always carry
// concrete values (navigation responses) may pass null.
// segments the server sent without a param value (`k: null`) or with an
// opaque fallback placeholder; see decodeTransportTreeIntoRouteTree.
// Callers whose responses always carry concrete values (the initial
// payload, server action responses) may pass null.
renderedPathname: string | null,
// Already normalized by the response reader (see getRenderedSearch); the
// router state stores it as a plain string, so it is re-branded here.
......@@ -334,8 +336,10 @@ export function decodeTransportTreeIntoRouteTree(
// Required to resolve dynamic segments the server sent without a param
// value (`k: null` — per-segment prefetch responses omit the value to stay
// cacheable across param values); the client parses the value from the
// pathname instead. Callers whose responses always carry concrete values
// (navigation responses) may pass null.
// pathname instead. The same applies to opaque fallback placeholder keys
// (see resolveTransportSegment). Callers whose responses always carry
// concrete values (the initial payload, server action responses) may pass
// null.
renderedPathname: string | null,
renderedSearch: NormalizedSearch,
acc: RouteTreeAccumulator
......@@ -365,6 +369,11 @@ export function decodeTransportTreeIntoRouteTree(
* URL position this segment occupies (tracked by the tree walk: incremented
* only for segments that appear in the URL, so route groups and other
* virtual segments don't consume a part).
*
* A key that is an opaque fallback placeholder is resolved the same way. A
* response rendered from a fallback shell carries placeholders for the params
* the shell doesn't know, and a platform may serve that response for every
* param value (e.g. when it strips the param before invoking the function).
*/
function resolveTransportSegment(
transportSegment: TransportSegment,
......@@ -375,7 +384,12 @@ function resolveTransportSegment(
return transportSegment
}
const paramKey = transportSegment.k
if (paramKey !== null) {
if (
paramKey !== null &&
// Without a pathname, keep the placeholder rather than throwing: the
// callers that pass none decode responses with concrete values.
(pathnameParts === null || !isOpaqueFallbackParamKey(paramKey))
) {
return [
transportSegment.n,
paramKey,
......
......@@ -5,6 +5,7 @@ import { parseNormalizedAppRoute } from '../../shared/lib/router/routes/app'
import { extractPathnameRouteParamSegmentsFromLoaderTree } from '../../build/static-paths/app/extract-pathname-route-param-segments-from-loader-tree'
import { getParamProperties } from '../../shared/lib/router/utils/get-segment-param'
import { InvariantError } from '../../shared/lib/invariant-error'
import { createOpaqueFallbackParamValue } from '../../shared/lib/router/utils/opaque-fallback-param'
/**
* Maps unknown param names to internal placeholders. The parameter wrappers
......@@ -34,7 +35,7 @@ export function createOpaqueFallbackRouteParams(
// Generate a unique key for the fallback route param, if this key is found
// in the static output, it represents a bug in cache components.
for (const { paramName } of fallbackRouteParams) {
keys.set(paramName, `%%drp:${paramName}:${uniqueID}%%`)
keys.set(paramName, createOpaqueFallbackParamValue(paramName, uniqueID))
}
return keys
......
import {
createOpaqueFallbackParamValue,
isOpaqueFallbackParamKey,
} from './opaque-fallback-param'
describe('opaque fallback params', () => {
it('recognizes created placeholders', () => {
const value = createOpaqueFallbackParamValue('itemId', 'abc123')
expect(value).toBe('%%drp:itemId:abc123%%')
expect(isOpaqueFallbackParamKey(value)).toBe(true)
})
it('does not recognize concrete param keys', () => {
for (const value of [
'expected-id',
'%%drp:itemId:abc123%%',
'50%',
'a/b',
'%%drp',
]) {
// Concrete keys are encoded with encodeURIComponent on the server.
expect(isOpaqueFallbackParamKey(encodeURIComponent(value))).toBe(false)
}
expect(isOpaqueFallbackParamKey('')).toBe(false)
})
})
// Fallback shells are rendered without concrete values for some route params.
// Those params get an opaque placeholder value, which also becomes the param's
// key in the rendered router tree. This module owns the placeholder format so
// the client can recognize such keys.
const OPAQUE_FALLBACK_PARAM_PREFIX = '%%drp:'
const OPAQUE_FALLBACK_PARAM_SUFFIX = '%%'
/**
* Creates the opaque placeholder value for a fallback route param.
*/
export function createOpaqueFallbackParamValue(
paramName: string,
uniqueID: string
): string {
return `${OPAQUE_FALLBACK_PARAM_PREFIX}${paramName}:${uniqueID}${OPAQUE_FALLBACK_PARAM_SUFFIX}`
}
/**
* Whether a router tree param key is an opaque fallback placeholder rather
* than a concrete param value.
*
* This can't match a concrete value. The server encodes concrete values with
* `encodeURIComponent`, which never produces `%%`: every `%` in its output
* starts a percent-encoded byte (two hex digits).
*/
export function isOpaqueFallbackParamKey(key: string): boolean {
return (
key.startsWith(OPAQUE_FALLBACK_PARAM_PREFIX) &&
key.endsWith(OPAQUE_FALLBACK_PARAM_SUFFIX)
)
}
'use client'
import Link from 'next/link'
import { useParams } from 'next/navigation'
export default function ItemPage() {
const { itemId } = useParams<{ itemId: string }>()
return (
<>
<p id="item-id">{itemId}</p>
<Link id="next-item" href="/items/second%20id" prefetch={false}>
Next item
</Link>
</>
)
}
import { Suspense, type ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>
<Suspense fallback={<p>Loading item...</p>}>{children}</Suspense>
</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<Link href="/items/expected-id" prefetch={false}>
Open item
</Link>
)
}
import { isNextStart, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import type * as Playwright from 'playwright'
// Regression test for the `basepath-fallback-params` client-navigation failure
// on the legacy (non-adapter) Vercel deploy target, where `useParams()`
// rendered an opaque `%%drp:itemId:<hash>%%` placeholder.
//
// Evidence from the failing deployment:
// - A full navigation to `/dashboard/items/expected-id` was served from
// `x-matched-path: /items/[itemId].rsc`, and its router tree had
// `k: "%%drp:itemId:<hash>%%"`.
// - The fallback shell has no postponed state. For such a shell, the legacy
// builder configures the navigation data route with `allowQuery: []`.
//
// When deployed, this performs the navigations for real. With `next start`, it
// emulates that legacy serving path in minimal mode: it renders the param-free
// route-template request and serves the result to real concrete client
// navigations. The platform-to-function request is an inference from the
// builder configuration, not a captured request.
//
// The client resolves the response's opaque placeholder keys from the
// navigated URL, so one shared response serves every param value.
describe('basepath-fallback-params-minimal-mode', () => {
const { next } = nextTestSetup({
files: __dirname,
env: isNextStart
? {
// Keep `x-matched-path`, which the platform sets, on the request.
NEXT_PRIVATE_TEST_HEADERS: '1',
NEXT_PRIVATE_MINIMAL_MODE: '1',
}
: {},
})
if (isNextStart) {
it('does not generate postponed state for the fallback shell', async () => {
const metadata = await next.readJSON(
'.next/server/app/items/[itemId].meta'
)
expect(metadata.postponed).toBeUndefined()
})
}
it('renders the concrete param after navigations without prefetching', async () => {
let sharedResponseCount = 0
const browser = await next.browser('/dashboard', {
async beforePageLoad(page: Playwright.Page) {
if (!isNextStart) {
return
}
await page.route('**/dashboard/items/*?**', async (route) => {
const headers = route.request().headers()
if (headers.rsc !== '1' || headers['next-router-prefetch']) {
await route.continue()
return
}
// Serve the shared fallback data route the legacy builder selects:
// the param is stripped, so the function renders the route template.
sharedResponseCount++
const response = await next.fetch('/dashboard/items/[itemId]', {
headers: { 'x-matched-path': '/items/[itemId]', rsc: '1' },
})
const responseHeaders = Object.fromEntries(response.headers)
delete responseHeaders['content-encoding']
delete responseHeaders['content-length']
await route.fulfill({
status: response.status,
headers: responseHeaders,
body: Buffer.from(await response.arrayBuffer()),
})
})
},
})
await browser.elementByCss('a').click()
expect(await browser.elementById('item-id').text()).toBe('expected-id')
// A second param value is served from the same shared response. The value
// stays URL-encoded, like after any client navigation in this route.
await browser.elementById('next-item').click()
await retry(async () => {
expect(await browser.elementById('item-id').text()).toBe('second%20id')
})
expect(sharedResponseCount).toBe(isNextStart ? 2 : 0)
})
})
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
basePath: '/dashboard',
cacheComponents: true,
}
export default nextConfig
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