Unverified Commit b57c8a16 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Preserve interception markers in parameter types (#85526)

### What?

This PR preserves the specific interception route markers in dynamic
parameter types throughout the routing system.

### Why?

Previously, the code only tracked whether a parameter was intercepted
(using generic types like `'dynamic-intercepted'` or
`'catchall-intercepted'`) but not which specific interception marker was
used. This meant that routing information about the interception pattern
was being lost.

Interception routes use different markers:
- `(.)` - matches segments at the same level
- `(..)` - matches segments one level up  
- `(..)(..)` - matches segments two levels up
- `(...)` - matches segments from the root app directory

Without preserving the specific marker, the routing system couldn't
properly distinguish between these different interception patterns,
which could lead to incorrect route matching behavior.

### How?

The fix encodes the specific interception marker pattern directly in the
type definition. For example:
- `'dynamic-intercepted'` → `'dynamic-intercepted-(.)'`,
`'dynamic-intercepted-(..)'`, etc.
- `'catchall-intercepted'` → `'catchall-intercepted-(.)'`,
`'catchall-intercepted-(..)'`, etc.

This change is propagated throughout the routing system:
- Type definitions in `app-router-types.ts` and `types.ts`
- Route parameter parsing in `route-params.ts`
- Static path generation in `build/static-paths/app.ts`
- Dynamic parameter utilities in `get-dynamic-param.ts` and
`get-segment-param.tsx`
- On-demand entry handling in development mode

NAR-486
parent 84de02c4
...@@ -596,7 +596,10 @@ export function resolveParallelRouteParams( ...@@ -596,7 +596,10 @@ export function resolveParallelRouteParams(
switch (paramType) { switch (paramType) {
case 'catchall': case 'catchall':
case 'optional-catchall': case 'optional-catchall':
case 'catchall-intercepted': case 'catchall-intercepted-(..)(..)':
case 'catchall-intercepted-(.)':
case 'catchall-intercepted-(..)':
case 'catchall-intercepted-(...)':
// If there are any non-parallel fallback route segments, we can't use the // If there are any non-parallel fallback route segments, we can't use the
// pathname to derive the value because it's not complete. We can make // pathname to derive the value because it's not complete. We can make
// this assumption because routes are resolved left to right. // this assumption because routes are resolved left to right.
...@@ -658,7 +661,10 @@ export function resolveParallelRouteParams( ...@@ -658,7 +661,10 @@ export function resolveParallelRouteParams(
break break
case 'dynamic': case 'dynamic':
case 'dynamic-intercepted': case 'dynamic-intercepted-(..)(..)':
case 'dynamic-intercepted-(.)':
case 'dynamic-intercepted-(..)':
case 'dynamic-intercepted-(...)':
// For regular dynamic parameters, take the segment at this depth // For regular dynamic parameters, take the segment at this depth
if (depth < pathSegments.length) { if (depth < pathSegments.length) {
const pathSegment = pathSegments[depth] const pathSegment = pathSegments[depth]
......
...@@ -6,7 +6,17 @@ export function hasInterceptionRouteInCurrentTree([ ...@@ -6,7 +6,17 @@ export function hasInterceptionRouteInCurrentTree([
parallelRoutes, parallelRoutes,
]: FlightRouterState): boolean { ]: FlightRouterState): boolean {
// If we have a dynamic segment, it's marked as an interception route by the presence of the `i` suffix. // If we have a dynamic segment, it's marked as an interception route by the presence of the `i` suffix.
if (Array.isArray(segment) && (segment[2] === 'di' || segment[2] === 'ci')) { if (
Array.isArray(segment) &&
(segment[2] === 'di(..)(..)' ||
segment[2] === 'ci(..)(..)' ||
segment[2] === 'di(.)' ||
segment[2] === 'ci(.)' ||
segment[2] === 'di(..)' ||
segment[2] === 'ci(..)' ||
segment[2] === 'di(...)' ||
segment[2] === 'ci(...)')
) {
return true return true
} }
......
...@@ -63,14 +63,29 @@ export function parseDynamicParamFromURLPart( ...@@ -63,14 +63,29 @@ export function parseDynamicParamFromURLPart(
// This needs to match the behavior in get-dynamic-param.ts. // This needs to match the behavior in get-dynamic-param.ts.
switch (paramType) { switch (paramType) {
// Catchalls // Catchalls
case 'c': case 'c': {
case 'ci': {
// Catchalls receive all the remaining URL parts. If there are no // Catchalls receive all the remaining URL parts. If there are no
// remaining pathname parts, return an empty array. // remaining pathname parts, return an empty array.
return partIndex < pathnameParts.length return partIndex < pathnameParts.length
? pathnameParts.slice(partIndex).map((s) => encodeURIComponent(s)) ? pathnameParts.slice(partIndex).map((s) => encodeURIComponent(s))
: [] : []
} }
// Catchall intercepted
case 'ci(..)(..)':
case 'ci(.)':
case 'ci(..)':
case 'ci(...)': {
const prefix = paramType.length - 2
return partIndex < pathnameParts.length
? pathnameParts.slice(partIndex).map((s, i) => {
if (i === 0) {
return encodeURIComponent(s.slice(prefix))
}
return encodeURIComponent(s)
})
: []
}
// Optional catchalls // Optional catchalls
case 'oc': { case 'oc': {
// Optional catchalls receive all the remaining URL parts, unless this is // Optional catchalls receive all the remaining URL parts, unless this is
...@@ -80,8 +95,7 @@ export function parseDynamicParamFromURLPart( ...@@ -80,8 +95,7 @@ export function parseDynamicParamFromURLPart(
: null : null
} }
// Dynamic // Dynamic
case 'd': case 'd': {
case 'di': {
if (partIndex >= pathnameParts.length) { if (partIndex >= pathnameParts.length) {
// The route tree expected there to be more parts in the URL than there // The route tree expected there to be more parts in the URL than there
// actually are. This could happen if the x-nextjs-rewritten-path header // actually are. This could happen if the x-nextjs-rewritten-path header
...@@ -94,6 +108,25 @@ export function parseDynamicParamFromURLPart( ...@@ -94,6 +108,25 @@ export function parseDynamicParamFromURLPart(
} }
return encodeURIComponent(pathnameParts[partIndex]) return encodeURIComponent(pathnameParts[partIndex])
} }
// Dynamic intercepted
case 'di(..)(..)':
case 'di(.)':
case 'di(..)':
case 'di(...)': {
const prefix = paramType.length - 2
if (partIndex >= pathnameParts.length) {
// The route tree expected there to be more parts in the URL than there
// actually are. This could happen if the x-nextjs-rewritten-path header
// is incorrectly set, or potentially due to bug in Next.js. TODO:
// Should this be a hard error? During a prefetch, we can just abort.
// During a client navigation, we could trigger a hard refresh. But if
// it happens during initial render, we don't really have any
// recovery options.
return ''
}
return encodeURIComponent(pathnameParts[partIndex].slice(prefix))
}
default: default:
paramType satisfies never paramType satisfies never
return '' return ''
......
...@@ -8,8 +8,14 @@ export const dynamicParamTypes: Record< ...@@ -8,8 +8,14 @@ export const dynamicParamTypes: Record<
DynamicParamTypesShort DynamicParamTypesShort
> = { > = {
catchall: 'c', catchall: 'c',
'catchall-intercepted': 'ci', 'catchall-intercepted-(..)(..)': 'ci(..)(..)',
'catchall-intercepted-(.)': 'ci(.)',
'catchall-intercepted-(..)': 'ci(..)',
'catchall-intercepted-(...)': 'ci(...)',
'optional-catchall': 'oc', 'optional-catchall': 'oc',
dynamic: 'd', dynamic: 'd',
'dynamic-intercepted': 'di', 'dynamic-intercepted-(..)(..)': 'di(..)(..)',
'dynamic-intercepted-(.)': 'di(.)',
'dynamic-intercepted-(..)': 'di(..)',
'dynamic-intercepted-(...)': 'di(...)',
} }
...@@ -21,7 +21,19 @@ import type { IncomingMessage } from 'http' ...@@ -21,7 +21,19 @@ import type { IncomingMessage } from 'http'
import type { RenderResumeDataCache } from '../resume-data-cache/resume-data-cache' import type { RenderResumeDataCache } from '../resume-data-cache/resume-data-cache'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator' import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
const dynamicParamTypesSchema = s.enums(['c', 'ci', 'oc', 'd', 'di']) const dynamicParamTypesSchema = s.enums([
'c',
'ci(..)(..)',
'ci(.)',
'ci(..)',
'ci(...)',
'oc',
'd',
'di(..)(..)',
'di(.)',
'di(..)',
'di(...)',
])
const segmentSchema = s.union([ const segmentSchema = s.union([
s.string(), s.string(),
......
...@@ -87,12 +87,18 @@ function convertDynamicParamTypeToSyntax( ...@@ -87,12 +87,18 @@ function convertDynamicParamTypeToSyntax(
) { ) {
switch (dynamicParamTypeShort) { switch (dynamicParamTypeShort) {
case 'c': case 'c':
case 'ci': case 'ci(..)(..)':
case 'ci(.)':
case 'ci(..)':
case 'ci(...)':
return `[...${param}]` return `[...${param}]`
case 'oc': case 'oc':
return `[[...${param}]]` return `[[...${param}]]`
case 'd': case 'd':
case 'di': case 'di(..)(..)':
case 'di(.)':
case 'di(..)':
case 'di(...)':
return `[${param}]` return `[${param}]`
default: default:
throw new Error('Unknown dynamic param type') throw new Error('Unknown dynamic param type')
......
...@@ -114,12 +114,29 @@ export type ReadyCacheNode = { ...@@ -114,12 +114,29 @@ export type ReadyCacheNode = {
export type DynamicParamTypes = export type DynamicParamTypes =
| 'catchall' | 'catchall'
| 'catchall-intercepted' | 'catchall-intercepted-(..)(..)'
| 'catchall-intercepted-(.)'
| 'catchall-intercepted-(..)'
| 'catchall-intercepted-(...)'
| 'optional-catchall' | 'optional-catchall'
| 'dynamic' | 'dynamic'
| 'dynamic-intercepted' | 'dynamic-intercepted-(..)(..)'
| 'dynamic-intercepted-(.)'
export type DynamicParamTypesShort = 'c' | 'ci' | 'oc' | 'd' | 'di' | 'dynamic-intercepted-(..)'
| 'dynamic-intercepted-(...)'
export type DynamicParamTypesShort =
| 'c'
| 'ci(..)(..)'
| 'ci(.)'
| 'ci(..)'
| 'ci(...)'
| 'oc'
| 'd'
| 'di(..)(..)'
| 'di(.)'
| 'di(..)'
| 'di(...)'
export type Segment = export type Segment =
| string | string
......
...@@ -64,12 +64,12 @@ describe('getDynamicParam', () => { ...@@ -64,12 +64,12 @@ describe('getDynamicParam', () => {
const params: Params = {} const params: Params = {}
expect(() => { expect(() => {
getDynamicParam(params, 'slug', 'di', null) getDynamicParam(params, 'slug', 'di(..)(..)', null)
}).toThrow(InvariantError) }).toThrow(InvariantError)
expect(() => { expect(() => {
getDynamicParam(params, 'slug', 'di', null) getDynamicParam(params, 'slug', 'di(..)(..)', null)
}).toThrow( }).toThrow(
'Invariant: Missing value for segment key: "slug" with dynamic param type: di. This is a bug in Next.js.' 'Invariant: Missing value for segment key: "slug" with dynamic param type: di(..)(..). This is a bug in Next.js.'
) )
}) })
}) })
...@@ -113,13 +113,13 @@ describe('getDynamicParam', () => { ...@@ -113,13 +113,13 @@ describe('getDynamicParam', () => {
it('should handle catchall intercepted (ci) with array values', () => { it('should handle catchall intercepted (ci) with array values', () => {
const params: Params = { path: ['photo', '123'] } const params: Params = { path: ['photo', '123'] }
const result = getDynamicParam(params, 'path', 'ci', null) const result = getDynamicParam(params, 'path', 'ci(..)(..)', null)
expect(result).toEqual({ expect(result).toEqual({
param: 'path', param: 'path',
value: ['photo', '123'], value: ['photo', '123'],
type: 'ci', type: 'ci(..)(..)',
treeSegment: ['path', 'photo/123', 'ci'], treeSegment: ['path', 'photo/123', 'ci(..)(..)'],
}) })
}) })
......
...@@ -70,7 +70,10 @@ export function interpolateParallelRouteParams( ...@@ -70,7 +70,10 @@ export function interpolateParallelRouteParams(
switch (segmentParam.type) { switch (segmentParam.type) {
case 'catchall': case 'catchall':
case 'optional-catchall': case 'optional-catchall':
case 'catchall-intercepted': case 'catchall-intercepted-(..)(..)':
case 'catchall-intercepted-(.)':
case 'catchall-intercepted-(..)':
case 'catchall-intercepted-(...)':
// For catchall parameters, take all remaining segments from this depth // For catchall parameters, take all remaining segments from this depth
const remainingSegments = pathSegments.slice(depth) const remainingSegments = pathSegments.slice(depth)
...@@ -92,7 +95,10 @@ export function interpolateParallelRouteParams( ...@@ -92,7 +95,10 @@ export function interpolateParallelRouteParams(
} }
break break
case 'dynamic': case 'dynamic':
case 'dynamic-intercepted': case 'dynamic-intercepted-(..)(..)':
case 'dynamic-intercepted-(.)':
case 'dynamic-intercepted-(..)':
case 'dynamic-intercepted-(...)':
// For regular dynamic parameters, take the segment at this depth // For regular dynamic parameters, take the segment at this depth
if (depth < pathSegments.length) { if (depth < pathSegments.length) {
const pathSegment = pathSegments[depth] const pathSegment = pathSegments[depth]
......
...@@ -29,14 +29,18 @@ export function getSegmentParam(segment: string): { ...@@ -29,14 +29,18 @@ export function getSegmentParam(segment: string): {
if (segment.startsWith('[...') && segment.endsWith(']')) { if (segment.startsWith('[...') && segment.endsWith(']')) {
return { return {
type: interceptionMarker ? 'catchall-intercepted' : 'catchall', type: interceptionMarker
? `catchall-intercepted-${interceptionMarker}`
: 'catchall',
param: segment.slice(4, -1), param: segment.slice(4, -1),
} }
} }
if (segment.startsWith('[') && segment.endsWith(']')) { if (segment.startsWith('[') && segment.endsWith(']')) {
return { return {
type: interceptionMarker ? 'dynamic-intercepted' : 'dynamic', type: interceptionMarker
? `dynamic-intercepted-${interceptionMarker}`
: 'dynamic',
param: segment.slice(1, -1), param: segment.slice(1, -1),
} }
} }
...@@ -46,10 +50,19 @@ export function getSegmentParam(segment: string): { ...@@ -46,10 +50,19 @@ export function getSegmentParam(segment: string): {
export function isCatchAll( export function isCatchAll(
type: DynamicParamTypes type: DynamicParamTypes
): type is 'catchall' | 'catchall-intercepted' | 'optional-catchall' { ): type is
| 'catchall'
| 'catchall-intercepted-(..)(..)'
| 'catchall-intercepted-(.)'
| 'catchall-intercepted-(..)'
| 'catchall-intercepted-(...)'
| 'optional-catchall' {
return ( return (
type === 'catchall' || type === 'catchall' ||
type === 'catchall-intercepted' || type === 'catchall-intercepted-(..)(..)' ||
type === 'catchall-intercepted-(.)' ||
type === 'catchall-intercepted-(..)' ||
type === 'catchall-intercepted-(...)' ||
type === 'optional-catchall' type === 'optional-catchall'
) )
} }
...@@ -63,7 +76,10 @@ export function getParamProperties(paramType: DynamicParamTypes): { ...@@ -63,7 +76,10 @@ export function getParamProperties(paramType: DynamicParamTypes): {
switch (paramType) { switch (paramType) {
case 'catchall': case 'catchall':
case 'catchall-intercepted': case 'catchall-intercepted-(..)(..)':
case 'catchall-intercepted-(.)':
case 'catchall-intercepted-(..)':
case 'catchall-intercepted-(...)':
repeat = true repeat = true
break break
case 'optional-catchall': case 'optional-catchall':
...@@ -71,7 +87,10 @@ export function getParamProperties(paramType: DynamicParamTypes): { ...@@ -71,7 +87,10 @@ export function getParamProperties(paramType: DynamicParamTypes): {
optional = true optional = true
break break
case 'dynamic': case 'dynamic':
case 'dynamic-intercepted': case 'dynamic-intercepted-(..)(..)':
case 'dynamic-intercepted-(.)':
case 'dynamic-intercepted-(..)':
case 'dynamic-intercepted-(...)':
break break
default: default:
paramType satisfies never paramType satisfies never
......
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