-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
navigation-guard.ts
252 lines (231 loc) · 8.01 KB
/
navigation-guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type { Router } from 'vue-router'
import { effectScope, type App, type EffectScope } from 'vue'
import {
ABORT_CONTROLLER_KEY,
APP_KEY,
LOADER_ENTRIES_KEY,
LOADER_SET_KEY,
PENDING_LOCATION_KEY,
} from './symbols'
import { IS_CLIENT, isDataLoader, setCurrentContext } from './utils'
import { isNavigationFailure } from 'vue-router'
/**
* Setups the different Navigation Guards to collect the data loaders from the route records and then to execute them.
*
* @param router - the router instance
* @returns
*/
export function setupLoaderGuard(
router: Router,
app: App,
effect: EffectScope
) {
// avoid creating the guards multiple times
if (router[LOADER_ENTRIES_KEY] != null) {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'[vue-router]: Data fetching was setup twice. Make sure to setup only once.'
)
}
return () => {}
}
// Access to the entries map for convenience
router[LOADER_ENTRIES_KEY] = new WeakMap()
// Access to `app.runWithContext()`
router[APP_KEY] = app
// guard to add the loaders to the meta property
const removeLoaderGuard = router.beforeEach((to) => {
// global pending location, used by nested loaders to know if they should load or not
router[PENDING_LOCATION_KEY] = to
// Differently from records, this one is reset on each navigation
// so it must be built each time
to.meta[LOADER_SET_KEY] = new Set()
// reference the loader entries map for convenience
to.meta[LOADER_ENTRIES_KEY] = router[LOADER_ENTRIES_KEY]
// adds an abort controller that can pass a signal to loaders
to.meta[ABORT_CONTROLLER_KEY] = new AbortController()
// Collect all the lazy loaded components to await them in parallel
const lazyLoadingPromises = []
for (const record of to.matched) {
if (!record.meta[LOADER_SET_KEY]) {
// setup an empty array to skip the check next time
record.meta[LOADER_SET_KEY] = new Set(record.meta.loaders || [])
// add all the loaders from the components to the set
for (const componentName in record.components) {
const component: unknown = record.components[componentName]
// we only add async modules because otherwise the component doesn't have any loaders and the user should add
// them with the `loaders` array
if (isAsyncModule(component)) {
const promise = component().then(
(viewModule: Record<string, unknown>) => {
for (const exportName in viewModule) {
const exportValue = viewModule[exportName]
if (isDataLoader(exportValue)) {
record.meta[LOADER_SET_KEY]!.add(exportValue)
}
}
}
)
lazyLoadingPromises.push(promise)
}
}
}
}
// TODO: not use record to store loaders, or maybe cleanup here
return Promise.all(lazyLoadingPromises).then(() => {
for (const record of to.matched) {
// merge the whole set of loaders
for (const loader of record.meta[LOADER_SET_KEY]!) {
to.meta[LOADER_SET_KEY]!.add(loader)
}
}
// we return nothing to remove the value to allow the navigation
// same as return true
})
})
const removeDataLoaderGuard = router.beforeResolve((to) => {
const loaders = Array.from(to.meta[LOADER_SET_KEY] || [])
/**
* - ~~Map the loaders to an array of promises~~
* - ~~Await all the promises (parallel)~~
* - Collect NavigationResults and call `selectNavigationResult` to select the one to use
*/
// unset the context so all loaders are executed as root loaders
setCurrentContext([])
return Promise.all(
loaders.map((loader) => {
const { commit, server, lazy } = loader._.options
// do not run on the server if specified
if (!server && !IS_CLIENT) {
return
}
// keep track of loaders that should be committed after all loaders are done
const ret = app
// allows inject and provide APIs
.runWithContext(() => loader._.load(to, router))
.then(() => {
// for immediate loaders, the load function handles this
// NOTE: it would be nice to also have here the immediate commit
// but running it here is too late for nested loaders
if (commit === 'after-load') {
return loader
}
})
// on client-side, lazy loaders are not awaited, but on server they are
return IS_CLIENT && lazy
? undefined
: // return the non-lazy loader to commit changes after all loaders are done
ret
})
) // let the navigation go through by returning true or void
.then((loaders) => {
for (const loader of loaders) {
if (loader) {
// console.log(`⬇️ Committing ${loader.name}`)
loader._.getEntry(router).commit(to)
}
}
// TODO:
// reset the initial state as it can only be used once
// initialData = undefined
// NOTE: could this be dev only?
// isFetched = true
})
// no catch so errors are propagated to the router
// TODO: handle navigation failures?
})
// listen to duplicated navigation failures to reset the pendingTo and pendingLoad
// since they won't trigger the beforeEach or beforeResolve defined above
const removeAfterEach = router.afterEach((_to, _from, failure) => {
// abort the signal of a failed navigation
// we need to check if it exists because the navigation guard that creates
// the abort controller could not be triggered depending on the failure
if (failure && _to.meta[ABORT_CONTROLLER_KEY]) {
_to.meta[ABORT_CONTROLLER_KEY].abort(failure)
}
if (
isNavigationFailure(failure, 16 /* NavigationFailureType.duplicated */)
) {
if (router[PENDING_LOCATION_KEY]) {
router[PENDING_LOCATION_KEY].meta[LOADER_SET_KEY]!.forEach((loader) => {
const entry = loader._.getEntry(router)
entry.pendingTo = null
entry.pendingLoad = null
})
router[PENDING_LOCATION_KEY] = null
}
}
})
// abort the signal on thrown errors
const removeOnError = router.onError((error, to) => {
// same as with afterEach, we check if it exists because the navigation guard
// that creates the abort controller could not be triggered depending on the error
if (to.meta[ABORT_CONTROLLER_KEY]) {
to.meta[ABORT_CONTROLLER_KEY].abort(error)
}
})
return () => {
// @ts-expect-error: must be there in practice
delete router[LOADER_ENTRIES_KEY]
removeLoaderGuard()
removeDataLoaderGuard()
removeAfterEach()
removeOnError()
}
}
/**
* Allows differentiating lazy components from functional components and vue-class-component
* @internal
*
* @param component
*/
export function isAsyncModule(
asyncMod: unknown
): asyncMod is () => Promise<Record<string, unknown>> {
return (
typeof asyncMod === 'function' &&
// vue functional components
!('displayName' in asyncMod) &&
!('props' in asyncMod) &&
!('emits' in asyncMod) &&
!('__vccOpts' in asyncMod)
)
}
/**
* Data Loader plugin to add data loading support to Vue Router.
*
* @example
* ```ts
* import { createApp } from 'vue'
* import {
* createRouter,
* DataLoaderPlugin,
* createWebHistory,
* } from 'vue-router/auto'
*
* const router = createRouter({
* history: createWebHistory(),
* })
*
* const app = createApp({})
* app.use(DataLoaderPlugin, { router })
* app.use(router)
* ```
*/
export function DataLoaderPlugin(
app: App,
{ router }: DataLoaderPluginOptions
) {
const effect = effectScope(true)
const removeGuards = setupLoaderGuard(router, app, effect)
// TODO: use https://github.com/vuejs/core/pull/8801 if merged
const { unmount } = app
app.unmount = () => {
effect.stop()
removeGuards()
unmount.call(app)
}
}
export interface DataLoaderPluginOptions {
router: Router
}