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(vue-app): fix regression with scrollToTop #7920

Merged
merged 7 commits into from
Aug 14, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/vue-app/template/router.scrollBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ if (process.client) {
}
}

export default function (to, from, savedPosition) {
// if the returned position is falsy or an empty object,
// will retain current scroll position.
export default function (to, from, savedPosition) {
// If the returned position is falsy or an empty object, will retain current scroll position
let position = false

// if no children detected and scrollToTop is not explicitly disabled
const Pages = getMatchedComponents(to)

// Scroll to the top of the page if...
if (
Pages.length < 2 &&
Pages.every(Page => Page.options.scrollToTop !== false)
// One of the children set `scrollToTop`
Pages.some(Page => Page.options.scrollToTop) ||
// scrollToTop set in only page without children
(Pages.length < 2 && Pages.every(Page => Page.options.scrollToTop !== false))
) {
// scroll to the top of the page
position = { x: 0, y: 0 }
} else if (Pages.some(Page => Page.options.scrollToTop)) {
// if one of the children has scrollToTop option set to true
position = { x: 0, y: 0 }
}

Expand All @@ -47,8 +45,12 @@ export default function (to, from, savedPosition) {

const nuxt = window.<%= globals.nuxt %>

// triggerScroll is only fired when a new component is loaded
if (to.path === from.path && to.hash !== from.hash) {
if (
// Page hash changes
(to.path === from.path && to.hash !== from.hash) ||
// Initial load (vuejs/vue-router#3199)
to === from
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva Do you think it is a safe check for detecting initial load?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not completely sure, we could also check if from is the START location that is exported in v4.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried exposing START ref but checking here, on initial load from and to are same references but not equal to START:

image

) {
nuxt.$nextTick(() => nuxt.$emit('triggerScroll'))
}

Expand Down