From f3d3332fff72d1df6f70c5893bfc90442b1776fb Mon Sep 17 00:00:00 2001 From: lchzh3473 Date: Fri, 21 Apr 2023 16:54:58 +0800 Subject: [PATCH] fix(router): scroll back to the hash anchor even if it is already selected (#2265) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- src/client/app/router.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 42f12021..2be95f09 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -187,10 +187,13 @@ export function createRouter( search === currentUrl.search ) { // scroll between hash anchors in the same page - if (hash && hash !== currentUrl.hash) { - history.pushState(null, '', hash) - // still emit the event so we can listen to it in themes - window.dispatchEvent(new Event('hashchange')) + if (hash) { + // avoid duplicate history entries when the hash is same + if (hash !== currentUrl.hash) { + history.pushState(null, '', hash) + // still emit the event so we can listen to it in themes + window.dispatchEvent(new Event('hashchange')) + } // use smooth scroll when clicking on header anchor links scrollTo(link, hash, link.classList.contains('header-anchor')) }