mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
Merge c376a1f240
into edc299a043
This commit is contained in:
commit
f9b003d9cb
@ -106,10 +106,6 @@ export interface AsyncDataExecuteOptions {
|
||||
|
||||
export interface _AsyncData<DataT, ErrorT> {
|
||||
data: Ref<DataT>
|
||||
/**
|
||||
* @deprecated Use `status` instead. This may be removed in a future major version.
|
||||
*/
|
||||
pending: Ref<boolean>
|
||||
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||
clear: () => void
|
||||
@ -249,7 +245,6 @@ export function useAsyncData<
|
||||
const _ref = options.deep ? ref : shallowRef
|
||||
nuxtApp._asyncData[key] = {
|
||||
data: _ref(hasCachedData ? initialCachedData : options.default!()),
|
||||
pending: ref(!hasCachedData),
|
||||
error: toRef(nuxtApp.payload._errors, key),
|
||||
status: ref('idle'),
|
||||
_default: options.default!,
|
||||
@ -277,7 +272,6 @@ export function useAsyncData<
|
||||
return Promise.resolve(cachedData)
|
||||
}
|
||||
}
|
||||
asyncData.pending.value = true
|
||||
asyncData.status.value = 'pending'
|
||||
// TODO: Cancel previous promise
|
||||
const promise = new Promise<ResT>(
|
||||
@ -322,8 +316,6 @@ export function useAsyncData<
|
||||
.finally(() => {
|
||||
if ((promise as any).cancelled) { return }
|
||||
|
||||
asyncData.pending.value = false
|
||||
|
||||
delete nuxtApp._asyncDataPromises[key]
|
||||
})
|
||||
nuxtApp._asyncDataPromises[key] = promise
|
||||
@ -366,7 +358,6 @@ export function useAsyncData<
|
||||
|
||||
if (fetchOnServer && nuxtApp.isHydrating && (asyncData.error.value || typeof initialCachedData !== 'undefined')) {
|
||||
// 1. Hydration (server: true): no fetch
|
||||
asyncData.pending.value = false
|
||||
asyncData.status.value = asyncData.error.value ? 'error' : 'success'
|
||||
} else if (instance && ((nuxtApp.payload.serverRendered && nuxtApp.isHydrating) || options.lazy) && options.immediate) {
|
||||
// 2. Initial load (server: false): fetch on mounted
|
||||
@ -527,7 +518,6 @@ function clearNuxtDataByKey (nuxtApp: NuxtApp, key: string): void {
|
||||
if (nuxtApp._asyncData[key]) {
|
||||
nuxtApp._asyncData[key]!.data.value = nuxtApp._asyncData[key]!._default()
|
||||
nuxtApp._asyncData[key]!.error.value = undefined
|
||||
nuxtApp._asyncData[key]!.pending.value = false
|
||||
nuxtApp._asyncData[key]!.status.value = 'idle'
|
||||
}
|
||||
|
||||
|
@ -126,10 +126,6 @@ interface _NuxtApp {
|
||||
/** @internal */
|
||||
_asyncData: Record<string, {
|
||||
data: Ref<unknown>
|
||||
/**
|
||||
* @deprecated This may be removed in a future major version.
|
||||
*/
|
||||
pending: Ref<boolean>
|
||||
error: Ref<Error | undefined>
|
||||
status: Ref<AsyncDataRequestStatus>
|
||||
/** @internal */
|
||||
|
@ -1,6 +1,6 @@
|
||||
<template>
|
||||
<div
|
||||
v-if="!pending"
|
||||
v-if="status !== 'pending'"
|
||||
v-text="'resolved:' + data.resolved"
|
||||
/>
|
||||
<div
|
||||
@ -11,5 +11,5 @@
|
||||
|
||||
<script setup>
|
||||
useNuxtData('call')
|
||||
const { data, pending } = await useAsyncData('call', () => Promise.resolve({ resolved: true }), { server: false })
|
||||
const { data, status } = await useAsyncData('call', () => Promise.resolve({ resolved: true }), { server: false })
|
||||
</script>
|
||||
|
@ -17,7 +17,7 @@ const ComponentDefinedInSetup = computed(() => h({
|
||||
template: compTemplate.value,
|
||||
}) as Component)
|
||||
|
||||
const { data, pending } = await useAsyncData('templates', async () => {
|
||||
const { data, status } = await useAsyncData('templates', async () => {
|
||||
const [interactiveComponent, templateString] = await Promise.all([
|
||||
$fetch('/api/full-component'),
|
||||
$fetch('/api/template'),
|
||||
@ -54,7 +54,7 @@ const Interactive = h({
|
||||
>
|
||||
{{ count }}
|
||||
</button>
|
||||
<template v-if="!pending">
|
||||
<template v-if="status !== 'pending'">
|
||||
<Name
|
||||
id="name"
|
||||
template="<div>I am the Name.ts component</div>"
|
||||
|
@ -131,7 +131,6 @@ describe('useAsyncData', () => {
|
||||
expect(Object.keys(res)).toMatchInlineSnapshot(`
|
||||
[
|
||||
"data",
|
||||
"pending",
|
||||
"error",
|
||||
"status",
|
||||
"execute",
|
||||
@ -149,29 +148,25 @@ describe('useAsyncData', () => {
|
||||
const immediate = await useAsyncData(() => Promise.resolve('test'))
|
||||
expect(immediate.data.value).toBe('test')
|
||||
expect(immediate.status.value).toBe('success')
|
||||
expect(immediate.pending.value).toBe(false)
|
||||
|
||||
const nonimmediate = await useAsyncData(() => Promise.resolve('test'), { immediate: false })
|
||||
expect(nonimmediate.data.value).toBe(undefined)
|
||||
expect(nonimmediate.status.value).toBe('idle')
|
||||
expect(nonimmediate.pending.value).toBe(true)
|
||||
})
|
||||
|
||||
it('should capture errors', async () => {
|
||||
const { data, error, status, pending } = await useAsyncData('error-test', () => Promise.reject(new Error('test')), { default: () => 'default' })
|
||||
const { data, error, status } = await useAsyncData('error-test', () => Promise.reject(new Error('test')), { default: () => 'default' })
|
||||
expect(data.value).toMatchInlineSnapshot('"default"')
|
||||
expect(error.value).toMatchInlineSnapshot('[Error: test]')
|
||||
expect(status.value).toBe('error')
|
||||
expect(pending.value).toBe(false)
|
||||
expect(useNuxtApp().payload._errors['error-test']).toMatchInlineSnapshot('[Error: test]')
|
||||
|
||||
// TODO: fix the below
|
||||
// const { data: syncedData, error: syncedError, status: syncedStatus, pending: syncedPending } = await useAsyncData('error-test', () => ({}), { immediate: false })
|
||||
// const { data: syncedData, error: syncedError, status: syncedStatus } = await useAsyncData('error-test', () => ({}), { immediate: false })
|
||||
|
||||
// expect(syncedData.value).toEqual(null)
|
||||
// expect(syncedError.value).toEqual(error.value)
|
||||
// expect(syncedStatus.value).toEqual('idle')
|
||||
// expect(syncedPending.value).toEqual(true)
|
||||
})
|
||||
|
||||
// https://github.com/nuxt/nuxt/issues/23411
|
||||
@ -248,9 +243,9 @@ describe('useAsyncData', () => {
|
||||
|
||||
it('should use default while pending', async () => {
|
||||
const promise = useAsyncData(() => Promise.resolve('test'), { default: () => 'default' })
|
||||
const { data, pending } = promise
|
||||
const { data, status } = promise
|
||||
|
||||
expect(pending.value).toBe(true)
|
||||
expect(status.value).toBe('pending')
|
||||
expect(data.value).toMatchInlineSnapshot('"default"')
|
||||
|
||||
await promise
|
||||
|
Loading…
Reference in New Issue
Block a user