Commit 154d7846 authored by JJ Kasper's avatar JJ Kasper Committed by 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
parent 788654e2
...@@ -71,7 +71,7 @@ export default class Router implements BaseRouter { ...@@ -71,7 +71,7 @@ export default class Router implements BaseRouter {
_bps: BeforePopStateCallback | undefined _bps: BeforePopStateCallback | undefined
events: MittEmitter events: MittEmitter
_wrapApp: (App: ComponentType) => any _wrapApp: (App: ComponentType) => any
historyId: number isSsr: boolean
static events: MittEmitter = mitt() static events: MittEmitter = mitt()
...@@ -127,8 +127,9 @@ export default class Router implements BaseRouter { ...@@ -127,8 +127,9 @@ export default class Router implements BaseRouter {
this.sub = subscription this.sub = subscription
this.clc = null this.clc = null
this._wrapApp = wrapApp this._wrapApp = wrapApp
// we use a historyId to enable ignoring invalid popstates // make sure to ignore extra popState in safari on navigating
this.historyId = Math.random() // back from external site
this.isSsr = true
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// in order for `e.state` to work on the `onpopstate` event // in order for `e.state` to work on the `onpopstate` event
...@@ -170,7 +171,12 @@ export default class Router implements BaseRouter { ...@@ -170,7 +171,12 @@ export default class Router implements BaseRouter {
// Make sure we don't re-render on initial load, // Make sure we don't re-render on initial load,
// can be caused by navigating back from an external site // 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 return
} }
...@@ -245,6 +251,9 @@ export default class Router implements BaseRouter { ...@@ -245,6 +251,9 @@ export default class Router implements BaseRouter {
change(method: string, _url: Url, _as: Url, options: any): Promise<boolean> { change(method: string, _url: Url, _as: Url, options: any): Promise<boolean> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!options._h) {
this.isSsr = false
}
// marking route changes as a navigation start entry // marking route changes as a navigation start entry
if (SUPPORTS_PERFORMANCE_USER_TIMING) { if (SUPPORTS_PERFORMANCE_USER_TIMING) {
performance.mark('routeChange') performance.mark('routeChange')
...@@ -380,10 +389,7 @@ export default class Router implements BaseRouter { ...@@ -380,10 +389,7 @@ export default class Router implements BaseRouter {
{ {
url, url,
as, as,
options: { options,
...options,
historyId: this.historyId,
},
}, },
null, null,
as as
......
...@@ -74,6 +74,18 @@ describe('Client Navigation', () => { ...@@ -74,6 +74,18 @@ describe('Client Navigation', () => {
await browser.close() 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 () => { it('should navigate via the client side', async () => {
const browser = await webdriver(context.appPort, '/nav') const browser = await webdriver(context.appPort, '/nav')
......
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