support hash and query

pull/4511/head
Divyansh Singh 8 months ago
parent 70fc22ef97
commit c57d4f2da1

@ -7,6 +7,8 @@ import { getScrollOffset, inBrowser, withBase } from './utils'
export interface Route { export interface Route {
path: string path: string
hash?: string
query?: string
data: PageData data: PageData
component: Component | null component: Component | null
} }
@ -45,10 +47,6 @@ export interface Router {
* Called after the route changes. * Called after the route changes.
*/ */
onAfterRouteChange?: (to: string) => Awaitable<void> onAfterRouteChange?: (to: string) => Awaitable<void>
/**
* @deprecated use `onAfterRouteChange` instead
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
} }
export const RouterSymbol: InjectionKey<Router> = Symbol() export const RouterSymbol: InjectionKey<Router> = Symbol()
@ -80,7 +78,8 @@ export function createRouter(
href = normalizeHref(href) href = normalizeHref(href)
if ((await router.onBeforeRouteChange?.(href)) === false) return if ((await router.onBeforeRouteChange?.(href)) === false) return
if (!inBrowser || (await changeRoute(href, options))) await loadPage(href) if (!inBrowser || (await changeRoute(href, options))) await loadPage(href)
await (router.onAfterRouteChange ?? router.onAfterRouteChanged)?.(href) syncRouteQueryAndHash()
await router.onAfterRouteChange?.(href)
} }
} }
@ -165,6 +164,11 @@ export function createRouter(
} }
} }
function syncRouteQueryAndHash() {
route.query = location.search
route.hash = location.hash
}
if (inBrowser) { if (inBrowser) {
if (history.state === null) history.replaceState({}, '') if (history.state === null) history.replaceState({}, '')
window.addEventListener( window.addEventListener(
@ -216,11 +220,13 @@ export function createRouter(
if (e.state === null) return if (e.state === null) return
const href = normalizeHref(location.href) const href = normalizeHref(location.href)
await loadPage(href, (e.state && e.state.scrollPosition) || 0) await loadPage(href, (e.state && e.state.scrollPosition) || 0)
await (router.onAfterRouteChange ?? router.onAfterRouteChanged)?.(href) syncRouteQueryAndHash()
await router.onAfterRouteChange?.(href)
}) })
window.addEventListener('hashchange', (e) => { window.addEventListener('hashchange', (e) => {
e.preventDefault() e.preventDefault()
syncRouteQueryAndHash()
}) })
} }

Loading…
Cancel
Save