MCPcopy
hub / github.com/nuxt/nuxt / useAsyncData

Function useAsyncData

packages/nuxt/src/app/composables/asyncData.ts:275–560  ·  view source on GitHub ↗
(...args: any[])

Source from the content-addressed store, hash-verified

273 opts?: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>,
274 ): AsyncData<PickFrom<DataT, [Array<never>] extends [FPickKeys] ? PickKeys : FPickKeys & KeysOf<DataT>> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined>
275 function useAsyncData<
276 ResT,
277 NuxtErrorDataT = unknown,
278 DataT = ResT,
279 PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
280 DefaultT = undefined,
281 > (...args: any[]): AsyncData<PickFrom<DataT, PickKeys>, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | undefined> {
282 const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
283 if (_isAutoKeyNeeded(args[0], args[1])) { args.unshift(autoKey) }
284
285 // eslint-disable-next-line prefer-const
286 let [_key, _handler, opts = {}] = args as [MaybeRefOrGetter<string>, AsyncDataHandler<ResT>, AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>]
287 let keyChanging = false
288 /** True if key is a Ref or getter; false for static string. When false, key watcher is skipped. */
289 const isKeyReactive = isRef(_key) || typeof _key === 'function'
290
291 // Validate arguments
292 const key = (isKeyReactive ? computed(() => toValue(_key)!) : { value: _key as string }) as { readonly value: string }
293 if (!key.value || typeof key.value !== 'string') {
294 throw new TypeError('[nuxt] [useAsyncData] key must be a non-empty string.')
295 }
296 if (typeof _handler !== 'function') {
297 throw new TypeError('[nuxt] [useAsyncData] handler must be a function.')
298 }
299
300 const shouldFactoryOptionsOverride = typeof options === 'function'
301
302 // Setup nuxt instance payload
303 const nuxtApp = useNuxtApp()
304
305 const factoryOptions = shouldFactoryOptionsOverride ? options(opts as any) : options
306 // assign factory defaults
307 if (!shouldFactoryOptionsOverride) {
308 for (const key in factoryOptions) {
309 // factory doesn't have a value set for the key
310 if (factoryOptions[key as keyof typeof factoryOptions] === undefined) { continue }
311 // opts already has a value set for the key
312 if (opts[key as keyof typeof opts] !== undefined) { continue }
313 opts[key as keyof typeof opts] = factoryOptions[key as keyof typeof factoryOptions] as any
314 }
315 }
316
317 opts.server ??= true
318 opts.default ??= getDefault as () => DefaultT
319 opts.getCachedData ??= getDefaultCachedData
320
321 opts.lazy ??= false
322 opts.immediate ??= true
323 opts.deep ??= asyncDataDefaults.deep
324 opts.dedupe ??= 'cancel'
325
326 // assign overrides from factory
327 if (shouldFactoryOptionsOverride) {
328 for (const key in factoryOptions) {
329 if (factoryOptions[key as keyof typeof factoryOptions] === undefined) { continue }
330 opts[key as keyof typeof opts] = factoryOptions[key as keyof typeof factoryOptions] as any
331 }
332 }

Callers 6

runLegacyAsyncDataFunction · 0.90
useFetchFunction · 0.90
setupFunction · 0.90
useCustomDataFunction · 0.90
testAsyncDataFunction · 0.90

Calls 13

useNuxtAppFunction · 0.90
getUserCallerFunction · 0.90
_isAutoKeyNeededFunction · 0.85
createHashFunction · 0.85
createInitialFetchFunction · 0.85
watchFunction · 0.85
buildAsyncDataFunction · 0.85
unregisterFunction · 0.85
writableComputedRefFunction · 0.85
clearNuxtDataByKeyFunction · 0.85
toStringMethod · 0.80
abortMethod · 0.80

Tested by 3

setupFunction · 0.72
useCustomDataFunction · 0.72
testAsyncDataFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…