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

Ensure history navigates correctly with dynamic routes + basePath #25459

Merged
merged 2 commits into from May 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/next-server/lib/router/router.ts
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
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