Unverified Commit 32ee65ef authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Correct shallow routing behavior through history (#16477)

When navigating from a non-shallow page to a page that was navigated to shallowly in history it causes incorrect page data to be shown since an incorrect shallow navigation is done. We can only maintain shallow routing when the current page was navigated to shallowly and the page being navigated to in history was also navigated to shallowly. Additional tests to ensure this behavior is working has also been added. 

Closes: https://github.com/vercel/next.js/issues/7395
Closes: https://github.com/vercel/next.js/issues/14928
Fixes: https://github.com/vercel/next.js/issues/16028
parent 120b41ec
......@@ -245,6 +245,7 @@ export default class Router implements BaseRouter {
isSsr: boolean
isFallback: boolean
_inFlightRoute?: string
_shallow?: boolean
static events: MittEmitter = mitt()
......@@ -410,7 +411,14 @@ export default class Router implements BaseRouter {
return
}
this.change('replaceState', url, as, options)
this.change(
'replaceState',
url,
as,
Object.assign({}, options, {
shallow: options.shallow && this._shallow,
})
)
}
reload(): void {
......@@ -644,6 +652,7 @@ export default class Router implements BaseRouter {
}
if (method !== 'pushState' || getURL() !== as) {
this._shallow = options.shallow
window.history[method](
{
url,
......
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="props">{JSON.stringify(props)}</p>
<Link href="/first?a=b" shallow>
<a id="add-query-shallow">add query shallow</a>
</Link>
<br />
<Link href="/first" shallow>
<a id="remove-query-shallow">remove query shallow</a>
</Link>
<br />
<Link href="/another">
<a id="to-another">to /another non-shallow</a>
</Link>
</>
)
}
export const getServerSideProps = ({ params }) => {
return {
props: {
params,
random: Math.random(),
},
}
}
/* eslint-env jest */
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../')
let app
let appPort
const runTests = () => {
it('should not shallowly navigate back in history when current page was not shallow', async () => {
const browser = await webdriver(appPort, '/first')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.params).toEqual({ slug: 'first' })
await browser.elementByCss('#add-query-shallow').click()
const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual(props)
await browser.elementByCss('#remove-query-shallow').click()
const props3 = JSON.parse(await browser.elementByCss('#props').text())
expect(props3).toEqual(props)
await browser.elementByCss('#to-another').click()
const props4 = JSON.parse(await browser.elementByCss('#props').text())
expect(props4.params).toEqual({ slug: 'another' })
expect(props4.random).not.toBe(props.random)
await browser.back()
const props5 = JSON.parse(await browser.elementByCss('#props').text())
expect(props5.params).toEqual({ slug: 'first' })
expect(props5.random).not.toBe(props4.random)
})
it('should not shallowly navigate forwards in history when current page was not shallow', async () => {
const browser = await webdriver(appPort, '/first')
const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.params).toEqual({ slug: 'first' })
await browser.elementByCss('#add-query-shallow').click()
const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual(props)
await browser.elementByCss('#to-another').click()
const props3 = JSON.parse(await browser.elementByCss('#props').text())
expect(props3.params).toEqual({ slug: 'another' })
expect(props3.random).not.toBe(props2.random)
await browser.back()
const props4 = JSON.parse(await browser.elementByCss('#props').text())
expect(props4.params).toEqual({ slug: 'first' })
expect(props4.random).not.toBe(props3.random)
await browser.forward()
const props5 = JSON.parse(await browser.elementByCss('#props').text())
expect(props5.params).toEqual({ slug: 'another' })
expect(props5.random).not.toBe(props4.random)
})
}
describe('Client Shallow Routing', () => {
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