Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 5b36d48

Browse files
committed
fix(nuxt): use shared state for useAsyncData
1 parent d9cf95f commit 5b36d48

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

packages/nuxt/src/app/composables/asyncData.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,16 @@ export function useAsyncData<
114114

115115
const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined
116116

117-
const asyncData = {
118-
data: ref(useInitialCache() ? nuxt.payload.data[key] : options.default?.() ?? null),
119-
pending: ref(!useInitialCache()),
120-
error: ref(nuxt.payload._errors[key] ?? null)
121-
} as AsyncData<DataT, DataE>
117+
// Create or use a shared asyncData entity
118+
if (!nuxt._asyncData[key]) {
119+
nuxt._asyncData[key] = {
120+
data: ref(useInitialCache() ? nuxt.payload.data[key] : options.default?.() ?? null),
121+
pending: ref(!useInitialCache()),
122+
error: ref(nuxt.payload._errors[key] ?? null)
123+
}
124+
}
125+
// TODO: Else, Soemhow check for confliciting keys with different defaults or fetcher
126+
const asyncData = { ...nuxt._asyncData[key] } as AsyncData<DataT, DataE>
122127

123128
asyncData.refresh = (opts = {}) => {
124129
// Avoid fetching same key more than once at a time

packages/nuxt/src/app/nuxt.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-use-before-define */
2-
import { getCurrentInstance, reactive } from 'vue'
2+
import { getCurrentInstance, reactive, Ref } from 'vue'
33
import type { App, onErrorCaptured, VNode } from 'vue'
44
import { createHooks, Hookable } from 'hookable'
55
import type { RuntimeConfig, AppConfigInput } from '@nuxt/schema'
@@ -66,6 +66,11 @@ interface _NuxtApp {
6666
[key: string]: any
6767

6868
_asyncDataPromises: Record<string, Promise<any> | undefined>
69+
_asyncData: Record<string, {
70+
data: Ref<any>
71+
pending: Ref<boolean>
72+
error: Ref<any>
73+
}>,
6974

7075
ssrContext?: NuxtSSRContext
7176
payload: {
@@ -113,6 +118,7 @@ export function createNuxtApp (options: CreateOptions) {
113118
}),
114119
isHydrating: process.client,
115120
_asyncDataPromises: {},
121+
_asyncData: {},
116122
...options
117123
} as any as NuxtApp
118124

0 commit comments

Comments
 (0)