@@ -64,16 +64,8 @@ export function createRouter(
64
64
}
65
65
66
66
async function go ( href : string = inBrowser ? location . href : '/' ) {
67
+ href = normalizeHref ( href )
67
68
if ( ( await router . onBeforeRouteChange ?.( href ) ) === false ) return
68
- const url = new URL ( href , fakeHost )
69
- if ( ! siteDataRef . value . cleanUrls ) {
70
- // ensure correct deep link so page refresh lands on correct files.
71
- // if cleanUrls is enabled, the server should handle this
72
- if ( ! url . pathname . endsWith ( '/' ) && ! url . pathname . endsWith ( '.html' ) ) {
73
- url . pathname += '.html'
74
- href = url . pathname + url . search + url . hash
75
- }
76
- }
77
69
updateHistory ( href )
78
70
await loadPage ( href )
79
71
await router . onAfterRouteChanged ?.( href )
@@ -230,7 +222,10 @@ export function createRouter(
230
222
)
231
223
232
224
window . addEventListener ( 'popstate' , ( e ) => {
233
- loadPage ( location . href , ( e . state && e . state . scrollPosition ) || 0 )
225
+ loadPage (
226
+ normalizeHref ( location . href ) ,
227
+ ( e . state && e . state . scrollPosition ) || 0
228
+ )
234
229
} )
235
230
236
231
window . addEventListener ( 'hashchange' , ( e ) => {
@@ -327,17 +322,28 @@ function handleHMR(route: Route): void {
327
322
}
328
323
329
324
function shouldHotReload ( payload : PageDataPayload ) : boolean {
330
- const payloadPath = payload . path . replace ( / ( \b i n d e x ) ? \. m d $ / , '' )
325
+ const payloadPath = payload . path . replace ( / (?: ( ^ | \/ ) i n d e x ) ? \. m d $ / , '$1 ' )
331
326
const locationPath = location . pathname
332
- . replace ( / ( \b i n d e x ) ? \. h t m l $ / , '' )
327
+ . replace ( / (?: ( ^ | \/ ) i n d e x ) ? \. h t m l $ / , '' )
333
328
. slice ( siteDataRef . value . base . length - 1 )
334
329
return payloadPath === locationPath
335
330
}
336
331
337
332
function updateHistory ( href : string ) {
338
- if ( inBrowser && href !== location . href ) {
333
+ if ( inBrowser && href !== normalizeHref ( location . href ) ) {
339
334
// save scroll position before changing url
340
335
history . replaceState ( { scrollPosition : window . scrollY } , document . title )
341
336
history . pushState ( null , '' , href )
342
337
}
343
338
}
339
+
340
+ function normalizeHref ( href : string ) : string {
341
+ const url = new URL ( href , fakeHost )
342
+ url . pathname = url . pathname . replace ( / ( ^ | \/ ) i n d e x ( \. h t m l ) ? $ / , '$1' )
343
+ // ensure correct deep link so page refresh lands on correct files.
344
+ if ( siteDataRef . value . cleanUrls )
345
+ url . pathname = url . pathname . replace ( / \. h t m l $ / , '' )
346
+ else if ( ! url . pathname . endsWith ( '/' ) && ! url . pathname . endsWith ( '.html' ) )
347
+ url . pathname += '.html'
348
+ return url . pathname + url . search + url . hash
349
+ }
0 commit comments