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) {
|
||||
if (!this.asPath) return false
|
||||
const [ oldUrlNoHash ] = this.asPath.split('#')
|
||||
const [ oldUrlNoHash, oldHash ] = this.asPath.split('#')
|
||||
const [ newUrlNoHash, newHash ] = as.split('#')
|
||||
|
||||
// If the urls are change, there's more than a hash change
|
||||
|
@ -252,14 +252,11 @@ export default class Router {
|
|||
return false
|
||||
}
|
||||
|
||||
// If there's no hash in the new url, we can't consider it as a hash change
|
||||
if (!newHash) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Now there's a hash in the new URL.
|
||||
// We don't need to worry about the old hash.
|
||||
return true
|
||||
// If the hash has changed, then it's a hash only change.
|
||||
// This check is necessary to handle both the enter and
|
||||
// leave hash === '' cases. The identity case falls through
|
||||
// and is treated as a next reload.
|
||||
return oldHash !== newHash
|
||||
}
|
||||
|
||||
scrollToHash (as) {
|
||||
|
|
|
@ -21,6 +21,9 @@ export default class SelfReload extends Component {
|
|||
<Link href='/nav/hash-changes'>
|
||||
<a id='page-url'>Page URL</a>
|
||||
</Link>
|
||||
<Link href='#'>
|
||||
<a id='via-empty-hash'>Via Empty Hash</a>
|
||||
</Link>
|
||||
<p>COUNT: {this.props.count}</p>
|
||||
</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', () => {
|
||||
it('should not run getInitialProps', async () => {
|
||||
const browser = await webdriver(context.appPort, '/nav/hash-changes')
|
||||
|
|
Loading…
Reference in a new issue