feat(nuxt): show source file when warning about undefined useAsyncData (#31144)

This commit is contained in:
xjccc 2025-03-01 14:40:10 +08:00 committed by GitHub
parent 0a69213358
commit c398ce2817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -206,6 +206,7 @@ export default createConfigForNuxt({
'ohash/utils',
'pathe',
'uncrypto',
'errx', /* only used in dev */
// internal deps
'nuxt/app',
].map(r => `!${r}`),

View File

@ -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