Unverified Commit 9c3780fb authored by Pepijn Senders's avatar Pepijn Senders Committed by GitHub

Fix rewrite and dynamic params on navigating to initial history entry (#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
parent 5476827f
......@@ -650,11 +650,14 @@ export default class Router implements BaseRouter {
if (as.substr(0, 2) !== '//') {
// in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization
const options: TransitionOptions = { locale }
;(options as any)._shouldResolveHref = as !== pathname
this.changeState(
'replaceState',
formatWithValidation({ pathname: addBasePath(pathname), query }),
getURL(),
{ locale }
options
)
}
......
......@@ -149,7 +149,7 @@ describe('Build Output', () => {
true
)
expect(parseFloat(mainSize)).toBeCloseTo(gz ? 19.4 : 60.5, 1)
expect(parseFloat(mainSize)).toBeCloseTo(gz ? 19.4 : 60.6, 1)
expect(mainSize.endsWith('kB')).toBe(true)
expect(parseFloat(frameworkSize)).toBeCloseTo(gz ? 42.0 : 130, 1)
......
module.exports = {
rewrites() {
return [
{
source: '/:pagePrefix/:path*',
destination: '/dynamic-page/:pagePrefix/:path*',
},
]
},
}
import { useRouter } from 'next/router'
import Link from 'next/link'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="another">another page</p>
<p id="pathname">{router.pathname}</p>
<p id="query">{JSON.stringify(router.query)}</p>
<Link href="/" as="/">
<a id="to-index">Go back to index</a>
</Link>
<br />
</>
)
}
export const getServerSideProps = () => {
return { props: {} }
}
import { useRouter } from 'next/router'
export default function Page() {
const router = useRouter()
return (
<>
<p id="index">index page</p>
<p id="pathname">{router.pathname}</p>
<p id="query">{JSON.stringify(router.query)}</p>
<br />
</>
)
}
export const getServerSideProps = () => {
return { props: {} }
}
/* eslint-env jest */
import { join } from 'path'
import {
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
let appPort
let app
const runTests = () => {
it('back-button should go back to rewritten path successfully', async () => {
const browser = await webdriver(appPort, '/rewrite-me/path')
expect(await browser.elementByCss('#another').text()).toBe('another page')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-index')
.click()
.waitForElementByCss('#index')
await browser.back()
expect(await browser.elementByCss('#another').text()).toBe('another page')
expect(await browser.eval('window.beforeNav')).toBe(1)
})
}
describe('rewrites persist with browser history actions', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment