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

Commit 39daddc

Browse files
Fix rewrite and dynamic params on navigating to initial history entry (vercel#25495)
* Fix rewrite and dynamic params * set _shouldResolveHref to true when initializing client state * add window.beforeNav check * fix broken path * fix build-output tests * remove yarn.lock changes
1 parent 715f883 commit 39daddc

File tree

6 files changed

+119
-2
lines changed

6 files changed

+119
-2
lines changed

packages/next/next-server/lib/router/router.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,14 @@ export default class Router implements BaseRouter {
650650
if (as.substr(0, 2) !== '//') {
651651
// in order for `e.state` to work on the `onpopstate` event
652652
// we have to register the initial route upon initialization
653+
const options: TransitionOptions = { locale }
654+
;(options as any)._shouldResolveHref = as !== pathname
655+
653656
this.changeState(
654657
'replaceState',
655658
formatWithValidation({ pathname: addBasePath(pathname), query }),
656659
getURL(),
657-
{ locale }
660+
options
658661
)
659662
}
660663

test/integration/build-output/test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('Build Output', () => {
149149
true
150150
)
151151

152-
expect(parseFloat(mainSize)).toBeCloseTo(gz ? 19.4 : 60.5, 1)
152+
expect(parseFloat(mainSize)).toBeCloseTo(gz ? 19.4 : 60.6, 1)
153153
expect(mainSize.endsWith('kB')).toBe(true)
154154

155155
expect(parseFloat(frameworkSize)).toBeCloseTo(gz ? 42.0 : 130, 1)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
rewrites() {
3+
return [
4+
{
5+
source: '/:pagePrefix/:path*',
6+
destination: '/dynamic-page/:pagePrefix/:path*',
7+
},
8+
]
9+
},
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useRouter } from 'next/router'
2+
import Link from 'next/link'
3+
4+
export default function Page(props) {
5+
const router = useRouter()
6+
7+
return (
8+
<>
9+
<p id="another">another page</p>
10+
<p id="pathname">{router.pathname}</p>
11+
<p id="query">{JSON.stringify(router.query)}</p>
12+
13+
<Link href="/" as="/">
14+
<a id="to-index">Go back to index</a>
15+
</Link>
16+
<br />
17+
</>
18+
)
19+
}
20+
21+
export const getServerSideProps = () => {
22+
return { props: {} }
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useRouter } from 'next/router'
2+
3+
export default function Page() {
4+
const router = useRouter()
5+
6+
return (
7+
<>
8+
<p id="index">index page</p>
9+
<p id="pathname">{router.pathname}</p>
10+
<p id="query">{JSON.stringify(router.query)}</p>
11+
12+
<br />
13+
</>
14+
)
15+
}
16+
17+
export const getServerSideProps = () => {
18+
return { props: {} }
19+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-env jest */
2+
3+
import { join } from 'path'
4+
import {
5+
findPort,
6+
killApp,
7+
launchApp,
8+
nextBuild,
9+
nextStart,
10+
} from 'next-test-utils'
11+
import webdriver from 'next-webdriver'
12+
13+
jest.setTimeout(1000 * 60 * 2)
14+
15+
const appDir = join(__dirname, '../')
16+
17+
let appPort
18+
let app
19+
20+
const runTests = () => {
21+
it('back-button should go back to rewritten path successfully', async () => {
22+
const browser = await webdriver(appPort, '/rewrite-me/path')
23+
24+
expect(await browser.elementByCss('#another').text()).toBe('another page')
25+
26+
await browser.eval('window.beforeNav = 1')
27+
28+
await browser
29+
.elementByCss('#to-index')
30+
.click()
31+
.waitForElementByCss('#index')
32+
33+
await browser.back()
34+
35+
expect(await browser.elementByCss('#another').text()).toBe('another page')
36+
37+
expect(await browser.eval('window.beforeNav')).toBe(1)
38+
})
39+
}
40+
41+
describe('rewrites persist with browser history actions', () => {
42+
describe('dev mode', () => {
43+
beforeAll(async () => {
44+
appPort = await findPort()
45+
app = await launchApp(appDir, appPort)
46+
})
47+
afterAll(() => killApp(app))
48+
49+
runTests()
50+
})
51+
52+
describe('production mode', () => {
53+
beforeAll(async () => {
54+
await nextBuild(appDir)
55+
appPort = await findPort()
56+
app = await nextStart(appDir, appPort)
57+
})
58+
afterAll(() => killApp(app))
59+
60+
runTests()
61+
})
62+
})

0 commit comments

Comments
 (0)