@@ -168,6 +168,8 @@ export function defineColadaLoader<Data>(
168
168
// set the current context before loading so nested loaders can use it
169
169
setCurrentContext ( [ entry , router , to ] )
170
170
171
+ const app = router [ APP_KEY ]
172
+
171
173
if ( ! entry . ext ) {
172
174
// console.log(`🚀 creating query for "${key}"`)
173
175
entry . ext = useQuery ( {
@@ -185,9 +187,13 @@ export function defineColadaLoader<Data>(
185
187
}
186
188
)
187
189
188
- return loader ( trackedRoute , {
189
- signal : route . meta [ ABORT_CONTROLLER_KEY ] ?. signal ,
190
- } )
190
+ // needed for automatic refetching and nested loaders
191
+ // https://github.com/posva/unplugin-vue-router/issues/583
192
+ return app . runWithContext ( ( ) =>
193
+ loader ( trackedRoute , {
194
+ signal : route . meta [ ABORT_CONTROLLER_KEY ] ?. signal ,
195
+ } )
196
+ )
191
197
} ,
192
198
key : ( ) => toValueWithParameters ( options . key , entry . route . value ) ,
193
199
// TODO: cleanup if gc
@@ -380,6 +386,7 @@ export function defineColadaLoader<Data>(
380
386
// fallback to the global router and routes for useDataLoaders used within components
381
387
const router = _router || useRouter ( )
382
388
const route = _route || ( useRoute ( ) as RouteLocationNormalizedLoaded )
389
+ const app = router [ APP_KEY ]
383
390
384
391
const entries = router [
385
392
LOADER_ENTRIES_KEY
@@ -399,7 +406,7 @@ export function defineColadaLoader<Data>(
399
406
// console.log(
400
407
// `🔁 loading from useData for "${options.key}": "${route.fullPath}"`
401
408
// )
402
- router [ APP_KEY ] . runWithContext ( ( ) =>
409
+ app . runWithContext ( ( ) =>
403
410
// in this case we always need to run the functions for nested loaders consistency
404
411
load ( route , router , undefined , parentEntry , true )
405
412
)
@@ -447,22 +454,22 @@ export function defineColadaLoader<Data>(
447
454
error,
448
455
isLoading,
449
456
reload : ( to : RouteLocationNormalizedLoaded = router . currentRoute . value ) =>
450
- router [ APP_KEY ] . runWithContext ( ( ) =>
451
- load ( to , router , undefined , undefined , true )
452
- ) . then ( ( ) => entry ! . commit ( to ) ) ,
457
+ app
458
+ . runWithContext ( ( ) => load ( to , router , undefined , undefined , true ) )
459
+ . then ( ( ) => entry ! . commit ( to ) ) ,
453
460
// pinia colada
454
461
refetch : (
455
462
to : RouteLocationNormalizedLoaded = router . currentRoute . value
456
463
) =>
457
- router [ APP_KEY ] . runWithContext ( ( ) =>
458
- load ( to , router , undefined , undefined , true )
459
- ) . then ( ( ) => ( entry ! . commit ( to ) , entry ! . ext ! . state . value ) ) ,
464
+ app
465
+ . runWithContext ( ( ) => load ( to , router , undefined , undefined , true ) )
466
+ . then ( ( ) => ( entry ! . commit ( to ) , entry ! . ext ! . state . value ) ) ,
460
467
refresh : (
461
468
to : RouteLocationNormalizedLoaded = router . currentRoute . value
462
469
) =>
463
- router [ APP_KEY ] . runWithContext ( ( ) => load ( to , router ) ) . then (
464
- ( ) => ( entry ! . commit ( to ) , entry . ext ! . state . value )
465
- ) ,
470
+ app
471
+ . runWithContext ( ( ) => load ( to , router ) )
472
+ . then ( ( ) => ( entry ! . commit ( to ) , entry . ext ! . state . value ) ) ,
466
473
status : ext ! . status ,
467
474
asyncStatus : ext ! . asyncStatus ,
468
475
state : ext ! . state ,
0 commit comments