mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
perf(nuxt): avoid multiple calls to getCachedData
(#28472)
This commit is contained in:
parent
7f199d014f
commit
dc9d756e5b
@ -239,18 +239,17 @@ export function useAsyncData<
|
||||
options.deep = options.deep ?? asyncDataDefaults.deep
|
||||
options.dedupe = options.dedupe ?? 'cancel'
|
||||
|
||||
// TODO: make more precise when v4 lands
|
||||
const hasCachedData = () => options.getCachedData!(key, nuxtApp) !== undefined
|
||||
|
||||
// Create or use a shared asyncData entity
|
||||
const initialCachedData = options.getCachedData!(key, nuxtApp)
|
||||
const hasCachedData = typeof initialCachedData !== 'undefined'
|
||||
|
||||
if (!nuxtApp._asyncData[key] || !options.immediate) {
|
||||
nuxtApp.payload._errors[key] ??= undefined
|
||||
|
||||
const _ref = options.deep ? ref : shallowRef
|
||||
const cachedData = options.getCachedData!(key, nuxtApp)
|
||||
nuxtApp._asyncData[key] = {
|
||||
data: _ref(typeof cachedData !== 'undefined' ? cachedData : options.default!()),
|
||||
pending: ref(!hasCachedData()),
|
||||
data: _ref(hasCachedData ? initialCachedData : options.default!()),
|
||||
pending: ref(!hasCachedData),
|
||||
error: toRef(nuxtApp.payload._errors, key),
|
||||
status: ref('idle'),
|
||||
_default: options.default!,
|
||||
@ -272,8 +271,11 @@ export function useAsyncData<
|
||||
(nuxtApp._asyncDataPromises[key] as any).cancelled = true
|
||||
}
|
||||
// Avoid fetching same key that is already fetched
|
||||
if ((opts._initial || (nuxtApp.isHydrating && opts._initial !== false)) && hasCachedData()) {
|
||||
return Promise.resolve(options.getCachedData!(key, nuxtApp))
|
||||
if ((opts._initial || (nuxtApp.isHydrating && opts._initial !== false))) {
|
||||
const cachedData = opts._initial ? initialCachedData : options.getCachedData!(key, nuxtApp)
|
||||
if (typeof cachedData !== 'undefined') {
|
||||
return Promise.resolve(cachedData)
|
||||
}
|
||||
}
|
||||
asyncData.pending.value = true
|
||||
asyncData.status.value = 'pending'
|
||||
@ -362,7 +364,7 @@ export function useAsyncData<
|
||||
onUnmounted(() => cbs.splice(0, cbs.length))
|
||||
}
|
||||
|
||||
if (fetchOnServer && nuxtApp.isHydrating && (asyncData.error.value || hasCachedData())) {
|
||||
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'
|
||||
|
@ -224,6 +224,17 @@ describe('useAsyncData', () => {
|
||||
`)
|
||||
})
|
||||
|
||||
it('should only call getCachedData once', async () => {
|
||||
const getCachedData = vi.fn(() => ({ val: false }))
|
||||
const { data } = await useAsyncData(() => Promise.resolve({ val: true }), { getCachedData })
|
||||
expect(data.value).toMatchInlineSnapshot(`
|
||||
{
|
||||
"val": false,
|
||||
}
|
||||
`)
|
||||
expect(getCachedData).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('should use default while pending', async () => {
|
||||
const promise = useAsyncData(() => Promise.resolve('test'), { default: () => 'default' })
|
||||
const { data, pending } = promise
|
||||
|
Loading…
Reference in New Issue
Block a user