Unverified Commit 60b4e678 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix client-side hydration with dynamic routes (#7986)

* Add a test for the hash case

* Add a special hydration option to router

* Add additional tests wrt scrolling
parent 86b6a4b6
...@@ -260,7 +260,11 @@ export default class Router implements BaseRouter { ...@@ -260,7 +260,11 @@ export default class Router implements BaseRouter {
// If the url change is only related to a hash change // If the url change is only related to a hash change
// We should not proceed. We should only change the state. // We should not proceed. We should only change the state.
if (this.onlyAHashChange(as)) {
// WARNING: `_h` is an internal option for handing Next.js client-side
// hydration. Your app should _never_ use this property. It may change at
// any time without notice.
if (!options._h && this.onlyAHashChange(as)) {
this.asPath = as this.asPath = as
Router.events.emit('hashChangeStart', as) Router.events.emit('hashChangeStart', as)
this.changeState(method, url, as) this.changeState(method, url, as)
......
...@@ -95,7 +95,13 @@ class Container extends React.Component { ...@@ -95,7 +95,13 @@ class Container extends React.Component {
...router.query, ...router.query,
...parseQs(location.search.substr(1)) ...parseQs(location.search.substr(1))
}), }),
asPath asPath,
{
// WARNING: `_h` is an internal option for handing Next.js
// client-side hydration. Your app should _never_ use this property.
// It may change at any time without notice.
_h: 1
}
) )
} }
} }
......
...@@ -22,6 +22,10 @@ const Page = () => ( ...@@ -22,6 +22,10 @@ const Page = () => (
<Link href='/[post]?fromHome=true' as='/post-1?fromHome=true'> <Link href='/[post]?fromHome=true' as='/post-1?fromHome=true'>
<a id='view-post-1-with-query'>View post 1 with query</a> <a id='view-post-1-with-query'>View post 1 with query</a>
</Link> </Link>
<br />
<Link href='/on-mount/[post]' as='/on-mount/test-w-hash#item-400'>
<a id='view-dynamic-with-hash'>View test with hash</a>
</Link>
</div> </div>
) )
......
...@@ -3,5 +3,16 @@ import { useRouter } from 'next/router' ...@@ -3,5 +3,16 @@ import { useRouter } from 'next/router'
export default () => { export default () => {
const router = useRouter() const router = useRouter()
const { query } = router const { query } = router
return <p>onmpost: {query.post || 'pending'}</p> return (
<>
<p>onmpost: {query.post || 'pending'}</p>
{Array.from({ length: 500 }, (x, i) => i + 1).map(i => {
return (
<div key={`item-${i}`} id={`item-${i}`}>
{i}
</div>
)
})}
</>
)
} }
...@@ -156,15 +156,42 @@ function runTests () { ...@@ -156,15 +156,42 @@ function runTests () {
const html = await renderViaHTTP(appPort, '/on-mount/post-1') const html = await renderViaHTTP(appPort, '/on-mount/post-1')
expect(html).not.toMatch(/post:.*?\[post\].*?<\/p>/) expect(html).not.toMatch(/post:.*?\[post\].*?<\/p>/)
}) })
it('should update with a hash in the URL', async () => {
const browser = await webdriver(appPort, '/on-mount/post-1#abc')
await waitFor(1000)
const text = await browser.eval(`document.body.innerHTML`)
expect(text).toMatch(/onmpost:.*post-1/)
})
it('should scroll to a hash on mount', async () => {
const browser = await webdriver(appPort, '/on-mount/post-1#item-400')
await waitFor(1000)
const text = await browser.eval(`document.body.innerHTML`)
expect(text).toMatch(/onmpost:.*post-1/)
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(7232)
})
it('should scroll to a hash on client-side navigation', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(1000)
await browser.elementByCss('#view-dynamic-with-hash').click()
await browser.waitForElementByCss('p')
const text = await browser.elementByCss('p').text()
expect(text).toMatch(/onmpost:.*test-w-hash/)
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(7232)
})
} }
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
describe('Dynamic Routing', () => { describe('Dynamic Routing', () => {
beforeAll(async () => {
await fs.remove(nextConfig)
})
describe('dev mode', () => { describe('dev mode', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort() appPort = await findPort()
...@@ -195,6 +222,7 @@ describe('Dynamic Routing', () => { ...@@ -195,6 +222,7 @@ describe('Dynamic Routing', () => {
describe('SSR production mode', () => { describe('SSR production mode', () => {
beforeAll(async () => { beforeAll(async () => {
await fs.remove(nextConfig)
await fs.writeFile( await fs.writeFile(
nextConfig, nextConfig,
` `
...@@ -215,7 +243,10 @@ describe('Dynamic Routing', () => { ...@@ -215,7 +243,10 @@ describe('Dynamic Routing', () => {
server = await startApp(app) server = await startApp(app)
appPort = server.address().port appPort = server.address().port
}) })
afterAll(() => stopApp(server)) afterAll(async () => {
await stopApp(server)
await fs.remove(nextConfig)
})
runTests() 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