mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Implement the correct browser behavior for hash changes. (#2223)
This commit is contained in:
parent
28b40a5d41
commit
0d4e0e52f7
|
@ -132,9 +132,10 @@ export default class Router {
|
||||||
const { pathname, query } = parse(url, true)
|
const { pathname, query } = parse(url, true)
|
||||||
|
|
||||||
// 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 replace the state.
|
// We should not proceed. We should only change the state.
|
||||||
if (this.onlyAHashChange(as)) {
|
if (this.onlyAHashChange(as)) {
|
||||||
this.changeState('replaceState', url, as)
|
this.changeState(method, url, as)
|
||||||
|
this.scrollToHash(as)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,6 +262,14 @@ export default class Router {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scrollToHash (as) {
|
||||||
|
const [ , hash ] = as.split('#')
|
||||||
|
const el = document.getElementById(hash)
|
||||||
|
if (el) {
|
||||||
|
el.scrollIntoView()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
urlIsNew (pathname, query) {
|
urlIsNew (pathname, query) {
|
||||||
return this.pathname !== pathname || !shallowEquals(query, this.query)
|
return this.pathname !== pathname || !shallowEquals(query, this.query)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue