From 628dc716b53dc38e722d10a44dbaca3714fe85b1 Mon Sep 17 00:00:00 2001 From: Luke Nelson Date: Thu, 14 Dec 2023 11:09:22 +0000 Subject: [PATCH] docs: add documentation on `getCachedData` option (#24697) --- docs/3.api/2.composables/use-async-data.md | 5 +++++ docs/3.api/2.composables/use-fetch.md | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/docs/3.api/2.composables/use-async-data.md b/docs/3.api/2.composables/use-async-data.md index 854c2bbf59..ba37c117ea 100644 --- a/docs/3.api/2.composables/use-async-data.md +++ b/docs/3.api/2.composables/use-async-data.md @@ -61,6 +61,7 @@ const { data: posts } = await useAsyncData( - `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 - `transform`: a function that can be used to alter `handler` function result after resolving + - `getCachedData`: Provide a function which returns cached data. A _null_ or _undefined_ return value will trigger a fetch. By default, this is: `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]`, which only caches data when `payloadExtraction` is enabled. - `pick`: only pick specified keys in this array from the `handler` function result - `watch`: watch reactive sources to auto-refresh - `deep`: return data in a deep ref object (it is `true` by default). It can be set to `false` to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive. @@ -76,6 +77,10 @@ Under the hood, `lazy: false` uses `` to block the loading of the rout You can use `useLazyAsyncData` to have the same behavior as `lazy: true` with `useAsyncData`. :: +::callout{icon="i-simple-icons-youtube" color="gray" to="https://www.youtube.com/watch?v=aQPR0xn-MMk" target="_blank"} +Learn how to use `transform` and `getCachedData` to avoid superfluous calls to an API and cache data for visitors on the client. +:: + ## Return Values - `data`: the result of the asynchronous function that is passed in. diff --git a/docs/3.api/2.composables/use-fetch.md b/docs/3.api/2.composables/use-fetch.md index d8b1349f53..057750ccf0 100644 --- a/docs/3.api/2.composables/use-fetch.md +++ b/docs/3.api/2.composables/use-fetch.md @@ -93,6 +93,7 @@ All fetch options can be given a `computed` or `ref` value. These will be watche - `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 - `transform`: a function that can be used to alter `handler` function result after resolving + - `getCachedData`: Provide a function which returns cached data. A _null_ or _undefined_ return value will trigger a fetch. By default, this is: `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]`, which only caches data when `payloadExtraction` is enabled. - `pick`: only pick specified keys in this array from the `handler` function result - `watch`: watch an array of reactive sources and auto-refresh the fetch result when they change. Fetch options and URL are watched by default. You can completely ignore reactive sources by using `watch: false`. Together with `immediate: false`, this allows for a fully-manual `useFetch`. - `deep`: return data in a deep ref object (it is `true` by default). It can be set to `false` to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive. @@ -104,6 +105,10 @@ All fetch options can be given a `computed` or `ref` value. These will be watche If you provide a function or ref as the `url` parameter, or if you provide functions as arguments to the `options` parameter, then the `useFetch` call will not match other `useFetch` calls elsewhere in your codebase, even if the options seem to be identical. If you wish to force a match, you may provide your own key in `options`. :: +::callout{icon="i-simple-icons-youtube" color="gray" to="https://www.youtube.com/watch?v=aQPR0xn-MMk" target="_blank"} +Learn how to use `transform` and `getCachedData` to avoid superfluous calls to an API and cache data for visitors on the client. +:: + ## Return Values - `data`: the result of the asynchronous function that is passed in.