From d32a25235d62fc7e5ca8a1f31dd39cb50ee9648d Mon Sep 17 00:00:00 2001 From: Jesper Kold-Hansen Date: Wed, 18 Sep 2024 22:13:14 +0200 Subject: [PATCH] [1.x] preserveScroll should be true on initial page visit (#1360) * Preserve scroll should be true on initial page visit. Preserve scroll should be true on initial page visit to mimic normal browser behavior. If a user has scrolled down the page, and then reloads, the page should stay where it is, so we cannot scroll to top, ergo we must preserve scroll. * Restore scroll on initial page load If a preserve scroll is set to true in setPage restore the scroll position. * Fixed a coding typo Just fixing a typing coding. --------- Co-authored-by: Pedro Borges --- packages/core/src/router.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/core/src/router.ts b/packages/core/src/router.ts index fab3793b2..79f0a3d32 100644 --- a/packages/core/src/router.ts +++ b/packages/core/src/router.ts @@ -86,7 +86,7 @@ export class Router { if (!this.page.url.includes(hash)) { this.page.url += hash } - this.setPage(page, { preserveState: true }).then(() => fireNavigateEvent(page)) + this.setPage(page, { preserveScroll: true ,preserveState: true }).then(() => fireNavigateEvent(page)) } protected setupEventListeners(): void { @@ -472,9 +472,8 @@ export class Router { replace = replace || hrefToUrl(page.url).href === window.location.href replace ? this.replaceState(page) : this.pushState(page) this.swapComponent({ component, page, preserveState }).then(() => { - if (!preserveScroll) { - this.resetScrollPositions() - } + preserveScroll ? this.restoreScrollPositions() : this.resetScrollPositions() + if (!replace) { fireNavigateEvent(page) }