Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add onBeforePageLoad hook for router #2564

Merged
merged 7 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,27 @@ Returns the VitePress router instance so you can programmatically navigate to an

```ts
interface Router {
/**
* Current route.
*/
route: Route
go: (href?: string) => Promise<void>
/**
* Navigate to a new URL.
*/
go: (to?: string) => Promise<void>
/**
* Called before the route changes. Return `false` to cancel the navigation.
*/
onBeforeRouteChange?: (to: string) => Awaitable<void | boolean>
/**
* Called before the page component is loaded (after the history state is
* updated). Return `false` to cancel the navigation.
*/
onBeforePageLoad?: (to: string) => Awaitable<void | boolean>
/**
* Called after the route changes.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
}
```

Expand Down
24 changes: 21 additions & 3 deletions src/client/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,26 @@ export interface Route {
}

export interface Router {
/**
* Current route.
*/
route: Route
go: (href?: string) => Promise<void>
onBeforeRouteChange?: (to: string) => Awaitable<void>
/**
* Navigate to a new URL.
*/
go: (to?: string) => Promise<void>
/**
* Called before the route changes. Return `false` to cancel the navigation.
*/
onBeforeRouteChange?: (to: string) => Awaitable<void | boolean>
/**
* Called before the page component is loaded (after the history state is
* updated). Return `false` to cancel the navigation.
*/
onBeforePageLoad?: (to: string) => Awaitable<void | boolean>
/**
* Called after the route changes.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
}

Expand Down Expand Up @@ -47,7 +64,7 @@ export function createRouter(
}

async function go(href: string = inBrowser ? location.href : '/') {
await router.onBeforeRouteChange?.(href)
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.
Expand All @@ -62,6 +79,7 @@ export function createRouter(
history.replaceState({ scrollPosition: window.scrollY }, document.title)
history.pushState(null, '', href)
}
if ((await router.onBeforePageLoad?.(href)) === false) return
await loadPage(href)
await router.onAfterRouteChanged?.(href)
}
Expand Down