Skip to content
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { cleanPath } from './util/path'
import { createMatcher } from './create-matcher'
import { normalizeLocation } from './util/location'
import { supportsPushState } from './util/push-state'
import { handleScroll } from './util/scroll'

import { HashHistory } from './history/hash'
import { HTML5History } from './history/html5'
Expand Down Expand Up @@ -117,8 +118,18 @@ export default class VueRouter {
const history = this.history

if (history instanceof HTML5History || history instanceof HashHistory) {
const setupListeners = () => {
const handleInitialScroll = (routeOrError) => {
const from = history.current
const expectScroll = this.options.scrollBehavior
const supportsScroll = supportsPushState && expectScroll

if (supportsScroll && 'fullPath' in routeOrError) {
handleScroll(this, routeOrError, from, false)
}
}
const setupListeners = (routeOrError) => {
history.setupListeners()
handleInitialScroll(routeOrError)
}
history.transitionTo(history.getCurrentLocation(), setupListeners, setupListeners)
}
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/specs/scroll-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ module.exports = {
null,
'scroll to anchor that starts with number'
)

.url('http://localhost:8080/scroll-behavior/bar#anchor')
.execute(function () {
location.reload(true)
})
.waitForElementVisible('#app', 1000)
.assert.evaluate(
function () {
return document.getElementById('anchor').getBoundingClientRect().top < 1
},
null,
'scroll to anchor on load'
)

.end()
}
}