File tree Expand file tree Collapse file tree 2 files changed +54
-11
lines changed Expand file tree Collapse file tree 2 files changed +54
-11
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,38 @@ describe('navigation-guard', () => {
203
203
}
204
204
} )
205
205
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
+
206
238
it ( 'awaits for all loaders to be resolved' , async ( ) => {
207
239
setupApp ( { isSSR : false } )
208
240
const router = getRouter ( )
Original file line number Diff line number Diff line change @@ -157,18 +157,29 @@ export function setupLoaderGuard({
157
157
}
158
158
}
159
159
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
166
169
}
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
+ } )
172
183
} )
173
184
174
185
const removeDataLoaderGuard = router . beforeResolve ( ( to , from ) => {
You can’t perform that action at this time.
0 commit comments