Unverified Commit 050d13e0 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Make catchall parameters consistent with existing router (#38456)

Ensures catchall parameters are passed as an array to `params` instead of as a string.


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent a4668f29
...@@ -409,7 +409,11 @@ export async function renderToHTML( ...@@ -409,7 +409,11 @@ export async function renderToHTML(
const getDynamicParamFromSegment = ( const getDynamicParamFromSegment = (
// [id] or [slug] // [id] or [slug]
segment: string segment: string
): { param: string; value: string } | null => { ): {
param: string
value: string | string[] | null
treeValue: string
} | null => {
// TODO: use correct matching for dynamic routes to get segment param // TODO: use correct matching for dynamic routes to get segment param
const segmentParam = getSegmentParam(segment) const segmentParam = getSegmentParam(segment)
if (!segmentParam) { if (!segmentParam) {
...@@ -422,24 +426,20 @@ export async function renderToHTML( ...@@ -422,24 +426,20 @@ export async function renderToHTML(
if (segmentParam.type === 'optional-catchall') { if (segmentParam.type === 'optional-catchall') {
return { return {
param: key, param: key,
value: '', value: null,
treeValue: '',
} }
} }
return null return null
} }
// TODO: this should only read from `pathParams`. There's an inconsistency where `query` holds params currently which has to be fixed.
const value = pathParams[key] ?? query[key]
return { return {
param: key, param: key,
value: value: value,
// TODO: this should only read from `pathParams`. There's an inconsistency where `query` holds params currently which has to be fixed. treeValue: Array.isArray(value) ? value.join('/') : value,
(Array.isArray(pathParams[key])
? // @ts-expect-error TODO: handle case where value is an array
pathParams[key].join('/')
: pathParams[key]) ??
(Array.isArray(query[key])
? // @ts-expect-error TODO: handle case where value is an array
query[key].join('/')
: query[key]),
} }
} }
...@@ -450,7 +450,7 @@ export async function renderToHTML( ...@@ -450,7 +450,7 @@ export async function renderToHTML(
const dynamicParam = getDynamicParamFromSegment(segment) const dynamicParam = getDynamicParamFromSegment(segment)
const segmentTree: FlightRouterState = [ const segmentTree: FlightRouterState = [
dynamicParam ? [dynamicParam.param, dynamicParam.value] : segment, dynamicParam ? [dynamicParam.param, dynamicParam.treeValue] : segment,
{}, {},
] ]
...@@ -506,16 +506,16 @@ export async function renderToHTML( ...@@ -506,16 +506,16 @@ export async function renderToHTML(
: undefined : undefined
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
const currentParams =
const currentParams = segmentParam // Handle null case where dynamic param is optional
? { segmentParam && segmentParam.value !== null
...parentParams, ? {
[segmentParam.param]: segmentParam.value, ...parentParams,
} [segmentParam.param]: segmentParam.value,
: parentParams }
: parentParams
const actualSegment = segmentParam const actualSegment = segmentParam
? [segmentParam.param, segmentParam.value] ? [segmentParam.param, segmentParam.treeValue]
: segment : segment
// This happens outside of rendering in order to eagerly kick off data fetching for layouts / the page further down // This happens outside of rendering in order to eagerly kick off data fetching for layouts / the page further down
...@@ -539,7 +539,7 @@ export async function renderToHTML( ...@@ -539,7 +539,7 @@ export async function renderToHTML(
const childProp: ChildProp = { const childProp: ChildProp = {
current: <ChildComponent />, current: <ChildComponent />,
segment: childSegmentParam segment: childSegmentParam
? [childSegmentParam.param, childSegmentParam.value] ? [childSegmentParam.param, childSegmentParam.treeValue]
: parallelRoutes[currentValue][0], : parallelRoutes[currentValue][0],
} }
...@@ -669,7 +669,7 @@ export async function renderToHTML( ...@@ -669,7 +669,7 @@ export async function renderToHTML(
// TODO: throw on invalid flightRouterState // TODO: throw on invalid flightRouterState
const walkTreeWithFlightRouterState = ( const walkTreeWithFlightRouterState = (
treeToFilter: LoaderTree, treeToFilter: LoaderTree,
parentParams: { [key: string]: any }, parentParams: { [key: string]: string | string[] },
flightRouterState?: FlightRouterState, flightRouterState?: FlightRouterState,
parentRendered?: boolean parentRendered?: boolean
): FlightDataPath => { ): FlightDataPath => {
...@@ -677,17 +677,18 @@ export async function renderToHTML( ...@@ -677,17 +677,18 @@ export async function renderToHTML(
const parallelRoutesKeys = Object.keys(parallelRoutes) const parallelRoutesKeys = Object.keys(parallelRoutes)
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
const currentParams =
// Handle null case where dynamic param is optional
segmentParam && segmentParam.value !== null
? {
...parentParams,
[segmentParam.param]: segmentParam.value,
}
: parentParams
const actualSegment: Segment = segmentParam const actualSegment: Segment = segmentParam
? [segmentParam.param, segmentParam.value] ? [segmentParam.param, segmentParam.treeValue]
: segment : segment
const currentParams = segmentParam
? {
...parentParams,
[segmentParam.param]: segmentParam.value,
}
: parentParams
const renderComponentsOnThisLevel = const renderComponentsOnThisLevel =
!flightRouterState || !flightRouterState ||
!matchSegment(actualSegment, flightRouterState[0]) || !matchSegment(actualSegment, flightRouterState[0]) ||
......
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