Unverified Commit 91242ca6 authored by Jan Potoms's avatar Jan Potoms Committed by GitHub

Ignore history state not created by next.js (#15379)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 17420c78
......@@ -341,7 +341,11 @@ export default class Router implements BaseRouter {
return
}
const { url, as, options } = e.state
const { url, as, options, __N } = e.state
if (!__N) {
// this history state wasn't created by next.js so it can be ignored
return
}
const { pathname } = parseRelativeUrl(url)
// Make sure we don't re-render on initial load,
......@@ -596,6 +600,7 @@ export default class Router implements BaseRouter {
url,
as,
options,
__N: true,
},
// Most browsers currently ignores this parameter, although they may use it in the future.
// Passing the empty string here should be safe against future changes to the method.
......
......@@ -1104,6 +1104,66 @@ describe('Client Navigation', () => {
})
})
describe('foreign history manipulation', () => {
it('should ignore history state without options', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/nav')
// push history object without options
await browser.eval(
'window.history.pushState({ url: "/whatever" }, "", "/whatever")'
)
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await waitFor(1000)
expect(await hasRedbox(browser)).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should ignore history state with an invalid url', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/nav')
// push history object wit invalid url (not relative)
await browser.eval(
'window.history.pushState({ url: "http://google.com" }, "", "/whatever")'
)
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await waitFor(1000)
expect(await hasRedbox(browser)).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should ignore foreign history state with missing properties', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/nav')
// push empty history state
await browser.eval('window.history.pushState({}, "", "/whatever")')
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await waitFor(1000)
expect(await hasRedbox(browser)).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
})
it('should not error on module.exports + polyfills', async () => {
let browser
try {
......
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