feat(nuxt): add private option to useAsyncData (#29064)

This commit is contained in:
Connor Pearson 2024-12-06 14:35:02 +01:00
parent f71cc9ddd1
commit bae3008728
2 changed files with 8 additions and 1 deletions

View File

@ -65,6 +65,7 @@ const { data: posts } = await useAsyncData(
- `handler`: an asynchronous function that must return a truthy value (for example, it should not be `undefined` or `null`) or the request may be duplicated on the client side
- `options`:
- `server`: whether to fetch the data on the server (defaults to `true`)
- `private`: when true, `server` will default to `false` on cached pages and `true` on uncached pages (defaults to `false`)
- `lazy`: whether to resolve the async function after loading the route, instead of blocking client-side navigation (defaults to `false`)
- `immediate`: when set to `false`, will prevent the request from firing immediately. (defaults to `true`)
- `default`: a factory function to set the default value of the `data`, before the async function resolves - useful with the `lazy: true` or `immediate: false` option

View File

@ -49,6 +49,11 @@ export interface AsyncDataOptions<
* @default true
*/
server?: boolean
/**
* When true, `server` will default to false on cached pages and true on uncached pages
* @default false
*/
private?: false
/**
* Whether to resolve the async function after loading the route, instead of blocking client-side navigation
* @default false
@ -230,7 +235,8 @@ export function useAsyncData<
const getDefaultCachedData = () => nuxtApp.isHydrating ? nuxtApp.payload.data[key] : nuxtApp.static.data[key]
// Apply defaults
options.server = options.server ?? true
const cachedRoute = !!nuxtApp.ssrContext?.event?.context.cache
options.server = options.server ?? (options.private ? !cachedRoute : true)
options.default = options.default ?? (getDefault as () => DefaultT)
options.getCachedData = options.getCachedData ?? getDefaultCachedData