@@ -64,16 +64,11 @@ export function createRouter(
64
64
}
65
65
66
66
async function go ( href : string = inBrowser ? location . href : '/' ) {
67
- const currentHash = inBrowser ? location . hash : ''
68
67
href = normalizeHref ( href )
69
68
if ( ( await router . onBeforeRouteChange ?.( href ) ) === false ) return
70
69
updateHistory ( href )
71
70
await loadPage ( href )
72
71
await router . onAfterRouteChanged ?.( href )
73
- // do after the route is changed so location.hash in theme code is the new hash
74
- if ( new URL ( href , fakeHost ) . hash !== currentHash ) {
75
- window . dispatchEvent ( new Event ( 'hashchange' ) )
76
- }
77
72
}
78
73
79
74
let latestPendingPath : string | null = null
@@ -212,7 +207,7 @@ export function createRouter(
212
207
// use smooth scroll when clicking on header anchor links
213
208
scrollTo ( link , hash , link . classList . contains ( 'header-anchor' ) )
214
209
} else {
215
- updateHistory ( href )
210
+ updateHistory ( href , false ) // already emitted hashchange above
216
211
window . scrollTo ( 0 , 0 )
217
212
}
218
213
} else {
@@ -305,11 +300,15 @@ function shouldHotReload(payload: PageDataPayload): boolean {
305
300
return payloadPath === locationPath
306
301
}
307
302
308
- function updateHistory ( href : string ) {
303
+ function updateHistory ( href : string , emitHashChange = true ) {
309
304
if ( inBrowser && normalizeHref ( href ) !== normalizeHref ( location . href ) ) {
305
+ const currentHash = location . hash
310
306
// save scroll position before changing url
311
307
history . replaceState ( { scrollPosition : window . scrollY } , document . title )
312
308
history . pushState ( null , '' , href )
309
+ if ( emitHashChange && new URL ( href , fakeHost ) . hash !== currentHash ) {
310
+ window . dispatchEvent ( new Event ( 'hashchange' ) )
311
+ }
313
312
}
314
313
}
315
314
0 commit comments