Skip to content

Commit c6bd6ef

Browse files
kpdeckertimneutkens
authored andcommitted
Treat navigation to empty hash as hash navigate (vercel#2971)
# -> #foo and #foo -> # now operate in the same way.
1 parent d19cc97 commit c6bd6ef

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

lib/router/router.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,19 @@ export default class Router {
244244

245245
onlyAHashChange (as) {
246246
if (!this.asPath) return false
247-
const [ oldUrlNoHash ] = this.asPath.split('#')
247+
const [ oldUrlNoHash, oldHash ] = this.asPath.split('#')
248248
const [ newUrlNoHash, newHash ] = as.split('#')
249249

250250
// If the urls are change, there's more than a hash change
251251
if (oldUrlNoHash !== newUrlNoHash) {
252252
return false
253253
}
254254

255-
// If there's no hash in the new url, we can't consider it as a hash change
256-
if (!newHash) {
257-
return false
258-
}
259-
260-
// Now there's a hash in the new URL.
261-
// We don't need to worry about the old hash.
262-
return true
255+
// If the hash has changed, then it's a hash only change.
256+
// This check is necessary to handle both the enter and
257+
// leave hash === '' cases. The identity case falls through
258+
// and is treated as a next reload.
259+
return oldHash !== newHash
263260
}
264261

265262
scrollToHash (as) {

test/integration/basic/pages/nav/hash-changes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export default class SelfReload extends Component {
2121
<Link href='/nav/hash-changes'>
2222
<a id='page-url'>Page URL</a>
2323
</Link>
24+
<Link href='#'>
25+
<a id='via-empty-hash'>Via Empty Hash</a>
26+
</Link>
2427
<p>COUNT: {this.props.count}</p>
2528
</div>
2629
)

test/integration/basic/test/client-navigation.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ export default (context, render) => {
188188
})
189189
})
190190

191+
describe('when hash set to empty', () => {
192+
it('should not run getInitialProps', async () => {
193+
const browser = await webdriver(context.appPort, '/nav/hash-changes')
194+
195+
const counter = await browser
196+
.elementByCss('#via-a').click()
197+
.elementByCss('#via-empty-hash').click()
198+
.elementByCss('p').text()
199+
200+
expect(counter).toBe('COUNT: 0')
201+
202+
browser.close()
203+
})
204+
})
205+
191206
describe('when hash changed to a different hash', () => {
192207
it('should not run getInitialProps', async () => {
193208
const browser = await webdriver(context.appPort, '/nav/hash-changes')

0 commit comments

Comments
 (0)