Skip to content

Commit

Permalink
Add support for catchall with new router (#38439)
Browse files Browse the repository at this point in the history
Adds support for `[...slug]` dynamic segments.

I found there's an inconsistency in query/params providing and added a quick fix for it now. Will update the handling in a follow-up PR to ensure it's consistently providing dynamic params outside of `query`.

Follow-up of #37551 and #38415.


## 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)
  • Loading branch information
timneutkens committed Jul 8, 2022
1 parent 6d8b2cd commit 41bedf6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/next/server/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,24 @@ export async function renderToHTML(
// TODO: use correct matching for dynamic routes to get segment param
const segmentParam =
segment.startsWith('[') && segment.endsWith(']')
? segment.slice(1, -1)
? segment.slice(segment.startsWith('[...') ? 4 : 1, -1)
: null

if (!segmentParam || !pathParams[segmentParam]) {
if (!segmentParam || (!pathParams[segmentParam] && !query[segmentParam])) {
return null
}

// @ts-expect-error TODO: handle case where value is an array
return { param: segmentParam, value: pathParams[segmentParam] }
return {
param: segmentParam,
// @ts-expect-error TODO: handle case where value is an array
value:
// TODO: this should only read from `pathParams`. There's an inconsistency where `query` holds params currently which has to be fixed.
pathParams[segmentParam] ??
(Array.isArray(query[segmentParam])
? // @ts-expect-error TODO: handle case where value is an array
query[segmentParam].join('/')
: query[segmentParam]),
}
}

const createFlightRouterStateFromLoaderTree = ([
Expand Down Expand Up @@ -546,10 +555,9 @@ export async function renderToHTML(
| GetServerSidePropsContext
| getServerSidePropsContextPage = {
headers,
// TODO: convert to NextCookies
cookies,
layoutSegments: segmentPath,
// TODO: change this to be URLSearchParams instead?
// TODO: Currently query holds params and pathname is not the actual pathname, it holds the dynamic parameter
...(isPage ? { query, pathname } : {}),
...(pageIsDynamic ? { params: currentParams } : undefined),
...(isPreview
Expand Down

0 comments on commit 41bedf6

Please sign in to comment.