Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): pass nuxt instance to getCachedData #26287

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/3.api/2.composables/use-async-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type AsyncDataOptions<DataT> = {
transform?: (input: DataT) => DataT | Promise<DataT>
pick?: string[]
watch?: WatchSource[]
getCachedData?: (key: string) => DataT
getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT
}

type AsyncData<DataT, ErrorT> = {
Expand Down
2 changes: 1 addition & 1 deletion docs/3.api/2.composables/use-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type UseFetchOptions<DataT> = {
server?: boolean
lazy?: boolean
immediate?: boolean
getCachedData?: (key: string) => DataT
getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT
deep?: boolean
dedupe?: 'cancel' | 'defer'
default?: () => DataT
Expand Down
8 changes: 4 additions & 4 deletions packages/nuxt/src/app/composables/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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')
Expand All @@ -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'
Expand Down