From 721953c6a9131a99d4e46e1502eef978380e3156 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Wed, 22 Jan 2025 23:58:37 +0530 Subject: [PATCH] simplify stuff --- src/client/app/router.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index e22c545c..8eab24bb 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -75,15 +75,20 @@ export function createRouter( href = normalizeHref(href) let loc: string | null = null + if ((await router.onBeforeRouteChange?.(href)) === false) return + if (inBrowser) { loc = normalizeHref(location.href) + if (href === loc) return const { pathname, hash } = new URL(href, fakeHost) const currentLoc = new URL(loc, fakeHost) - if (pathname === currentLoc.pathname && href !== loc) { - history.pushState({}, '', href) + // save scroll position before changing url + history.replaceState({ scrollPosition: window.scrollY }, '') + history.pushState({}, '', href) + if (pathname === currentLoc.pathname) { if (hash !== currentLoc.hash) { window.dispatchEvent( new HashChangeEvent('hashchange', { @@ -99,12 +104,6 @@ export function createRouter( } } - if ((await router.onBeforeRouteChange?.(href)) === false) return - if (loc !== null && href !== loc) { - // save scroll position before changing url - history.replaceState({ scrollPosition: window.scrollY }, '') - history.pushState({}, '', href) - } await loadPage(href) await (router.onAfterRouteChange ?? router.onAfterRouteChanged)?.(href) }