fix(nuxt): initialise asyncData errors with null (#23428)

This commit is contained in:
Damian Głowala 2023-09-27 15:43:53 +02:00 committed by GitHub
parent fb26a160f5
commit 7005a66d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View File

@ -146,6 +146,8 @@ export function useAsyncData<
// Create or use a shared asyncData entity // Create or use a shared asyncData entity
if (!nuxt._asyncData[key] || !options.immediate) { if (!nuxt._asyncData[key] || !options.immediate) {
nuxt.payload._errors[key] ??= null
nuxt._asyncData[key] = { nuxt._asyncData[key] = {
data: ref(getCachedData() ?? options.default!()), data: ref(getCachedData() ?? options.default!()),
pending: ref(!hasCachedData()), pending: ref(!hasCachedData()),
@ -153,6 +155,7 @@ export function useAsyncData<
status: ref('idle') status: ref('idle')
} }
} }
// TODO: Else, somehow check for conflicting keys with different defaults or fetcher // TODO: Else, somehow check for conflicting keys with different defaults or fetcher
const asyncData = { ...nuxt._asyncData[key] } as AsyncData<DataT | DefaultT, DataE> const asyncData = { ...nuxt._asyncData[key] } as AsyncData<DataT | DefaultT, DataE>
@ -373,11 +376,11 @@ export function clearNuxtData (keys?: string | string[] | ((key: string) => bool
nuxtApp.payload.data[key] = undefined nuxtApp.payload.data[key] = undefined
} }
if (key in nuxtApp.payload._errors) { if (key in nuxtApp.payload._errors) {
nuxtApp.payload._errors[key] = undefined nuxtApp.payload._errors[key] = null
} }
if (nuxtApp._asyncData[key]) { if (nuxtApp._asyncData[key]) {
nuxtApp._asyncData[key]!.data.value = undefined nuxtApp._asyncData[key]!.data.value = undefined
nuxtApp._asyncData[key]!.error.value = undefined nuxtApp._asyncData[key]!.error.value = null
nuxtApp._asyncData[key]!.pending.value = false nuxtApp._asyncData[key]!.pending.value = false
nuxtApp._asyncData[key]!.status.value = 'idle' nuxtApp._asyncData[key]!.status.value = 'idle'
} }

View File

@ -81,7 +81,7 @@ export interface NuxtPayload {
description: string description: string
data?: any data?: any
} | null } | null
_errors: Record<string, NuxtError | undefined> _errors: Record<string, NuxtError | null>
[key: string]: unknown [key: string]: unknown
} }
@ -104,7 +104,7 @@ interface _NuxtApp {
_asyncData: Record<string, { _asyncData: Record<string, {
data: Ref<any> data: Ref<any>
pending: Ref<boolean> pending: Ref<boolean>
error: Ref<any> error: Ref<Error | null>
status: Ref<AsyncDataRequestStatus> status: Ref<AsyncDataRequestStatus>
} | undefined> } | undefined>

View File

@ -125,6 +125,14 @@ describe('useAsyncData', () => {
expect(pending.value).toBe(false) expect(pending.value).toBe(false)
}) })
// https://github.com/nuxt/nuxt/issues/23411
it('should initialize with error set to null when immediate: false', async () => {
const { error, execute } = useAsyncData(() => ({}), { immediate: false })
expect(error.value).toBe(null)
await execute()
expect(error.value).toBe(null)
})
it('should be accessible with useNuxtData', async () => { it('should be accessible with useNuxtData', async () => {
await useAsyncData('key', () => Promise.resolve('test')) await useAsyncData('key', () => Promise.resolve('test'))
const data = useNuxtData('key') const data = useNuxtData('key')