Skip to content

Commit

Permalink
Fix dynamic route match and basePath (vercel#14036)
Browse files Browse the repository at this point in the history
This correctly strips the `basePath` before generating the `route-matcher` for dynamic routes and adds regression tests to ensure these work correctly with the `basePath` feature

Closes: vercel#13966
  • Loading branch information
ijjk authored and darshkpatel committed Jun 12, 2020
1 parent 5dc4b29 commit 95915d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ export default class Router implements BaseRouter {
if (isDynamicRoute(route)) {
const { pathname: asPathname } = parse(as)
const routeRegex = getRouteRegex(route)
const routeMatch = getRouteMatcher(routeRegex)(asPathname)
const routeMatch = getRouteMatcher(routeRegex)(
delBasePath(asPathname || '')
)
if (!routeMatch) {
const missingParams = Object.keys(routeRegex.groups).filter(
(param) => !query[param]
Expand Down
3 changes: 3 additions & 0 deletions test/integration/basepath/pages/[slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useRouter } from 'next/router'

export default () => <p>slug: {useRouter().query.slug}</p>
3 changes: 3 additions & 0 deletions test/integration/basepath/pages/catchall/[...parts].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useRouter } from 'next/router'

export default () => <p>parts: {useRouter().query.parts?.join('/')}</p>
12 changes: 12 additions & 0 deletions test/integration/basepath/pages/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export default () => (
<h1>getServerSideProps</h1>
</a>
</Link>
<br />
<Link href="/[slug]" as="/first">
<a id="dynamic-link">
<h1>dynamic page</h1>
</a>
</Link>
<br />
<Link href="/catchall/[...parts]" as="/catchall/hello/world">
<a id="catchall-link">
<h1>catchall page</h1>
</a>
</Link>
<div id="base-path">{useRouter().basePath}</div>
<div id="pathname">{useRouter().pathname}</div>
</>
Expand Down
18 changes: 18 additions & 0 deletions test/integration/basepath/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ const runTests = (context, dev = false) => {
await check(() => browser.elementByCss('p').text(), /hello from another/)
})

it('should work with normal dynamic page', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.elementByCss('#dynamic-link').click()
await check(
() => browser.eval(() => document.documentElement.innerHTML),
/slug: first/
)
})

it('should work with catch-all page', async () => {
const browser = await webdriver(context.appPort, '/docs/hello')
await browser.elementByCss('#catchall-link').click()
await check(
() => browser.eval(() => document.documentElement.innerHTML),
/parts: hello\/world/
)
})

it('should 404 when manually adding basePath with <Link>', async () => {
const browser = await webdriver(
context.appPort,
Expand Down

0 comments on commit 95915d7

Please sign in to comment.