Skip to content

Commit

Permalink
fix interception route rewrite regex not supporting special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Apr 19, 2024
1 parent ea0f516 commit e0df3d8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
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,39 @@ 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'

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

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

await browser.refresh()

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

0 comments on commit e0df3d8

Please sign in to comment.