mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
feat(nuxt): pass nuxt instance to getCachedData
(#26287)
This commit is contained in:
parent
e0ac145e9a
commit
4be430e13f
@ -122,7 +122,7 @@ type AsyncDataOptions<DataT> = {
|
|||||||
transform?: (input: DataT) => DataT | Promise<DataT>
|
transform?: (input: DataT) => DataT | Promise<DataT>
|
||||||
pick?: string[]
|
pick?: string[]
|
||||||
watch?: WatchSource[]
|
watch?: WatchSource[]
|
||||||
getCachedData?: (key: string) => DataT
|
getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT
|
||||||
}
|
}
|
||||||
|
|
||||||
type AsyncData<DataT, ErrorT> = {
|
type AsyncData<DataT, ErrorT> = {
|
||||||
|
@ -144,7 +144,7 @@ type UseFetchOptions<DataT> = {
|
|||||||
server?: boolean
|
server?: boolean
|
||||||
lazy?: boolean
|
lazy?: boolean
|
||||||
immediate?: boolean
|
immediate?: boolean
|
||||||
getCachedData?: (key: string) => DataT
|
getCachedData?: (key: string, nuxtApp: NuxtApp) => DataT
|
||||||
deep?: boolean
|
deep?: boolean
|
||||||
dedupe?: 'cancel' | 'defer'
|
dedupe?: 'cancel' | 'defer'
|
||||||
default?: () => DataT
|
default?: () => DataT
|
||||||
|
@ -61,7 +61,7 @@ export interface AsyncDataOptions<
|
|||||||
* A `null` or `undefined` return value will trigger a fetch.
|
* 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.
|
* 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.
|
* A function that can be used to alter handler function result after resolving.
|
||||||
* Do not use it along with the `pick` option.
|
* 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.')
|
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
|
// Create or use a shared asyncData entity
|
||||||
if (!nuxtApp._asyncData[key] || !options.immediate) {
|
if (!nuxtApp._asyncData[key] || !options.immediate) {
|
||||||
@ -252,7 +252,7 @@ export function useAsyncData<
|
|||||||
const _ref = options.deep ? ref : shallowRef
|
const _ref = options.deep ? ref : shallowRef
|
||||||
|
|
||||||
nuxtApp._asyncData[key] = {
|
nuxtApp._asyncData[key] = {
|
||||||
data: _ref(options.getCachedData!(key) ?? options.default!()),
|
data: _ref(options.getCachedData!(key, nuxtApp) ?? options.default!()),
|
||||||
pending: ref(!hasCachedData()),
|
pending: ref(!hasCachedData()),
|
||||||
error: toRef(nuxtApp.payload._errors, key),
|
error: toRef(nuxtApp.payload._errors, key),
|
||||||
status: ref('idle')
|
status: ref('idle')
|
||||||
@ -272,7 +272,7 @@ export function useAsyncData<
|
|||||||
}
|
}
|
||||||
// Avoid fetching same key that is already fetched
|
// Avoid fetching same key that is already fetched
|
||||||
if ((opts._initial || (nuxtApp.isHydrating && opts._initial !== false)) && hasCachedData()) {
|
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.pending.value = true
|
||||||
asyncData.status.value = 'pending'
|
asyncData.status.value = 'pending'
|
||||||
|
Loading…
Reference in New Issue
Block a user