提交 154d7846 编写于 作者: J JJ Kasper 提交者: Joe Haddad

Replace historyId with initial page tracking (#9380)

* Replace historyId with initial page tracking

* Apply suggestions from code review

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

* Apply suggestions from code review
上级 788654e2
......@@ -71,7 +71,7 @@ export default class Router implements BaseRouter {
_bps: BeforePopStateCallback | undefined
events: MittEmitter
_wrapApp: (App: ComponentType) => any
historyId: number
isSsr: boolean
static events: MittEmitter = mitt()
......@@ -127,8 +127,9 @@ export default class Router implements BaseRouter {
this.sub = subscription
this.clc = null
this._wrapApp = wrapApp
// we use a historyId to enable ignoring invalid popstates
this.historyId = Math.random()
// make sure to ignore extra popState in safari on navigating
// back from external site
this.isSsr = true
if (typeof window !== 'undefined') {
// in order for `e.state` to work on the `onpopstate` event
......@@ -170,7 +171,12 @@ export default class Router implements BaseRouter {
// Make sure we don't re-render on initial load,
// can be caused by navigating back from an external site
if (e.state.options && e.state.options.historyId !== this.historyId) {
if (
e.state &&
this.isSsr &&
e.state.url === this.pathname &&
e.state.as === this.asPath
) {
return
}
......@@ -245,6 +251,9 @@ export default class Router implements BaseRouter {
change(method: string, _url: Url, _as: Url, options: any): Promise<boolean> {
return new Promise((resolve, reject) => {
if (!options._h) {
this.isSsr = false
}
// marking route changes as a navigation start entry
if (SUPPORTS_PERFORMANCE_USER_TIMING) {
performance.mark('routeChange')
......@@ -380,10 +389,7 @@ export default class Router implements BaseRouter {
{
url,
as,
options: {
...options,
historyId: this.historyId,
},
options,
},
null,
as
......
......@@ -74,6 +74,18 @@ describe('Client Navigation', () => {
await browser.close()
})
it('should navigate back after reload', async () => {
const browser = await webdriver(context.appPort, '/nav')
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.eval(`window.location.href = window.location.href`)
await waitFor(5000)
await browser.eval(`window.history.back()`)
await waitFor(3000)
const text = await browser.elementsByCss('#about-link').text()
expect(text).toMatch(/About/)
})
it('should navigate via the client side', async () => {
const browser = await webdriver(context.appPort, '/nav')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册