MCPcopy
hub / github.com/nuxt/nuxt / buildAsyncData

Function buildAsyncData

packages/nuxt/src/app/composables/asyncData.ts:711–884  ·  view source on GitHub ↗
(nuxtApp: NuxtApp, key: string, _handler: AsyncDataHandler<ResT>, options: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, initialCachedData?: NoInfer<DataT>)

Source from the content-addressed store, hash-verified

709export type CreatedAsyncData<ResT, NuxtErrorDataT = unknown, DataT = ResT, DefaultT = undefined> = Omit<_AsyncData<DataT | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>)>, 'clear' | 'refresh'> & { _off: () => void, _hash?: Record<string, string | undefined>, _default: () => unknown, _init: boolean, _deps: number, _execute: DebouncedReturn<[opts?: AsyncDataExecuteOptions | undefined], void>, _abortController?: AbortController }
710
711function buildAsyncData<
712 ResT,
713 NuxtErrorDataT = unknown,
714 DataT = ResT,
715 PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
716 DefaultT = undefined,
717> (nuxtApp: NuxtApp, key: string, _handler: AsyncDataHandler<ResT>, options: AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, initialCachedData?: NoInfer<DataT>): CreatedAsyncData<ResT, NuxtErrorDataT, DataT, DefaultT> {
718 nuxtApp.payload._errors[key] ??= undefined
719
720 const hasCustomGetCachedData = options.getCachedData !== getDefaultCachedData
721
722 // When prerendering, share payload data automatically between requests
723 const handler: AsyncDataHandler<ResT> = import.meta.client || !import.meta.prerender || !nuxtApp.ssrContext?.['~sharedPrerenderCache']
724 ? _handler
725 : (nuxtApp, options) => {
726 const value = nuxtApp.ssrContext!['~sharedPrerenderCache']!.get(key)
727 if (value) { return value as Promise<ResT> }
728
729 const promise = Promise.resolve().then(() => nuxtApp.runWithContext(() => _handler(nuxtApp, options)))
730
731 nuxtApp.ssrContext!['~sharedPrerenderCache']!.set(key, promise)
732 return promise
733 }
734
735 const _ref = options.deep ? ref : shallowRef
736 const hasCachedData = initialCachedData !== undefined
737 const unsubRefreshAsyncData = nuxtApp.hook('app:data:refresh', async (keys) => {
738 if (!keys || keys.includes(key)) {
739 await asyncData.execute({ cause: 'refresh:hook' })
740 }
741 })
742 const asyncData: CreatedAsyncData<ResT, NuxtErrorDataT, DataT, DefaultT> = {
743 data: _ref(hasCachedData ? initialCachedData : options.default!()) as any,
744 pending: pendingWhenIdle ? shallowRef(!hasCachedData) : computed(() => asyncData.status.value === 'pending'),
745 error: toRef(nuxtApp.payload._errors, key) as any,
746 status: shallowRef('idle'),
747 execute: (...args) => {
748 const [_opts, newValue = undefined] = args
749 const opts = _opts && newValue === undefined && typeof _opts === 'object' ? _opts : {}
750 if (import.meta.dev && newValue !== undefined && (!_opts || typeof _opts !== 'object')) {
751 // @ts-expect-error private property
752 console.warn(`[nuxt] [${options._functionName}] Do not pass \`execute\` directly to \`watch\`. Instead, use an inline function, such as \`watch(q, () => execute())\`.`)
753 }
754 if (nuxtApp._asyncDataPromises[key]) {
755 if ((opts.dedupe ?? options.dedupe) === 'defer') {
756 // Avoid fetching same key more than once at a time
757 return nuxtApp._asyncDataPromises[key]!
758 }
759 }
760 // Avoid fetching same key that is already fetched
761 if (granularCachedData || opts.cause === 'initial' || nuxtApp.isHydrating) {
762 const cachedData = 'cachedData' in opts ? opts.cachedData : options.getCachedData!(key, nuxtApp, { cause: opts.cause ?? 'refresh:manual' })
763 if (cachedData !== undefined) {
764 nuxtApp.payload.data[key] = asyncData.data.value = cachedData as DataT
765 asyncData.error.value = undefined
766 asyncData.status.value = 'success'
767 return Promise.resolve(cachedData)
768 }

Callers 2

createInitialFetchFunction · 0.85
useAsyncDataFunction · 0.85

Calls 12

getUserCallerFunction · 0.90
createErrorFunction · 0.90
mergeAbortSignalsFunction · 0.85
pickFunction · 0.85
createHashFunction · 0.85
clearNuxtDataByKeyFunction · 0.85
getMethod · 0.80
resolveMethod · 0.80
setMethod · 0.80
abortMethod · 0.80
addEventListenerMethod · 0.80
handlerFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…