Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scroll): run scrollBehavior on initial load (fix #3196) #3199

Merged
merged 10 commits into from Jun 18, 2020
12 changes: 11 additions & 1 deletion src/index.js
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,17 @@ export default class VueRouter {
const history = this.history

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

if (supportsScroll) {
handleScroll(this, route, route, false)
Calvin-LL marked this conversation as resolved.
Show resolved Hide resolved
}
}
const setupListeners = (route) => {
history.setupListeners()
handleInitialScroll(route)
}
history.transitionTo(history.getCurrentLocation(), setupListeners, setupListeners)
}
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/specs/scroll-behavior.js
Expand Up @@ -119,6 +119,44 @@ 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'
)
.url('http://localhost:8080/scroll-behavior/bar#anchor2')
.execute(function () {
location.reload(true)
})
.waitForElementVisible('#app', 1000)
Calvin-LL marked this conversation as resolved.
Show resolved Hide resolved
.assert.evaluate(
function () {
return document.getElementById('anchor2').getBoundingClientRect().top < 101
},
null,
'scroll to anchor with offset on load'
)
.url('http://localhost:8080/scroll-behavior/bar#1number')
.execute(function () {
location.reload(true)
})
.waitForElementVisible('#app', 1000)
.assert.evaluate(
function () {
return document.getElementById('1number').getBoundingClientRect().top < 1
},
null,
'scroll to anchor that starts with number on load'
)

.end()
}
}