From 4be430e13fe7d817ef4f0a805da2c00979f3f1b4 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Fri, 15 Mar 2024 21:54:47 +0100 Subject: [PATCH] feat(nuxt): pass nuxt instance to `getCachedData` (#26287) --- docs/3.api/2.composables/use-async-data.md | 2 +- docs/3.api/2.composables/use-fetch.md | 2 +- packages/nuxt/src/app/composables/asyncData.ts | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/3.api/2.composables/use-async-data.md b/docs/3.api/2.composables/use-async-data.md index 8ef849235f..4768cedafe 100644 --- a/docs/3.api/2.composables/use-async-data.md +++ b/docs/3.api/2.composables/use-async-data.md @@ -122,7 +122,7 @@ type AsyncDataOptions = { transform?: (input: DataT) => DataT | Promise pick?: string[] watch?: WatchSource[] - getCachedData?: (key: string) => DataT + getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT } type AsyncData = { diff --git a/docs/3.api/2.composables/use-fetch.md b/docs/3.api/2.composables/use-fetch.md index 029f2e1778..1b2c98ab93 100644 --- a/docs/3.api/2.composables/use-fetch.md +++ b/docs/3.api/2.composables/use-fetch.md @@ -144,7 +144,7 @@ type UseFetchOptions = { server?: boolean lazy?: boolean immediate?: boolean - getCachedData?: (key: string) => DataT + getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT deep?: boolean dedupe?: 'cancel' | 'defer' default?: () => DataT diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 319be17438..41bf259270 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -61,7 +61,7 @@ export interface AsyncDataOptions< * A `null` or `undefined` return value will trigger a fetch. * Default is `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]` which only caches data when payloadExtraction is enabled. */ - getCachedData?: (key: string) => DataT + getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT /** * A function that can be used to alter handler function result after resolving. * Do not use it along with the `pick` option. @@ -243,7 +243,7 @@ export function useAsyncData< console.warn('[nuxt] `boolean` values are deprecated for the `dedupe` option of `useAsyncData` and will be removed in the future. Use \'cancel\' or \'defer\' instead.') } - const hasCachedData = () => options.getCachedData!(key) != null + const hasCachedData = () => options.getCachedData!(key, nuxtApp) != null // Create or use a shared asyncData entity if (!nuxtApp._asyncData[key] || !options.immediate) { @@ -252,7 +252,7 @@ export function useAsyncData< const _ref = options.deep ? ref : shallowRef nuxtApp._asyncData[key] = { - data: _ref(options.getCachedData!(key) ?? options.default!()), + data: _ref(options.getCachedData!(key, nuxtApp) ?? options.default!()), pending: ref(!hasCachedData()), error: toRef(nuxtApp.payload._errors, key), status: ref('idle') @@ -272,7 +272,7 @@ export function useAsyncData< } // Avoid fetching same key that is already fetched if ((opts._initial || (nuxtApp.isHydrating && opts._initial !== false)) && hasCachedData()) { - return Promise.resolve(options.getCachedData!(key)) + return Promise.resolve(options.getCachedData!(key, nuxtApp)) } asyncData.pending.value = true asyncData.status.value = 'pending'