Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Ensure history navigates correctly with dynamic routes + basePath (ve…
Browse files Browse the repository at this point in the history
…rcel#25459)

This ensures the `basePath` is correctly re-added to the `url` after resolving a dynamic route since the `url` stored in history is expected to already contain the `basePath`, an additional test has been added to ensure this is working correctly back/forward through history

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

Fixes: vercel#25285
  • Loading branch information
ijjk authored May 26, 2021
1 parent b054f3b commit 1b74292
Show file tree
Hide file tree
Showing 2 changed files with 29 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 @@ -993,14 +993,15 @@ export default class Router implements BaseRouter {
// if this directly matches a page we need to update the href to
// allow the correct page chunk to be loaded
pathname = rewritesResult.resolvedHref
parsed.pathname = pathname
parsed.pathname = addBasePath(pathname)
url = formatWithValidation(parsed)
}
} else {
parsed.pathname = resolveDynamicRoute(pathname, pages)

if (parsed.pathname !== pathname) {
pathname = parsed.pathname
parsed.pathname = addBasePath(pathname)
url = formatWithValidation(parsed)
}
}
Expand Down Expand Up @@ -1529,6 +1530,7 @@ export default class Router implements BaseRouter {

if (parsed.pathname !== pathname) {
pathname = parsed.pathname
parsed.pathname = pathname
url = formatWithValidation(parsed)
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/integration/basepath/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ afterAll(async () => {
})

const runTests = (dev = false) => {
it('should navigate back correctly to a dynamic route', async () => {
const browser = await webdriver(appPort, `${basePath}`)

expect(await browser.elementByCss('#index-page').text()).toContain(
'index page'
)

await browser.eval('window.beforeNav = 1')

await browser.eval('window.next.router.push("/catchall/first")')
await check(() => browser.elementByCss('p').text(), /first/)
expect(await browser.eval('window.beforeNav')).toBe(1)

await browser.eval('window.next.router.push("/catchall/second")')
await check(() => browser.elementByCss('p').text(), /second/)
expect(await browser.eval('window.beforeNav')).toBe(1)

await browser.eval('window.next.router.back()')
await check(() => browser.elementByCss('p').text(), /first/)
expect(await browser.eval('window.beforeNav')).toBe(1)

await browser.eval('window.history.forward()')
await check(() => browser.elementByCss('p').text(), /second/)
expect(await browser.eval('window.beforeNav')).toBe(1)
})

if (dev) {
describe('Hot Module Reloading', () => {
describe('delete a page and add it back', () => {
Expand Down

0 comments on commit 1b74292

Please sign in to comment.