From 9f4d420b1f5f120d8f29b7b2a74f12b390f71a36 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Fri, 14 Aug 2020 23:42:04 +0200 Subject: [PATCH] fix(vue-app): fix regression with `scrollToTop` (#7920) --- .../vue-app/template/router.scrollBehavior.js | 26 ++++++++++--------- test/dev/unicode-base.size-limit.test.js | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/vue-app/template/router.scrollBehavior.js b/packages/vue-app/template/router.scrollBehavior.js index 2d124c1257..0a9eee54e4 100644 --- a/packages/vue-app/template/router.scrollBehavior.js +++ b/packages/vue-app/template/router.scrollBehavior.js @@ -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')) } diff --git a/test/dev/unicode-base.size-limit.test.js b/test/dev/unicode-base.size-limit.test.js index 6f22f4c9cf..3dd5fbe91c 100644 --- a/test/dev/unicode-base.size-limit.test.js +++ b/test/dev/unicode-base.size-limit.test.js @@ -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) }) })