Skip to content

Commit

Permalink
Fix: expected "catch all routes" are not matched in “parallel routes" (
Browse files Browse the repository at this point in the history
…#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
#58215

---------

Co-authored-by: Jimmy Lai <[email protected]>
  • Loading branch information
nonoakij and feedthejim authored Nov 13, 2023
1 parent dc59d3c commit 4fe968b
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/next/src/build/normalize-catchall-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ export function normalizeCatchAllRoutes(
normalizer = new AppPathnameNormalizer()
) {
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 catchAllRoute of catchAllRoutes) {
const normalizedCatchAllRoute = normalizer.normalize(catchAllRoute)
const normalizedCatchAllRouteBasePath = normalizedCatchAllRoute.slice(
0,
normalizedCatchAllRoute.indexOf('[')
normalizedCatchAllRoute.search(catchAllRouteRegex)
)

if (
Expand Down Expand Up @@ -48,6 +54,8 @@ function hasMatchedSlots(path1: string, path2: string): boolean {
return true
}

const catchAllRouteRegex = /\[?\[\.\.\./

function isCatchAllRoute(pathname: string): boolean {
return pathname.includes('[...') || pathname.includes('[[...')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'slot catchall'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Default() {
return (
<div>
<div>Default</div>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Foo() {
return 'foo id catchAll'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Foo() {
return 'foo slot'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'main catchall'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Bar() {
return 'bar'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'foo id'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'foo'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Page() {
return (
<div>
<div>Page</div>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
const browser = await next.browser('/parallel-prefetch-false')

Expand Down

0 comments on commit 4fe968b

Please sign in to comment.