nuxt-multi-cache
Version:
SSR route, component and data cache for Nuxt.js
49 lines (48 loc) • 2.61 kB
TypeScript
import type { NuxtApp, AsyncDataOptions, AsyncData, NuxtError } from 'nuxt/app';
import type { DefaultAsyncDataErrorValue, DefaultAsyncDataValue } from '#app/defaults';
import type { PickFrom } from '#app/composables/asyncData';
type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
type ValueOrMethod<T extends number | string[] | undefined, ResT> = ((v: ResT) => T) | T;
type CachedAsyncDataOptions<ResT, DataT = ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = DefaultAsyncDataValue> = Omit<AsyncDataOptions<ResT, DataT, PickKeys, DefaultT>, 'getCachedData'> & {
/**
* The client-side max age in seconds.
*
* If undefined, 0 or negative, nothing will be cached.
*
* If a positive integer value is provided, the composable will return
* cached data (either from payload or from previous client-side
* fetches) for the given duration.
*/
clientMaxAge?: number;
/**
* The server-side max age in seconds.
*
* Can be a number or a method that receives the result of your useAsyncData
* handler and should return a number.
*/
serverMaxAge?: ValueOrMethod<number, ResT>;
/**
* The server-side cache tags to use.
*
* Can be an array of strings or a method that receives the result of your useAsyncData
* handler and should return an array of strings.
*/
serverCacheTags?: ValueOrMethod<string[], ResT>;
};
/**
* Wrapper around useAsyncData and useDataCache.
*
* By default, the data is indefinitely cached on the server side. When the `
* serverMaxAge` option is provided, you can define a max age for server-side
* caching.
*
* If you want to extend caching to the client-side, provide a `clientMaxAge`
* value. This will cache the result in Nuxt's static data cache. The cache is
* not permanent, e.g. a browser refresh "purges" all cached data.
*
* @param {string} key The key used for both useAsyncData and useDataCache.
* @param {Function} handler The handler that should fetch the data.
* @param {CachedAsyncDataOptions|undefined} providedOptions The options.
*/
export declare function useCachedAsyncData<ResT, NuxtErrorDataT = unknown, DataT = ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = null>(key: string, handler: (app?: NuxtApp) => Promise<ResT>, providedOptions?: CachedAsyncDataOptions<ResT, DataT, PickKeys, DefaultT>): AsyncData<PickFrom<DataT, PickKeys> | DefaultT, (NuxtErrorDataT extends Error | NuxtError ? NuxtErrorDataT : NuxtError<NuxtErrorDataT>) | DefaultAsyncDataErrorValue>;
export {};