From 7b26e5c4338daebec33c8fc45bee25ada7c3a42d Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Wed, 10 Apr 2024 20:50:06 +0530 Subject: [PATCH] fix(client): don't reload page on hash change --- src/client/app/router.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 9fe2314ef7c7..e51b7f80faa0 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -68,8 +68,8 @@ export function createRouter( if ((await router.onBeforeRouteChange?.(href)) === false) return if (inBrowser && href !== normalizeHref(location.href)) { // save scroll position before changing url - history.replaceState({ scrollPosition: window.scrollY }, document.title) - history.pushState(null, '', href) + history.replaceState({ scrollPosition: window.scrollY }, '') + history.pushState({}, '', href) } await loadPage(href) await router.onAfterRouteChanged?.(href) @@ -111,7 +111,7 @@ export function createRouter( if (actualPathname !== targetLoc.pathname) { targetLoc.pathname = actualPathname href = actualPathname + targetLoc.search + targetLoc.hash - history.replaceState(null, '', href) + history.replaceState({}, '', href) } if (targetLoc.hash && !scrollPosition) { @@ -162,6 +162,9 @@ export function createRouter( } if (inBrowser) { + if (history.state === null) { + history.replaceState({}, '') + } window.addEventListener( 'click', (e) => { @@ -203,7 +206,7 @@ export function createRouter( // scroll between hash anchors in the same page // avoid duplicate history entries when the hash is same if (hash !== currentUrl.hash) { - history.pushState(null, '', href) + history.pushState({}, '', href) // still emit the event so we can listen to it in themes window.dispatchEvent( new HashChangeEvent('hashchange', { @@ -228,6 +231,9 @@ export function createRouter( ) window.addEventListener('popstate', async (e) => { + if (e.state === null) { + return + } await loadPage( normalizeHref(location.href), (e.state && e.state.scrollPosition) || 0