@@ -109,6 +109,9 @@ export interface AsyncDataExecuteOptions {
109
109
dedupe ?: 'cancel' | 'defer'
110
110
111
111
cause ?: AsyncDataRefreshCause
112
+
113
+ /** @internal */
114
+ cachedData ?: any
112
115
}
113
116
114
117
export interface _AsyncData < DataT , ErrorT > {
@@ -245,15 +248,16 @@ export function useAsyncData<
245
248
}
246
249
247
250
// Create or use a shared asyncData entity
248
- const initialCachedData = options . getCachedData ! ( key . value , nuxtApp , { cause : 'initial' } )
251
+ const initialFetchOptions : AsyncDataExecuteOptions = { cause : 'initial' , dedupe : options . dedupe }
249
252
if ( ! nuxtApp . _asyncData [ key . value ] ?. _init ) {
250
- nuxtApp . _asyncData [ key . value ] = createAsyncData ( nuxtApp , key . value , _handler , options , initialCachedData )
253
+ initialFetchOptions . cachedData = options . getCachedData ! ( key . value , nuxtApp , { cause : 'initial' } )
254
+ nuxtApp . _asyncData [ key . value ] = createAsyncData ( nuxtApp , key . value , _handler , options , initialFetchOptions . cachedData )
251
255
}
252
256
const asyncData = nuxtApp . _asyncData [ key . value ] !
253
257
254
258
asyncData . _deps ++
255
259
256
- const initialFetch = ( ) => nuxtApp . _asyncData [ key . value ] ! . execute ( { cause : 'initial' , dedupe : options . dedupe } )
260
+ const initialFetch = ( ) => nuxtApp . _asyncData [ key . value ] ! . execute ( initialFetchOptions )
257
261
258
262
const fetchOnServer = options . server !== false && nuxtApp . payload . serverRendered
259
263
@@ -292,7 +296,7 @@ export function useAsyncData<
292
296
293
297
const isWithinClientOnly = instance && ( instance . _nuxtClientOnly || inject ( clientOnlySymbol , false ) )
294
298
295
- if ( fetchOnServer && nuxtApp . isHydrating && ( asyncData . error . value || typeof initialCachedData !== 'undefined' ) ) {
299
+ if ( fetchOnServer && nuxtApp . isHydrating && ( asyncData . error . value || asyncData . data . value !== asyncDataDefaults . value ) ) {
296
300
// 1. Hydration (server: true): no fetch
297
301
if ( pendingWhenIdle ) {
298
302
asyncData . pending . value = false
@@ -333,12 +337,14 @@ export function useAsyncData<
333
337
if ( oldKey ) {
334
338
unregister ( oldKey )
335
339
}
340
+ const initialFetchOptions : AsyncDataExecuteOptions = { cause : 'initial' , dedupe : options . dedupe }
336
341
if ( ! nuxtApp . _asyncData [ newKey ] ?. _init ) {
337
- nuxtApp . _asyncData [ newKey ] = createAsyncData ( nuxtApp , newKey , _handler , options , options . getCachedData ! ( newKey , nuxtApp , { cause : 'initial' } ) )
342
+ initialFetchOptions . cachedData = options . getCachedData ! ( newKey , nuxtApp , { cause : 'initial' } )
343
+ nuxtApp . _asyncData [ newKey ] = createAsyncData ( nuxtApp , newKey , _handler , options , initialFetchOptions . cachedData )
338
344
}
339
345
nuxtApp . _asyncData [ newKey ] . _deps ++
340
346
if ( options . immediate || hasRun ) {
341
- nuxtApp . _asyncData [ newKey ] . execute ( { cause : 'initial' , dedupe : options . dedupe } )
347
+ nuxtApp . _asyncData [ newKey ] . execute ( initialFetchOptions )
342
348
}
343
349
} , { flush : 'sync' } )
344
350
@@ -526,7 +532,7 @@ export function clearNuxtData (keys?: string | string[] | ((key: string) => bool
526
532
527
533
function clearNuxtDataByKey ( nuxtApp : NuxtApp , key : string ) : void {
528
534
if ( key in nuxtApp . payload . data ) {
529
- nuxtApp . payload . data [ key ] = undefined
535
+ nuxtApp . payload . data [ key ] = asyncDataDefaults . value
530
536
}
531
537
532
538
if ( key in nuxtApp . payload . _errors ) {
@@ -588,7 +594,7 @@ function createAsyncData<
588
594
}
589
595
590
596
const _ref = options . deep ? ref : shallowRef
591
- const hasCachedData = typeof initialCachedData !== 'undefined'
597
+ const hasCachedData = initialCachedData !== asyncDataDefaults . value
592
598
const unsubRefreshAsyncData = nuxtApp . hook ( 'app:data:refresh' , async ( keys ) => {
593
599
if ( ! keys || keys . includes ( key ) ) {
594
600
await asyncData . execute ( { cause : 'refresh:hook' } )
@@ -609,9 +615,9 @@ function createAsyncData<
609
615
}
610
616
// Avoid fetching same key that is already fetched
611
617
if ( granularCachedData || opts . cause === 'initial' || nuxtApp . isHydrating ) {
612
- const cachedData = opts . cause === 'initial' ? initialCachedData : options . getCachedData ! ( key , nuxtApp , { cause : opts . cause ?? 'refresh:manual' } )
613
- if ( typeof cachedData !== 'undefined' ) {
614
- nuxtApp . payload . data [ key ] = asyncData . data . value = cachedData
618
+ const cachedData = 'cachedData' in opts ? opts . cachedData : options . getCachedData ! ( key , nuxtApp , { cause : opts . cause ?? 'refresh:manual' } )
619
+ if ( cachedData !== asyncDataDefaults . value ) {
620
+ nuxtApp . payload . data [ key ] = asyncData . data . value = cachedData as DataT
615
621
asyncData . error . value = asyncDataDefaults . errorValue
616
622
asyncData . status . value = 'success'
617
623
return Promise . resolve ( cachedData )
0 commit comments