fix(build): consistent route.path across dev and ssr (#2997)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/3005/head
烽宁 1 year ago committed by GitHub
parent aa40cecd48
commit 0d56855b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -64,16 +64,8 @@ export function createRouter(
} }
async function go(href: string = inBrowser ? location.href : '/') { async function go(href: string = inBrowser ? location.href : '/') {
href = normalizeHref(href)
if ((await router.onBeforeRouteChange?.(href)) === false) return if ((await router.onBeforeRouteChange?.(href)) === false) return
const url = new URL(href, fakeHost)
if (!siteDataRef.value.cleanUrls) {
// ensure correct deep link so page refresh lands on correct files.
// if cleanUrls is enabled, the server should handle this
if (!url.pathname.endsWith('/') && !url.pathname.endsWith('.html')) {
url.pathname += '.html'
href = url.pathname + url.search + url.hash
}
}
updateHistory(href) updateHistory(href)
await loadPage(href) await loadPage(href)
await router.onAfterRouteChanged?.(href) await router.onAfterRouteChanged?.(href)
@ -230,7 +222,10 @@ export function createRouter(
) )
window.addEventListener('popstate', (e) => { window.addEventListener('popstate', (e) => {
loadPage(location.href, (e.state && e.state.scrollPosition) || 0) loadPage(
normalizeHref(location.href),
(e.state && e.state.scrollPosition) || 0
)
}) })
window.addEventListener('hashchange', (e) => { window.addEventListener('hashchange', (e) => {
@ -327,17 +322,28 @@ function handleHMR(route: Route): void {
} }
function shouldHotReload(payload: PageDataPayload): boolean { function shouldHotReload(payload: PageDataPayload): boolean {
const payloadPath = payload.path.replace(/(\bindex)?\.md$/, '') const payloadPath = payload.path.replace(/(?:(^|\/)index)?\.md$/, '$1')
const locationPath = location.pathname const locationPath = location.pathname
.replace(/(\bindex)?\.html$/, '') .replace(/(?:(^|\/)index)?\.html$/, '')
.slice(siteDataRef.value.base.length - 1) .slice(siteDataRef.value.base.length - 1)
return payloadPath === locationPath return payloadPath === locationPath
} }
function updateHistory(href: string) { function updateHistory(href: string) {
if (inBrowser && href !== location.href) { if (inBrowser && href !== normalizeHref(location.href)) {
// save scroll position before changing url // save scroll position before changing url
history.replaceState({ scrollPosition: window.scrollY }, document.title) history.replaceState({ scrollPosition: window.scrollY }, document.title)
history.pushState(null, '', href) history.pushState(null, '', href)
} }
} }
function normalizeHref(href: string): string {
const url = new URL(href, fakeHost)
url.pathname = url.pathname.replace(/(^|\/)index(\.html)?$/, '$1')
// ensure correct deep link so page refresh lands on correct files.
if (siteDataRef.value.cleanUrls)
url.pathname = url.pathname.replace(/\.html$/, '')
else if (!url.pathname.endsWith('/') && !url.pathname.endsWith('.html'))
url.pathname += '.html'
return url.pathname + url.search + url.hash
}

Loading…
Cancel
Save