From 0d4e0e52f7c2eab736cc568d3fc3517822eda3e1 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Sat, 10 Jun 2017 01:02:28 +0530 Subject: [PATCH] Implement the correct browser behavior for hash changes. (#2223) --- lib/router/router.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/router/router.js b/lib/router/router.js index a5197288..0b560cf2 100644 --- a/lib/router/router.js +++ b/lib/router/router.js @@ -132,9 +132,10 @@ export default class Router { const { pathname, query } = parse(url, true) // 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)) { - this.changeState('replaceState', url, as) + this.changeState(method, url, as) + this.scrollToHash(as) return } @@ -261,6 +262,14 @@ export default class Router { return true } + scrollToHash (as) { + const [ , hash ] = as.split('#') + const el = document.getElementById(hash) + if (el) { + el.scrollIntoView() + } + } + urlIsNew (pathname, query) { return this.pathname !== pathname || !shallowEquals(query, this.query) }