Skip to content

Commit 74b725a

Browse files
authored
fix(client): don't reload page on hash change (#3777)
1 parent f65053c commit 74b725a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/client/app/router.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export function createRouter(
6868
if ((await router.onBeforeRouteChange?.(href)) === false) return
6969
if (inBrowser && href !== normalizeHref(location.href)) {
7070
// save scroll position before changing url
71-
history.replaceState({ scrollPosition: window.scrollY }, document.title)
72-
history.pushState(null, '', href)
71+
history.replaceState({ scrollPosition: window.scrollY }, '')
72+
history.pushState({}, '', href)
7373
}
7474
await loadPage(href)
7575
await router.onAfterRouteChanged?.(href)
@@ -111,7 +111,7 @@ export function createRouter(
111111
if (actualPathname !== targetLoc.pathname) {
112112
targetLoc.pathname = actualPathname
113113
href = actualPathname + targetLoc.search + targetLoc.hash
114-
history.replaceState(null, '', href)
114+
history.replaceState({}, '', href)
115115
}
116116

117117
if (targetLoc.hash && !scrollPosition) {
@@ -162,6 +162,9 @@ export function createRouter(
162162
}
163163

164164
if (inBrowser) {
165+
if (history.state === null) {
166+
history.replaceState({}, '')
167+
}
165168
window.addEventListener(
166169
'click',
167170
(e) => {
@@ -203,7 +206,7 @@ export function createRouter(
203206
// scroll between hash anchors in the same page
204207
// avoid duplicate history entries when the hash is same
205208
if (hash !== currentUrl.hash) {
206-
history.pushState(null, '', href)
209+
history.pushState({}, '', href)
207210
// still emit the event so we can listen to it in themes
208211
window.dispatchEvent(
209212
new HashChangeEvent('hashchange', {
@@ -228,6 +231,9 @@ export function createRouter(
228231
)
229232

230233
window.addEventListener('popstate', async (e) => {
234+
if (e.state === null) {
235+
return
236+
}
231237
await loadPage(
232238
normalizeHref(location.href),
233239
(e.state && e.state.scrollPosition) || 0

0 commit comments

Comments
 (0)