@@ -28,6 +28,8 @@ export interface Router {
28
28
initialLoad ?: boolean
29
29
// Whether to smoothly scroll to the target position.
30
30
smoothScroll ?: boolean
31
+ // Whether to replace the current history entry.
32
+ replace ?: boolean
31
33
}
32
34
) => Promise < void >
33
35
/**
@@ -322,7 +324,7 @@ function normalizeHref(href: string): string {
322
324
323
325
async function changeRoute (
324
326
href : string ,
325
- { smoothScroll = false , initialLoad = false } = { }
327
+ { smoothScroll = false , initialLoad = false , replace = false } = { }
326
328
) : Promise < boolean > {
327
329
const loc = normalizeHref ( location . href )
328
330
const nextUrl = new URL ( href , location . origin )
@@ -334,9 +336,13 @@ async function changeRoute(
334
336
return false
335
337
}
336
338
} else {
337
- // save scroll position before changing URL
338
- history . replaceState ( { scrollPosition : window . scrollY } , '' )
339
- history . pushState ( { } , '' , href )
339
+ if ( replace ) {
340
+ history . replaceState ( { } , '' , href )
341
+ } else {
342
+ // save scroll position before changing URL
343
+ history . replaceState ( { scrollPosition : window . scrollY } , '' )
344
+ history . pushState ( { } , '' , href )
345
+ }
340
346
341
347
if ( nextUrl . pathname === currentUrl . pathname ) {
342
348
// scroll between hash anchors on the same page, avoid duplicate entries
0 commit comments