From b47238878a16f11c96f5f0041c5ab667fda69d3b Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 23 Jan 2025 15:29:20 +0530 Subject: [PATCH] restore comments --- src/client/app/router.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index caf36a2d..8152f98f 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -95,11 +95,15 @@ export function createRouter( if (href === loc) { if (!initialLoad) return scrollTo(hash, smoothScroll) } else { + // save scroll position before changing url history.replaceState({ scrollPosition: window.scrollY }, '') history.pushState({}, '', href) if (pathname === currentLoc.pathname) { + // scroll between hash anchors in the same page + // avoid duplicate history entries when the hash is same if (hash !== currentLoc.hash) { + // still emit the event so we can listen to it in themes window.dispatchEvent( new HashChangeEvent('hashchange', { oldURL: currentLoc.href, @@ -238,6 +242,7 @@ export function createRouter( // only intercept inbound html links if (origin === currentLoc.origin && treatAsHtml(pathname)) { e.preventDefault() + // use smooth scroll when clicking on header anchor links go(href, { smoothScroll: link.classList.contains('header-anchor') }) } },