|
|
@ -68,8 +68,8 @@ export function createRouter(
|
|
|
|
if ((await router.onBeforeRouteChange?.(href)) === false) return
|
|
|
|
if ((await router.onBeforeRouteChange?.(href)) === false) return
|
|
|
|
if (inBrowser && href !== normalizeHref(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 }, '')
|
|
|
|
history.pushState(null, '', href)
|
|
|
|
history.pushState({}, '', href)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await loadPage(href)
|
|
|
|
await loadPage(href)
|
|
|
|
await router.onAfterRouteChanged?.(href)
|
|
|
|
await router.onAfterRouteChanged?.(href)
|
|
|
@ -111,7 +111,7 @@ export function createRouter(
|
|
|
|
if (actualPathname !== targetLoc.pathname) {
|
|
|
|
if (actualPathname !== targetLoc.pathname) {
|
|
|
|
targetLoc.pathname = actualPathname
|
|
|
|
targetLoc.pathname = actualPathname
|
|
|
|
href = actualPathname + targetLoc.search + targetLoc.hash
|
|
|
|
href = actualPathname + targetLoc.search + targetLoc.hash
|
|
|
|
history.replaceState(null, '', href)
|
|
|
|
history.replaceState({}, '', href)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (targetLoc.hash && !scrollPosition) {
|
|
|
|
if (targetLoc.hash && !scrollPosition) {
|
|
|
@ -162,6 +162,9 @@ export function createRouter(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inBrowser) {
|
|
|
|
if (inBrowser) {
|
|
|
|
|
|
|
|
if (history.state === null) {
|
|
|
|
|
|
|
|
history.replaceState({}, '')
|
|
|
|
|
|
|
|
}
|
|
|
|
window.addEventListener(
|
|
|
|
window.addEventListener(
|
|
|
|
'click',
|
|
|
|
'click',
|
|
|
|
(e) => {
|
|
|
|
(e) => {
|
|
|
@ -203,7 +206,7 @@ export function createRouter(
|
|
|
|
// scroll between hash anchors in the same page
|
|
|
|
// scroll between hash anchors in the same page
|
|
|
|
// avoid duplicate history entries when the hash is same
|
|
|
|
// avoid duplicate history entries when the hash is same
|
|
|
|
if (hash !== currentUrl.hash) {
|
|
|
|
if (hash !== currentUrl.hash) {
|
|
|
|
history.pushState(null, '', href)
|
|
|
|
history.pushState({}, '', href)
|
|
|
|
// still emit the event so we can listen to it in themes
|
|
|
|
// still emit the event so we can listen to it in themes
|
|
|
|
window.dispatchEvent(
|
|
|
|
window.dispatchEvent(
|
|
|
|
new HashChangeEvent('hashchange', {
|
|
|
|
new HashChangeEvent('hashchange', {
|
|
|
@ -228,6 +231,9 @@ export function createRouter(
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener('popstate', async (e) => {
|
|
|
|
window.addEventListener('popstate', async (e) => {
|
|
|
|
|
|
|
|
if (e.state === null) {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
await loadPage(
|
|
|
|
await loadPage(
|
|
|
|
normalizeHref(location.href),
|
|
|
|
normalizeHref(location.href),
|
|
|
|
(e.state && e.state.scrollPosition) || 0
|
|
|
|
(e.state && e.state.scrollPosition) || 0
|
|
|
|