Skip to content

Commit 5c14ca8

Browse files
committed
handle error while collecting loaders
1 parent 4848247 commit 5c14ca8

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/data-loaders/navigation-guard.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,38 @@ describe('navigation-guard', () => {
203203
}
204204
})
205205

206+
it('collects all loaders from lazy loaded pages when first navigation fails', async () => {
207+
setupApp({ isSSR: false })
208+
const router = getRouter()
209+
210+
let first = true
211+
router.addRoute({
212+
name: '_test',
213+
path: '/fetch',
214+
component: async () => {
215+
if (first) {
216+
first = false
217+
218+
throw new Error('Failed to load component')
219+
}
220+
221+
return import('../../tests/data-loaders/ComponentWithLoader.vue')
222+
},
223+
})
224+
225+
const firstNavPromise = router.push('/fetch')
226+
await expect(firstNavPromise).rejects.toThrow(Error)
227+
228+
await router.push('/fetch')
229+
230+
const set = router.currentRoute.value.meta[LOADER_SET_KEY]
231+
expect([...set!]).toEqual([useDataOne, useDataTwo])
232+
233+
for (const record of router.currentRoute.value.matched) {
234+
expect(record.meta[LOADER_SET_PROMISES_KEY]).toBeUndefined()
235+
}
236+
})
237+
206238
it('awaits for all loaders to be resolved', async () => {
207239
setupApp({ isSSR: false })
208240
const router = getRouter()

src/data-loaders/navigation-guard.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,29 @@ export function setupLoaderGuard({
157157
}
158158
}
159159

160-
return Promise.all(lazyLoadingPromises).then(() => {
161-
// group all the loaders in a single set
162-
for (const record of to.matched) {
163-
// merge the whole set of loaders
164-
for (const loader of record.meta[LOADER_SET_KEY]!) {
165-
to.meta[LOADER_SET_KEY]!.add(loader)
160+
return Promise.all(lazyLoadingPromises)
161+
.then(() => {
162+
// group all the loaders in a single set
163+
for (const record of to.matched) {
164+
// merge the whole set of loaders
165+
for (const loader of record.meta[LOADER_SET_KEY]!) {
166+
to.meta[LOADER_SET_KEY]!.add(loader)
167+
}
168+
record.meta[LOADER_SET_PROMISES_KEY] = undefined
166169
}
167-
record.meta[LOADER_SET_PROMISES_KEY] = undefined
168-
}
169-
// we return nothing to remove the value to allow the navigation
170-
// same as return true
171-
})
170+
// we return nothing to remove the value to allow the navigation
171+
// same as return true
172+
})
173+
.catch((error) => {
174+
// If error happens while collecting loaders, reset them
175+
// so on next navigation we can try again
176+
for (const record of to.matched) {
177+
record.meta[LOADER_SET_KEY] = undefined
178+
record.meta[LOADER_SET_PROMISES_KEY] = undefined
179+
}
180+
181+
throw error
182+
})
172183
})
173184

174185
const removeDataLoaderGuard = router.beforeResolve((to, from) => {

0 commit comments

Comments
 (0)