Unverified Commit 4fe968bb authored by Aki Matsumoto's avatar Aki Matsumoto Committed by GitHub

Fix: expected "catch all routes" are not matched in “parallel routes" (#58368)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?



-->
This PR fixes a bug where the expected catch-all route would not match
if there were multiple "catch-all routes" under the "parallel route”.

The page on the non-parallel routes path matches the catch all routes.
that exist on the more detailed path.
However, under parallel routes, it matches the most parent page of all
the pages with catch all routes.

For example, if there are files like below:
```
app/@sidebar/[...catchall]/page.tsx
app/@sidebar/dashboard/[...catchall]/page.tsx
```
When accessing `/foo`, it should match
`app/@sidebar/[...catchall]/page.tsx`, and this is working correctly.
However, when accessing `/dashboard/foo`, it should match
`app/@sidebar/dashboard/[...catchall]/page.tsx`, but
`app/@sidebar/[...catchall]/page.tsx` is being matched instead.

## Repository to reproduce
https://github.com/nonoakij/fix-parallel-routes-with-catch-all

## Related PR
https://github.com/vercel/next.js/pull/58215

---------
Co-authored-by: 's avatarJimmy Lai <laijimmy0@gmail.com>
parent dc59d3cc
...@@ -12,7 +12,13 @@ export function normalizeCatchAllRoutes( ...@@ -12,7 +12,13 @@ export function normalizeCatchAllRoutes(
normalizer = new AppPathnameNormalizer() normalizer = new AppPathnameNormalizer()
) { ) {
const catchAllRoutes = [ const catchAllRoutes = [
...new Set(Object.values(appPaths).flat().filter(isCatchAllRoute)), ...new Set(
Object.values(appPaths)
.flat()
.filter(isCatchAllRoute)
// Sorting is important because we want to match the most specific path.
.sort((a, b) => b.split('/').length - a.split('/').length)
),
] ]
for (const appPath of Object.keys(appPaths)) { for (const appPath of Object.keys(appPaths)) {
...@@ -20,7 +26,7 @@ export function normalizeCatchAllRoutes( ...@@ -20,7 +26,7 @@ export function normalizeCatchAllRoutes(
const normalizedCatchAllRoute = normalizer.normalize(catchAllRoute) const normalizedCatchAllRoute = normalizer.normalize(catchAllRoute)
const normalizedCatchAllRouteBasePath = normalizedCatchAllRoute.slice( const normalizedCatchAllRouteBasePath = normalizedCatchAllRoute.slice(
0, 0,
normalizedCatchAllRoute.indexOf('[') normalizedCatchAllRoute.search(catchAllRouteRegex)
) )
if ( if (
...@@ -48,6 +54,8 @@ function hasMatchedSlots(path1: string, path2: string): boolean { ...@@ -48,6 +54,8 @@ function hasMatchedSlots(path1: string, path2: string): boolean {
return true return true
} }
const catchAllRouteRegex = /\[?\[\.\.\./
function isCatchAllRoute(pathname: string): boolean { function isCatchAllRoute(pathname: string): boolean {
return pathname.includes('[...') || pathname.includes('[[...') return pathname.includes('[...') || pathname.includes('[[...')
} }
export default function Default() {
return (
<div>
<div>Default</div>
</div>
)
}
import Link from 'next/link'
export default function Layout({ children, slot }) {
return (
<div>
<div>Main content</div>
<div id="main">{children}</div>
<div>
Slot content:
<div id="slot-content">{slot}</div>
</div>
<div>
<Link href="/parallel-nested-catchall/foo">foo</Link>
</div>
<div>
<Link href="/parallel-nested-catchall/bar">catchall bar</Link>
</div>
<div>
<Link href="/parallel-nested-catchall/foo/123">catchall foo id</Link>
</div>
</div>
)
}
export default function Page() {
return (
<div>
<div>Page</div>
</div>
)
}
...@@ -339,6 +339,37 @@ createNextDescribe( ...@@ -339,6 +339,37 @@ createNextDescribe(
) )
}) })
it('Should match the catch-all routes of the more specific path, If there is more than one catch-all route', async () => {
const browser = await next.browser('/parallel-nested-catchall')
await browser
.elementByCss('[href="/parallel-nested-catchall/foo"]')
.click()
await check(() => browser.waitForElementByCss('#main').text(), 'foo')
await check(
() => browser.waitForElementByCss('#slot-content').text(),
'foo slot'
)
await browser
.elementByCss('[href="/parallel-nested-catchall/bar"]')
.click()
await check(() => browser.waitForElementByCss('#main').text(), 'bar')
await check(
() => browser.waitForElementByCss('#slot-content').text(),
'slot catchall'
)
await browser
.elementByCss('[href="/parallel-nested-catchall/foo/123"]')
.click()
await check(() => browser.waitForElementByCss('#main').text(), 'foo id')
await check(
() => browser.waitForElementByCss('#slot-content').text(),
'foo id catchAll'
)
})
it('should navigate with a link with prefetch=false', async () => { it('should navigate with a link with prefetch=false', async () => {
const browser = await next.browser('/parallel-prefetch-false') const browser = await next.browser('/parallel-prefetch-false')
......
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