Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix interception route rewrite regex not supporting hyphenated segments #64805

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import type { Rewrite } from './load-custom-routes'
// a function that converts normalised paths (e.g. /foo/[bar]/[baz]) to the format expected by pathToRegexp (e.g. /foo/:bar/:baz)
function toPathToRegexpPath(path: string): string {
return path.replace(/\[\[?([^\]]+)\]\]?/g, (_, capture) => {
// path-to-regexp only supports word characters, so we replace any non-word characters with underscores
const paramName = capture.replace(/\W+/g, '_')

// handle catch-all segments (e.g. /foo/bar/[...baz] or /foo/bar/[[...baz]])
if (capture.startsWith('...')) {
return `:${capture.slice(3)}*`
if (paramName.startsWith('...')) {
return `:${paramName.slice(3)}*`
}
return ':' + capture
return ':' + paramName
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page({ params }) {
return (
<div>
Hello from [this-is-my-route]/@intercept/some-page. Param:{' '}
{params['this-is-my-route']}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function Layout({
children,
intercept,
}: {
children: React.ReactNode
intercept: React.ReactNode
}) {
return (
<div>
<div>{children}</div>
<div>{intercept}</div>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Link from 'next/link'

export default function Page() {
return (
<Link href="/interception-route-special-params/some-random-param/some-page">
Trigger Interception
</Link>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page({ params }) {
return (
<div>
Hello from [this-is-my-route]/some-page. Param:{' '}
{params['this-is-my-route']}
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,43 @@ createNextDescribe(
await check(() => browser.waitForElementByCss('#main-slot').text(), '1')
})

it('should intercept on routes that contain hyphenated/special dynamic params', async () => {
const browser = await next.browser(
'/interception-route-special-params/some-random-param'
)

await browser
.elementByCss(
"[href='/interception-route-special-params/some-random-param/some-page']"
)
.click()

const interceptionText =
'Hello from [this-is-my-route]/@intercept/some-page. Param: some-random-param'
const pageText =
'Hello from [this-is-my-route]/some-page. Param: some-random-param'

await retry(async () => {
expect(await browser.elementByCss('body').text()).toContain(
interceptionText
)

expect(await browser.elementByCss('body').text()).not.toContain(
pageText
)
})

await browser.refresh()

await retry(async () => {
expect(await browser.elementByCss('body').text()).toContain(pageText)

expect(await browser.elementByCss('body').text()).not.toContain(
interceptionText
)
})
})

if (isNextStart) {
it('should not have /default paths in the prerender manifest', async () => {
const prerenderManifest = JSON.parse(
Expand Down
Loading