Unverified Commit 504a52aa authored by Xavier Coppens's avatar Xavier Coppens Committed by GitHub

Fixed interception on a catch-all route (#72902)

## For Contributors
### Fixing a bug

Fixes #72523 
Related to b914ad81
Related to #51787 

## For Maintainers
- Fixes catch-all segments in intercepted routes.

### What?
The regex replacement, also replaces `...` with `_`. Which causes the
check below, which handles catch-all segments, to always return false.
```
const paramName = capture.replace(/\W+/g, '_');
```

### Why?
Currently when you have an intercepted route with a catch-all segment,
it only matches the first segment. As soon as you navigate to
`/multiple/segments` the intercept fails because the rewrite is not
capturing this correctly.

### How?
The changes introduced in b914ad81 added a `replace(/\W+/g, '_')` which
causes the check below to always fail, since if it started with `...`
that would at that point be replaced with `_`.

### Todo
- [ ] write tests to verify the return value of `rewrites` has `:path*`
instead of `:path`

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent b6be5323
...@@ -13,8 +13,8 @@ function toPathToRegexpPath(path: string): string { ...@@ -13,8 +13,8 @@ function toPathToRegexpPath(path: string): string {
const paramName = capture.replace(/\W+/g, '_') const paramName = capture.replace(/\W+/g, '_')
// handle catch-all segments (e.g. /foo/bar/[...baz] or /foo/bar/[[...baz]]) // handle catch-all segments (e.g. /foo/bar/[...baz] or /foo/bar/[[...baz]])
if (paramName.startsWith('...')) { if (capture.startsWith('...')) {
return `:${paramName.slice(3)}*` return `:${capture.slice(3)}*`
} }
return ':' + paramName return ':' + paramName
}) })
......
import Link from 'next/link'
import React from 'react'
async function Page({ params }: { params: Promise<{ catchAll: string[] }> }) {
const { catchAll } = await params
return (
<div className="text-lg">
Showcase Simple Page
<Link href={`/templates/${catchAll.join('/')}`}>
templates {catchAll.join(' -> ')}
</Link>
</div>
)
}
export default Page
export default function RootLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function HomePage() {
return <Link href="/templates/multi/slug">Go to test page</Link>
}
import Link from 'next/link'
import React from 'react'
async function Page() {
return (
<div id="intercepting-page">
Showcase Intercepting Page
<Link href="/templates/single">Single</Link>
</div>
)
}
export default Page
import Link from 'next/link'
import React from 'react'
async function Page({ params }: { params: Promise<{ catchAll: string[] }> }) {
const { catchAll } = await params
return (
<div>
Simple Page
<Link href={`/showcase/${catchAll.join('/')}`}>
To Same Path <span className="opacity-70">{catchAll.join(' -> ')}</span>
</Link>
<Link href="/showcase/single">To Single Path</Link>
<Link href="/showcase/another/slug">multi-slug</Link>
</div>
)
}
export default Page
import Link from 'next/link'
import React from 'react'
function Page() {
return (
<div>
Simple Page
<Link href={'/showcase/new'}>showcase/new</Link>
</div>
)
}
export default Page
import { nextTestSetup } from 'e2e-utils'
describe('interception-routes-multiple-catchall', () => {
const { next } = nextTestSetup({
files: __dirname,
})
describe('multi-param catch-all', () => {
it('should intercept when navigating to the same path with route interception', async () => {
const browser = await next.browser('/templates/multi/slug')
await browser.elementByCss("[href='/showcase/multi/slug']").click()
await browser.waitForElementByCss('#intercepting-page')
})
it('should intercept when navigating to a single param path', async () => {
const browser = await next.browser('/templates/multi/slug')
await browser.elementByCss("[href='/showcase/single']").click()
await browser.waitForElementByCss('#intercepting-page')
})
it('should intercept when navigating to a multi-param path', async () => {
const browser = await next.browser('/templates/multi/slug')
await browser.elementByCss("[href='/showcase/another/slug']").click()
await browser.waitForElementByCss('#intercepting-page')
})
})
describe('single param catch-all', () => {
it('should intercept when navigating to a multi-param path', async () => {
const browser = await next.browser('/templates/single')
await browser.elementByCss("[href='/showcase/another/slug']").click()
await browser.waitForElementByCss('#intercepting-page')
})
it('should intercept when navigating to a single param path', async () => {
const browser = await next.browser('/templates/single')
await browser.elementByCss("[href='/showcase/single']").click()
await browser.waitForElementByCss('#intercepting-page')
})
})
})
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