fix(nuxt): only warn when useAsyncData returns undefined (#28154)

This commit is contained in:
xjccc 2024-07-15 23:26:30 +08:00 committed by GitHub
parent 48a8b18083
commit 494871062c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -250,16 +250,16 @@ export function useAsyncData<
} }
// TODO: make more precise when v4 lands // TODO: make more precise when v4 lands
const hasCachedData = () => options.getCachedData!(key, nuxtApp) != null const hasCachedData = () => options.getCachedData!(key, nuxtApp) !== undefined
// Create or use a shared asyncData entity // Create or use a shared asyncData entity
if (!nuxtApp._asyncData[key] || !options.immediate) { if (!nuxtApp._asyncData[key] || !options.immediate) {
nuxtApp.payload._errors[key] ??= undefined nuxtApp.payload._errors[key] ??= undefined
const _ref = options.deep ? ref : shallowRef const _ref = options.deep ? ref : shallowRef
const cachedData = options.getCachedData!(key, nuxtApp)
nuxtApp._asyncData[key] = { nuxtApp._asyncData[key] = {
data: _ref(options.getCachedData!(key, nuxtApp) ?? options.default!()), data: _ref(typeof cachedData !== 'undefined' ? cachedData : options.default!()),
pending: ref(!hasCachedData()), pending: ref(!hasCachedData()),
error: toRef(nuxtApp.payload._errors, key), error: toRef(nuxtApp.payload._errors, key),
status: ref('idle'), status: ref('idle'),
@ -308,9 +308,9 @@ export function useAsyncData<
result = pick(result as any, options.pick) as DataT result = pick(result as any, options.pick) as DataT
} }
if (import.meta.dev && import.meta.server && !result) { if (import.meta.dev && import.meta.server && typeof result === 'undefined') {
// @ts-expect-error private property // @ts-expect-error private property
console.warn(`[nuxt] \`${options._functionName || 'useAsyncData'}\` must return a truthy value (for example, it should not be \`undefined\`, \`null\` or empty string) or the request may be duplicated on the client side.`) console.warn(`[nuxt] \`${options._functionName || 'useAsyncData'}\` must return a value (it should not be \`undefined\`) or the request may be duplicated on the client side.`)
} }
nuxtApp.payload.data[key] = result nuxtApp.payload.data[key] = result