Unverified Commit 30c64ba0 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Fix interception route rewrites for nested dynamic routes (#84413)

### What?

Fixes interception routes with dynamic parameters that were generating
rewrite rules in an incorrect format, preventing proper URL rewriting by
Vercel and Next.js.

### Why?

The failing test
`parallel-routes-and-interception-nested-dynamic-routes` revealed that
interception routes with dynamic segments weren't working correctly when
deployed. The root cause was that the previous implementation using
`safePathToRegexp` produced rewrite destinations that weren't in a
format compatible with direct parameter substitution during the rewrite
process.

When the router tried to rewrite URLs to their intercepted destinations,
the parameter format prevented Vercel/Next.js from properly substituting
the captured values, causing routes to fail.

### How?

Completely rewrote the `generate-interception-routes-rewrites` logic to
use `getNamedRouteRegex` instead of `safePathToRegexp`. This ensures:

1. **Correct rewrite format**: Rewrites are now generated in a format
that allows direct parameter substitution
2. **Parameter consistency**: Source regex is generated first, then its
parameter reference is passed to destination generation to ensure
parameter names match for substitution
3. **Clean URLs**: Added `stripNormalizedSeparators` to prevent
`_NEXTSEP_` internal separator from leaking into client-facing URLs
4. **Stricter matching**: Updated router reducers to pass `next-url`
header for the stricter Next-Url regex matching required by the new
rewrite mechanism

**Testing:**
- Added 23 comprehensive tests for rewrite generation covering all
interception marker types
- Added 18 tests for route matching utilities
- All existing tests pass including the previously failing
`parallel-routes-and-interception-nested-dynamic-routes`

Fixes the failing test:
`test/e2e/app-dir/parallel-routes-and-interception-nested-dynamic-routes/parallel-routes-and-interception-nested-dynamic-routes.test.ts`

NAR-432
parent ec560204
......@@ -865,5 +865,7 @@
"864": "Missing value for segment key: \"%s\" with dynamic param type: %s",
"865": "`experimental.rdcForNavigations` is enabled, but `experimental.cacheComponents` is not.",
"866": "Both \"%s\" and \"%s\" files are detected. Please use \"%s\" instead.",
"867": "The %s \"%s\" must export a %s or a \\`default\\` function"
"867": "The %s \"%s\" must export a %s or a \\`default\\` function",
"868": "No reference found for param: %s in reference: %s",
"869": "No reference found for segment: %s with reference: %s"
}
......@@ -396,7 +396,7 @@ function Router({
}
}, [])
const { cache, tree, nextUrl, focusAndScrollRef } = state
const { cache, tree, nextUrl, focusAndScrollRef, previousNextUrl } = state
const matchingHead = useMemo(() => {
return findHeadInCache(cache, tree[1])
......@@ -423,8 +423,9 @@ function Router({
tree,
focusAndScrollRef,
nextUrl,
previousNextUrl,
}
}, [tree, focusAndScrollRef, nextUrl])
}, [tree, focusAndScrollRef, nextUrl, previousNextUrl])
let head
if (matchingHead !== null) {
......
......@@ -389,7 +389,14 @@ function InnerLayoutRouter({
new URL(url, location.origin),
{
flightRouterState: refetchTree,
nextUrl: includeNextUrl ? context.nextUrl : null,
nextUrl: includeNextUrl
? // We always send the last next-url, not the current when
// performing a dynamic request. This is because we update
// the next-url after a navigation, but we want the same
// interception route to be matched that used the last
// next-url.
context.previousNextUrl || context.nextUrl
: null,
}
).then((serverResponse) => {
startTransition(() => {
......
......@@ -138,6 +138,7 @@ describe('createInitialRouterState', () => {
},
cache: expectedCache,
nextUrl: '/linking',
previousNextUrl: null,
}
expect(state).toMatchObject(expected)
......
......@@ -110,6 +110,7 @@ export function createInitialRouterState({
// the || operator is intentional, the pathname can be an empty string
(extractPathFromFlightRouterState(initialTree) || location?.pathname) ??
null,
previousNextUrl: null,
}
if (process.env.NODE_ENV !== 'development' && location) {
......
......@@ -16,6 +16,7 @@ export function handleMutable(
// shouldScroll is true by default, can override to false.
const shouldScroll = mutable.shouldScroll ?? true
let previousNextUrl = state.previousNextUrl
let nextUrl = state.nextUrl
if (isNotUndefined(mutable.patchedTree)) {
......@@ -23,6 +24,7 @@ export function handleMutable(
const changedPath = computeChangedPath(state.tree, mutable.patchedTree)
if (changedPath) {
// If the tree changed, we need to update the nextUrl
previousNextUrl = nextUrl
nextUrl = changedPath
} else if (!nextUrl) {
// if the tree ends up being the same (ie, no changed path), and we don't have a nextUrl, then we should use the canonicalUrl
......@@ -84,5 +86,6 @@ export function handleMutable(
? mutable.patchedTree
: state.tree,
nextUrl,
previousNextUrl: previousNextUrl,
}
}
......@@ -391,7 +391,12 @@ export function navigateReducer(
new URL(updatedCanonicalUrl, url.origin),
{
flightRouterState: dynamicRequestTree,
nextUrl: state.nextUrl,
// We always send the last next-url, not the current when
// performing a dynamic request. This is because we update
// the next-url after a navigation, but we want the same
// interception route to be matched that used the last
// next-url.
nextUrl: state.previousNextUrl || state.nextUrl,
}
)
......
......@@ -45,5 +45,6 @@ export function restoreReducer(
// Restore provided tree
tree: treeToRestore,
nextUrl: extractPathFromFlightRouterState(treeToRestore) ?? url.pathname,
previousNextUrl: null,
}
}
......@@ -257,8 +257,14 @@ export function serverActionReducer(
// Otherwise the server action might be intercepted with the wrong action id
// (ie, one that corresponds with the intercepted route)
const nextUrl =
state.nextUrl && hasInterceptionRouteInCurrentTree(state.tree)
? state.nextUrl
// We always send the last next-url, not the current when
// performing a dynamic request. This is because we update
// the next-url after a navigation, but we want the same
// interception route to be matched that used the last
// next-url.
(state.previousNextUrl || state.nextUrl) &&
hasInterceptionRouteInCurrentTree(state.tree)
? state.previousNextUrl || state.nextUrl
: null
const navigatedAt = Date.now()
......
......@@ -254,6 +254,11 @@ export type AppRouterState = {
* The underlying "url" representing the UI state, which is used for intercepting routes.
*/
nextUrl: string | null
/**
* The previous next-url that was used previous to a dynamic navigation.
*/
previousNextUrl: string | null
}
export type ReadonlyReducerState = Readonly<AppRouterState>
......
......@@ -45,7 +45,19 @@ export function buildCustomRoute(
)
}
const regex = normalizeRouteRegex(source)
// If this is an internal rewrite and it already provides a regex, use it
// otherwise, normalize the source to a regex.
let regex: string
if (
!route.internal ||
type !== 'rewrite' ||
!('regex' in route) ||
typeof route.regex !== 'string'
) {
regex = normalizeRouteRegex(source)
} else {
regex = route.regex
}
if (type !== 'redirect') {
return { ...route, regex }
......
......@@ -31,6 +31,11 @@ export type Rewrite = {
* @internal - used internally for routing
*/
internal?: boolean
/**
* @internal - used internally for routing
*/
regex?: string
}
export type Header = {
......
......@@ -4,7 +4,7 @@ import type { NextConfigComplete } from '../../config-shared'
import type { RenderServer, initialize } from '../router-server'
import type { PatchMatcher } from '../../../shared/lib/router/utils/path-match'
import type { Redirect } from '../../../types'
import type { Header, Rewrite } from '../../../lib/load-custom-routes'
import type { Header } from '../../../lib/load-custom-routes'
import type { UnwrapPromise } from '../../../lib/coalesced-function'
import type { NextUrlWithParsedQuery } from '../../request-meta'
......@@ -42,12 +42,8 @@ import type { TLSSocket } from 'tls'
import {
NEXT_REWRITTEN_PATH_HEADER,
NEXT_REWRITTEN_QUERY_HEADER,
NEXT_ROUTER_STATE_TREE_HEADER,
RSC_HEADER,
} from '../../../client/components/app-router-headers'
import { getSelectedParams } from '../../../client/components/router-reducer/compute-changed-path'
import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites'
import { parseAndValidateFlightRouterState } from '../../app-render/parse-and-validate-flight-router-state'
const debug = setupDebug('next:router-server:resolve-routes')
......@@ -775,27 +771,6 @@ export function getResolveRoutes(
if (route.destination) {
let rewriteParams = params
try {
// An interception rewrite might reference a dynamic param for a route the user
// is currently on, which wouldn't be extractable from the matched route params.
// This attempts to extract the dynamic params from the provided router state.
if (isInterceptionRouteRewrite(route as Rewrite)) {
const stateHeader = req.headers[NEXT_ROUTER_STATE_TREE_HEADER]
if (stateHeader) {
rewriteParams = {
...getSelectedParams(
parseAndValidateFlightRouterState(stateHeader)
),
...params,
}
}
}
} catch (err) {
// this is a no-op -- we couldn't extract dynamic params from the provided router state,
// so we'll just use the params from the route matcher
}
const { parsedDestination } = prepareDestination({
appendParamsToQuery: true,
destination: route.destination,
......
......@@ -27,10 +27,6 @@ import { decodeQueryPathParameter } from './lib/decode-query-path-parameter'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { parseReqUrl } from '../lib/url'
import { formatUrl } from '../shared/lib/router/utils/format-url'
import { parseAndValidateFlightRouterState } from './app-render/parse-and-validate-flight-router-state'
import { isInterceptionRouteRewrite } from '../lib/generate-interception-routes-rewrites'
import { NEXT_ROUTER_STATE_TREE_HEADER } from '../client/components/app-router-headers'
import { getSelectedParams } from '../client/components/router-reducer/compute-changed-path'
function filterInternalQuery(
query: Record<string, undefined | string | string[]>,
......@@ -266,27 +262,6 @@ export function getServerUtils({
}
if (params) {
try {
// An interception rewrite might reference a dynamic param for a route the user
// is currently on, which wouldn't be extractable from the matched route params.
// This attempts to extract the dynamic params from the provided router state.
if (isInterceptionRouteRewrite(rewrite as Rewrite)) {
const stateHeader = req.headers[NEXT_ROUTER_STATE_TREE_HEADER]
if (stateHeader) {
params = {
...getSelectedParams(
parseAndValidateFlightRouterState(stateHeader)
),
...params,
}
}
}
} catch (err) {
// this is a no-op -- we couldn't extract dynamic params from the provided router state,
// so we'll just use the params from the route matcher
}
const { parsedDestination, destQuery } = prepareDestination({
appendParamsToQuery: true,
destination: rewrite.destination,
......@@ -303,20 +278,6 @@ export function getServerUtils({
Object.assign(rewrittenParsedUrl.query, parsedDestination.query)
delete (parsedDestination as any).query
// for each property in rewrittenParsedUrl.query, if the value is parametrized (eg :foo), look up the value
// in rewriteParams and replace the parametrized value with the actual value
// this is used when the rewrite destination does not contain the original source param
// and so the value is still parametrized and needs to be replaced with the actual rewrite param
Object.entries(rewrittenParsedUrl.query).forEach(([key, value]) => {
if (value && typeof value === 'string' && value.startsWith(':')) {
const paramName = value.slice(1)
const actualValue = rewriteParams[paramName]
if (actualValue) {
rewrittenParsedUrl.query[key] = actualValue
}
}
})
Object.assign(rewrittenParsedUrl, parsedDestination)
fsPathname = rewrittenParsedUrl.pathname
......
......@@ -68,6 +68,7 @@ export const GlobalLayoutRouterContext = React.createContext<{
tree: FlightRouterState
focusAndScrollRef: FocusAndScrollRef
nextUrl: string | null
previousNextUrl: string | null
}>(null as any)
export const TemplateContext = React.createContext<React.ReactNode>(null as any)
......
......@@ -43,3 +43,13 @@ export function getSegmentParam(segment: string): {
return null
}
export function isCatchAll(
type: DynamicParamTypes
): type is 'catchall' | 'catchall-intercepted' | 'optional-catchall' {
return (
type === 'catchall' ||
type === 'catchall-intercepted' ||
type === 'optional-catchall'
)
}
......@@ -52,6 +52,13 @@ type GetNamedRouteRegexOptions = {
* the routes-manifest during the build.
*/
backreferenceDuplicateKeys?: boolean
/**
* If provided, this will be used as the reference for the dynamic parameter
* keys instead of generating them in context. This is currently only used for
* interception routes.
*/
reference?: Record<string, string>
}
type GetRouteRegexOptions = {
......@@ -242,9 +249,15 @@ function getSafeKeyFromSegment({
pattern = `(?<${cleanedKey}>[^/]+?)`
}
return optional
? `(?:/${interceptionPrefix}${pattern})?`
: `/${interceptionPrefix}${pattern}`
return {
key,
pattern: optional
? `(?:/${interceptionPrefix}${pattern})?`
: `/${interceptionPrefix}${pattern}`,
cleanedKey: cleanedKey,
optional,
repeat,
}
}
function getNamedParametrizedRoute(
......@@ -252,12 +265,18 @@ function getNamedParametrizedRoute(
prefixRouteKeys: boolean,
includeSuffix: boolean,
includePrefix: boolean,
backreferenceDuplicateKeys: boolean
backreferenceDuplicateKeys: boolean,
reference: Record<string, string> = {}
) {
const getSafeRouteKey = buildGetSafeRouteKey()
const routeKeys: { [named: string]: string } = {}
const segments: string[] = []
const inverseParts: string[] = []
// Ensure we don't mutate the original reference object.
reference = structuredClone(reference)
for (const segment of removeTrailingSlash(route).slice(1).split('/')) {
const hasInterceptionMarker = INTERCEPTION_ROUTE_MARKERS.some((m) =>
segment.startsWith(m)
......@@ -267,7 +286,7 @@ function getNamedParametrizedRoute(
if (hasInterceptionMarker && paramMatches && paramMatches[2]) {
// If there's an interception marker, add it to the segments.
segments.push(
const { key, pattern, cleanedKey, repeat, optional } =
getSafeKeyFromSegment({
getSafeRouteKey,
interceptionMarker: paramMatches[1],
......@@ -278,40 +297,56 @@ function getNamedParametrizedRoute(
: undefined,
backreferenceDuplicateKeys,
})
segments.push(pattern)
inverseParts.push(
`/${paramMatches[1]}:${reference[key] ?? cleanedKey}${repeat ? (optional ? '*' : '+') : ''}`
)
reference[key] ??= cleanedKey
} else if (paramMatches && paramMatches[2]) {
// If there's a prefix, add it to the segments if it's enabled.
if (includePrefix && paramMatches[1]) {
segments.push(`/${escapeStringRegexp(paramMatches[1])}`)
inverseParts.push(`/${paramMatches[1]}`)
}
let s = getSafeKeyFromSegment({
getSafeRouteKey,
segment: paramMatches[2],
routeKeys,
keyPrefix: prefixRouteKeys ? NEXT_QUERY_PARAM_PREFIX : undefined,
backreferenceDuplicateKeys,
})
const { key, pattern, cleanedKey, repeat, optional } =
getSafeKeyFromSegment({
getSafeRouteKey,
segment: paramMatches[2],
routeKeys,
keyPrefix: prefixRouteKeys ? NEXT_QUERY_PARAM_PREFIX : undefined,
backreferenceDuplicateKeys,
})
// Remove the leading slash if includePrefix already added it.
let s = pattern
if (includePrefix && paramMatches[1]) {
s = s.substring(1)
}
segments.push(s)
inverseParts.push(
`/:${reference[key] ?? cleanedKey}${repeat ? (optional ? '*' : '+') : ''}`
)
reference[key] ??= cleanedKey
} else {
segments.push(`/${escapeStringRegexp(segment)}`)
inverseParts.push(`/${segment}`)
}
// If there's a suffix, add it to the segments if it's enabled.
if (includeSuffix && paramMatches && paramMatches[3]) {
segments.push(escapeStringRegexp(paramMatches[3]))
inverseParts.push(paramMatches[3])
}
}
return {
namedParameterizedRoute: segments.join(''),
routeKeys,
pathToRegexpPattern: inverseParts.join(''),
reference,
}
}
......@@ -332,7 +367,8 @@ export function getNamedRouteRegex(
options.prefixRouteKeys,
options.includeSuffix ?? false,
options.includePrefix ?? false,
options.backreferenceDuplicateKeys ?? false
options.backreferenceDuplicateKeys ?? false,
options.reference
)
let namedRegex = result.namedParameterizedRoute
......@@ -344,6 +380,8 @@ export function getNamedRouteRegex(
...getRouteRegex(normalizedRoute, options),
namedRegex: `^${namedRegex}$`,
routeKeys: result.routeKeys,
pathToRegexpPattern: result.pathToRegexpPattern,
reference: result.reference,
}
}
......@@ -375,7 +413,8 @@ export function getNamedMiddlewareRegex(
false,
false,
false,
false
false,
undefined
)
let catchAllGroupedRegex = catchAll ? '(?:(/.*)?)' : ''
return {
......
......@@ -83,10 +83,7 @@ describe('parallel-route-navigations', () => {
// If it's PPR or Cache Components, we'll see an immediate transition for
// the client component.
if (
process.env.__NEXT_EXPERIMENTAL_PPR === 'true' ||
process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS === 'true'
) {
if (process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS === 'true') {
await browser.waitForElementByCss(
'[data-file="/[teamID]/sub/other-folder/page.tsx"]'
)
......@@ -106,19 +103,9 @@ describe('parallel-route-navigations', () => {
// Wait for the dynamic RSC request to lock, then unlock that request.
await stable(async () => {
if (
process.env.__NEXT_EXPERIMENTAL_PPR === 'true' ||
process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS === 'true' ||
process.env.__NEXT_EXPERIMENTAL_CLIENT_SEGMENT_CACHE === 'true'
) {
// When we're in PPR or Cache Components, we'll see one dynamic
// request.
expect(hadLocked).toBe(1)
} else {
// When we're not in PPR or Cache Components, we'll see two dynamic
// requests. One made to refetch each of the slots.
expect(hadLocked).toBe(2)
}
// As Client Segment Cache is the default, we should see one dynamic
// request.
expect(hadLocked).toBe(1)
})
unlock()
......
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