mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Treat navigation to empty hash as hash navigate (#2971)
# -> #foo and #foo -> # now operate in the same way.
This commit is contained in:
parent
d19cc975f4
commit
c6bd6ef997
|
@ -244,7 +244,7 @@ export default class Router {
|
||||||
|
|
||||||
onlyAHashChange (as) {
|
onlyAHashChange (as) {
|
||||||
if (!this.asPath) return false
|
if (!this.asPath) return false
|
||||||
const [ oldUrlNoHash ] = this.asPath.split('#')
|
const [ oldUrlNoHash, oldHash ] = this.asPath.split('#')
|
||||||
const [ newUrlNoHash, newHash ] = as.split('#')
|
const [ newUrlNoHash, newHash ] = as.split('#')
|
||||||
|
|
||||||
// If the urls are change, there's more than a hash change
|
// If the urls are change, there's more than a hash change
|
||||||
|
@ -252,14 +252,11 @@ export default class Router {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there's no hash in the new url, we can't consider it as a hash change
|
// If the hash has changed, then it's a hash only change.
|
||||||
if (!newHash) {
|
// This check is necessary to handle both the enter and
|
||||||
return false
|
// leave hash === '' cases. The identity case falls through
|
||||||
}
|
// and is treated as a next reload.
|
||||||
|
return oldHash !== newHash
|
||||||
// Now there's a hash in the new URL.
|
|
||||||
// We don't need to worry about the old hash.
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollToHash (as) {
|
scrollToHash (as) {
|
||||||
|
|
|
@ -21,6 +21,9 @@ export default class SelfReload extends Component {
|
||||||
<Link href='/nav/hash-changes'>
|
<Link href='/nav/hash-changes'>
|
||||||
<a id='page-url'>Page URL</a>
|
<a id='page-url'>Page URL</a>
|
||||||
</Link>
|
</Link>
|
||||||
|
<Link href='#'>
|
||||||
|
<a id='via-empty-hash'>Via Empty Hash</a>
|
||||||
|
</Link>
|
||||||
<p>COUNT: {this.props.count}</p>
|
<p>COUNT: {this.props.count}</p>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -188,6 +188,21 @@ export default (context, render) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('when hash set to empty', () => {
|
||||||
|
it('should not run getInitialProps', async () => {
|
||||||
|
const browser = await webdriver(context.appPort, '/nav/hash-changes')
|
||||||
|
|
||||||
|
const counter = await browser
|
||||||
|
.elementByCss('#via-a').click()
|
||||||
|
.elementByCss('#via-empty-hash').click()
|
||||||
|
.elementByCss('p').text()
|
||||||
|
|
||||||
|
expect(counter).toBe('COUNT: 0')
|
||||||
|
|
||||||
|
browser.close()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('when hash changed to a different hash', () => {
|
describe('when hash changed to a different hash', () => {
|
||||||
it('should not run getInitialProps', async () => {
|
it('should not run getInitialProps', async () => {
|
||||||
const browser = await webdriver(context.appPort, '/nav/hash-changes')
|
const browser = await webdriver(context.appPort, '/nav/hash-changes')
|
||||||
|
|
Loading…
Reference in a new issue