diff --git a/docs/3.api/2.composables/use-async-data.md b/docs/3.api/2.composables/use-async-data.md index 7619dd1eb7..76c610226c 100644 --- a/docs/3.api/2.composables/use-async-data.md +++ b/docs/3.api/2.composables/use-async-data.md @@ -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 diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 29193a0616..14464e926a 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -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