fix(vue-app): fix regression with `scrollToTop` (#7920)

This commit is contained in:
pooya parsa 2020-08-14 23:42:04 +02:00 committed by GitHub
parent b487c7deb9
commit 9f4d420b1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View File

@ -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 }
}
@ -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 (
// Route hash changes
(to.path === from.path && to.hash !== from.hash) ||
// Initial load (vuejs/vue-router#3199)
to === from
) {
nuxt.$nextTick(() => nuxt.$emit('triggerScroll'))
}

View File

@ -20,7 +20,7 @@ describe('nuxt minimal vue-app bundle size limit', () => {
it('should stay within the size limit range', async () => {
const filter = filename => filename === 'vue-app.nuxt.js'
const legacyResourcesSize = await getResourcesSize(distDir, 'client', { filter })
const LEGACY_JS_RESOURCES_KB_SIZE = 16.5
const LEGACY_JS_RESOURCES_KB_SIZE = 16.6
expect(legacyResourcesSize.uncompressed).toBeWithinSize(LEGACY_JS_RESOURCES_KB_SIZE)
})
})