@@ -12,9 +12,26 @@ export interface Route {
12
12
}
13
13
14
14
export interface Router {
15
+ /**
16
+ * Current route.
17
+ */
15
18
route : Route
16
- go : ( href ?: string ) => Promise < void >
17
- onBeforeRouteChange ?: ( to : string ) => Awaitable < void >
19
+ /**
20
+ * Navigate to a new URL.
21
+ */
22
+ go : ( to ?: string ) => Promise < void >
23
+ /**
24
+ * Called before the route changes. Return `false` to cancel the navigation.
25
+ */
26
+ onBeforeRouteChange ?: ( to : string ) => Awaitable < void | boolean >
27
+ /**
28
+ * Called before the page component is loaded (after the history state is
29
+ * updated). Return `false` to cancel the navigation.
30
+ */
31
+ onBeforePageLoad ?: ( to : string ) => Awaitable < void | boolean >
32
+ /**
33
+ * Called after the route changes.
34
+ */
18
35
onAfterRouteChanged ?: ( to : string ) => Awaitable < void >
19
36
}
20
37
@@ -47,7 +64,7 @@ export function createRouter(
47
64
}
48
65
49
66
async function go ( href : string = inBrowser ? location . href : '/' ) {
50
- await router . onBeforeRouteChange ?.( href )
67
+ if ( ( await router . onBeforeRouteChange ?.( href ) ) === false ) return
51
68
const url = new URL ( href , fakeHost )
52
69
if ( ! siteDataRef . value . cleanUrls ) {
53
70
// ensure correct deep link so page refresh lands on correct files.
@@ -62,6 +79,7 @@ export function createRouter(
62
79
history . replaceState ( { scrollPosition : window . scrollY } , document . title )
63
80
history . pushState ( null , '' , href )
64
81
}
82
+ if ( ( await router . onBeforePageLoad ?.( href ) ) === false ) return
65
83
await loadPage ( href )
66
84
await router . onAfterRouteChanged ?.( href )
67
85
}
0 commit comments