From e21f68140c13c294a1a5a1bd4641e6c0bf3d1066 Mon Sep 17 00:00:00 2001 From: xjccc <546534045@qq.com> Date: Mon, 15 Jul 2024 23:26:30 +0800 Subject: [PATCH] fix(nuxt): only warn when `useAsyncData` returns undefined (#28154) --- packages/nuxt/src/app/composables/asyncData.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 2a68ac0932..80ad7c4756 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -253,16 +253,16 @@ export function useAsyncData< } // 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 if (!nuxtApp._asyncData[key] || !options.immediate) { nuxtApp.payload._errors[key] ??= asyncDataDefaults.errorValue const _ref = options.deep ? ref : shallowRef - + const cachedData = options.getCachedData!(key, nuxtApp) nuxtApp._asyncData[key] = { - data: _ref(options.getCachedData!(key, nuxtApp) ?? options.default!()), + data: _ref(typeof cachedData !== 'undefined' ? cachedData : options.default!()), pending: ref(!hasCachedData()), error: toRef(nuxtApp.payload._errors, key), status: ref('idle'), @@ -311,9 +311,9 @@ export function useAsyncData< 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 - 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