From c398ce28173c673a2dac844900940c5ba49374d9 Mon Sep 17 00:00:00 2001 From: xjccc <546534045@qq.com> Date: Sat, 1 Mar 2025 14:40:10 +0800 Subject: [PATCH] feat(nuxt): show source file when warning about undefined useAsyncData (#31144) --- eslint.config.mjs | 1 + packages/nuxt/src/app/composables/asyncData.ts | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 7e1f5f3e11..61a0b25a6c 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -206,6 +206,7 @@ export default createConfigForNuxt({ 'ohash/utils', 'pathe', 'uncrypto', + 'errx', /* only used in dev */ // internal deps 'nuxt/app', ].map(r => `!${r}`), diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index f12244179c..78ca6e06f2 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -1,5 +1,6 @@ import { computed, getCurrentInstance, getCurrentScope, onBeforeMount, onScopeDispose, onServerPrefetch, onUnmounted, ref, shallowRef, toRef, unref, watch } from 'vue' import type { MultiWatchSources, Ref } from 'vue' +import { captureStackTrace } from 'errx' import type { NuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt' import { toArray } from '../utils' @@ -301,8 +302,11 @@ export function useAsyncData< } if (import.meta.dev && import.meta.server && typeof result === 'undefined') { + const stack = captureStackTrace() + const { source, line, column } = stack[stack.length - 1] ?? {} + const explanation = source ? ` (used at ${source.replace(/^file:\/\//, '')}:${line}:${column})` : '' // @ts-expect-error private property - 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.`) + console.warn(`[nuxt] \`${options._functionName || 'useAsyncData'}${explanation}\` must return a value (it should not be \`undefined\`) or the request may be duplicated on the client side.`) } nuxtApp.payload.data[key] = result