Skip to content

Commit c5d77ae

Browse files
committed
chore: update resolveNavigationGuards logic
1 parent be8a320 commit c5d77ae

File tree

1 file changed

+15
-1
lines changed
  • packages/router/src/navigation-api

1 file changed

+15
-1
lines changed

packages/router/src/navigation-api/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
136136
const [leavingRecords, updatingRecords, enteringRecords] =
137137
extractChangingRecords(to, from)
138138

139+
// run the queue of per route beforeRouteLeave guards
139140
let guards = extractComponentsGuards(
140141
leavingRecords.reverse(),
141142
'beforeRouteLeave',
@@ -155,20 +156,25 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
155156

156157
await runGuardQueue(guards)
157158

159+
// check global guards beforeEach
158160
guards = []
159161
for (const guard of beforeGuards.list()) {
160162
guards.push(guardToPromiseFn(guard, to, from, { info: navigationInfo }))
161163
}
164+
guards.push(canceledNavigationCheck)
162165
await runGuardQueue(guards)
163166

167+
// check in components beforeRouteUpdate
164168
guards = extractComponentsGuards(
165169
updatingRecords,
166170
'beforeRouteUpdate',
167171
to,
168172
from
169173
)
174+
guards.push(canceledNavigationCheck)
170175
await runGuardQueue(guards)
171176

177+
// check the route beforeEnter
172178
guards = []
173179
for (const record of enteringRecords) {
174180
if (record.beforeEnter) {
@@ -186,23 +192,31 @@ export function createNavigationApiRouter(options: RouterApiOptions): Router {
186192
}
187193
}
188194
}
195+
guards.push(canceledNavigationCheck)
189196
await runGuardQueue(guards)
190197

198+
// NOTE: at this point to.matched is normalized and does not contain any () => Promise<Component>
199+
// clear existing enterCallbacks, these are added by extractComponentsGuards
200+
to.matched.forEach(record => (record.enterCallbacks = {}))
201+
191202
// Resolve async components and run beforeRouteEnter
192203
guards = extractComponentsGuards(
193204
enteringRecords,
194205
'beforeRouteEnter',
195206
to,
196207
from,
197-
undefined,
208+
runWithContext,
198209
navigationInfo
199210
)
211+
guards.push(canceledNavigationCheck)
200212
await runGuardQueue(guards)
201213

214+
// check global guards beforeResolve
202215
guards = []
203216
for (const guard of beforeResolveGuards.list()) {
204217
guards.push(guardToPromiseFn(guard, to, from, { info: navigationInfo }))
205218
}
219+
guards.push(canceledNavigationCheck)
206220
await runGuardQueue(guards)
207221
}
208222

0 commit comments

Comments
 (0)