From 70ad7ebf80c96c375475d0f947267053dcb7724e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 19 Oct 2022 15:41:37 +0200 Subject: [PATCH 1/5] fix(app): scroll to top on dynamic route with different params --- packages/nuxt/src/pages/runtime/router.options.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/pages/runtime/router.options.ts b/packages/nuxt/src/pages/runtime/router.options.ts index 1137b346dde..e8f88e3c18b 100644 --- a/packages/nuxt/src/pages/runtime/router.options.ts +++ b/packages/nuxt/src/pages/runtime/router.options.ts @@ -16,11 +16,8 @@ export default { let position: ScrollPosition = savedPosition || undefined // Scroll to top if route is changed by default - if ( - !position && - (from && to && from.matched[0] !== to.matched[0]) && - to.meta.scrollToTop !== false - ) { + const isDifferentRoute = from && to && (from.matched[0] !== to.matched[0] || (from.matched[0] === to.matched[0] && JSON.stringify(from.params) !== JSON.stringify(to.params))) + if (!position && isDifferentRoute && to.meta.scrollToTop !== false) { position = { left: 0, top: 0 } } From f1c286d7b8305c545ead3a51cd162c5b76ecef7a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 19 Oct 2022 15:56:09 +0200 Subject: [PATCH 2/5] refactor: isDifferent route --- .../nuxt/src/pages/runtime/router.options.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/pages/runtime/router.options.ts b/packages/nuxt/src/pages/runtime/router.options.ts index e8f88e3c18b..41e71b9bfff 100644 --- a/packages/nuxt/src/pages/runtime/router.options.ts +++ b/packages/nuxt/src/pages/runtime/router.options.ts @@ -1,5 +1,6 @@ import type { RouterConfig } from '@nuxt/schema' -import type { RouterScrollBehavior } from 'vue-router' +import type { RouterScrollBehavior, RouteLocationNormalized } from 'vue-router' +import { isEqual } from 'ohash' import { nextTick } from 'vue' import { useNuxtApp } from '#app' @@ -16,8 +17,7 @@ export default { let position: ScrollPosition = savedPosition || undefined // Scroll to top if route is changed by default - const isDifferentRoute = from && to && (from.matched[0] !== to.matched[0] || (from.matched[0] === to.matched[0] && JSON.stringify(from.params) !== JSON.stringify(to.params))) - if (!position && isDifferentRoute && to.meta.scrollToTop !== false) { + if (!position && from && to && to.meta.scrollToTop !== false && isDifferentRoute(a, b)) { position = { left: 0, top: 0 } } @@ -53,3 +53,14 @@ function _getHashElementScrollMarginTop (selector: string): number { } return 0 } + +function isDifferentRoute (a: RouteLocationNormalized, b: RouteLocationNormalized): boolean { + const samePageComponent = a.matched[0] === b.matched[0] + if (!samePageComponent) { + return true + } + if (!samePageComponent && !isEqual(a.params, b.params)) { + return true + } + return false +} From f7a0fce651d84d2d07c9d3b24bde768a8c5931e7 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 19 Oct 2022 15:56:58 +0200 Subject: [PATCH 3/5] fix typo --- packages/nuxt/src/pages/runtime/router.options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/pages/runtime/router.options.ts b/packages/nuxt/src/pages/runtime/router.options.ts index 41e71b9bfff..1b15ab8584c 100644 --- a/packages/nuxt/src/pages/runtime/router.options.ts +++ b/packages/nuxt/src/pages/runtime/router.options.ts @@ -59,7 +59,7 @@ function isDifferentRoute (a: RouteLocationNormalized, b: RouteLocationNormalize if (!samePageComponent) { return true } - if (!samePageComponent && !isEqual(a.params, b.params)) { + if (samePageComponent && !isEqual(a.params, b.params)) { return true } return false From 41761faa40e39f455a48686dc05c6c7b4f528e3e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 19 Oct 2022 15:57:47 +0200 Subject: [PATCH 4/5] style: _ --- packages/nuxt/src/pages/runtime/router.options.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/pages/runtime/router.options.ts b/packages/nuxt/src/pages/runtime/router.options.ts index 1b15ab8584c..9ea59805468 100644 --- a/packages/nuxt/src/pages/runtime/router.options.ts +++ b/packages/nuxt/src/pages/runtime/router.options.ts @@ -17,7 +17,7 @@ export default { let position: ScrollPosition = savedPosition || undefined // Scroll to top if route is changed by default - if (!position && from && to && to.meta.scrollToTop !== false && isDifferentRoute(a, b)) { + if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(a, b)) { position = { left: 0, top: 0 } } @@ -54,7 +54,7 @@ function _getHashElementScrollMarginTop (selector: string): number { return 0 } -function isDifferentRoute (a: RouteLocationNormalized, b: RouteLocationNormalized): boolean { +function _isDifferentRoute (a: RouteLocationNormalized, b: RouteLocationNormalized): boolean { const samePageComponent = a.matched[0] === b.matched[0] if (!samePageComponent) { return true From 8a25bb8be3435b5c6a8c0bb02564be1518a51c02 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 19 Oct 2022 16:00:09 +0200 Subject: [PATCH 5/5] fix typo --- packages/nuxt/src/pages/runtime/router.options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxt/src/pages/runtime/router.options.ts b/packages/nuxt/src/pages/runtime/router.options.ts index 9ea59805468..a6541810725 100644 --- a/packages/nuxt/src/pages/runtime/router.options.ts +++ b/packages/nuxt/src/pages/runtime/router.options.ts @@ -17,7 +17,7 @@ export default { let position: ScrollPosition = savedPosition || undefined // Scroll to top if route is changed by default - if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(a, b)) { + if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(from, to)) { position = { left: 0, top: 0 } }