@nuxt/docs-nightly 4.5.0-29739713.e01e04a0 → 4.5.0-29739956.3ae095b2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -194,7 +194,7 @@ const { data, error } = await useAsyncData(() => myGetFunction('users'))
|
|
|
194
194
|
::note
|
|
195
195
|
The first argument of [`useAsyncData`](/docs/4.x/api/composables/use-async-data) is a unique key used to cache the response of the second argument, the querying function. This key can be ignored by directly passing the querying function, the key will be auto-generated.
|
|
196
196
|
:br :br
|
|
197
|
-
Since the autogenerated key only takes into account the
|
|
197
|
+
Since the autogenerated key only takes into account the location where `useAsyncData` is invoked, it is recommended to always create your own key to avoid unwanted behavior, like when you are creating your own custom composable wrapping `useAsyncData`.
|
|
198
198
|
:br :br
|
|
199
199
|
Setting a key can be useful to share the same data between components using [`useNuxtData`](/docs/4.x/api/composables/use-nuxt-data) or to [refresh specific data](/docs/4.x/api/utils/refresh-nuxt-data#refresh-specific-data).
|
|
200
200
|
::
|
|
@@ -368,8 +368,8 @@ Both `pick` and `transform` don't prevent the unwanted data from being fetched i
|
|
|
368
368
|
|
|
369
369
|
[`useFetch`](/docs/4.x/api/composables/use-fetch) and [`useAsyncData`](/docs/4.x/api/composables/use-async-data) use keys to prevent refetching the same data.
|
|
370
370
|
|
|
371
|
-
- [`useFetch`](/docs/4.x/api/composables/use-fetch)
|
|
372
|
-
- [`useAsyncData`](/docs/4.x/api/composables/use-async-data) uses its first argument as a key if it is a string. If the first argument is the handler function that performs the query, then a key that is unique to the
|
|
371
|
+
- [`useFetch`](/docs/4.x/api/composables/use-fetch) generates a key from the URL, the fetch options and the location of the call in your source code. This means two `useFetch` calls with the same URL in different components have **different** keys and will each perform their own request. To share the same data between multiple components, provide the same explicit `key` in the `options` object passed as a last argument.
|
|
372
|
+
- [`useAsyncData`](/docs/4.x/api/composables/use-async-data) uses its first argument as a key if it is a string. If the first argument is the handler function that performs the query, then a key that is unique to the location of the `useAsyncData` call in your source code will be generated for you.
|
|
373
373
|
|
|
374
374
|
::tip
|
|
375
375
|
To get the cached data by key, you can use [`useNuxtData`](/docs/4.x/api/composables/use-nuxt-data)
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
This composable provides a convenient wrapper around [`useAsyncData`](/docs/4.x/api/composables/use-async-data) and [`$fetch`](/docs/4.x/api/utils/dollarfetch).
|
|
12
|
-
It automatically generates a key
|
|
12
|
+
It automatically generates a key for the request, provides type hints for request url based on server routes, and infers API response type.
|
|
13
13
|
|
|
14
14
|
::note
|
|
15
15
|
`useFetch` is a composable meant to be called directly in a setup function, plugin, or route middleware. It returns reactive composables and handles adding responses to the Nuxt payload so they can be passed from server to client without re-fetching the data on client side when the page hydrates.
|
|
@@ -84,7 +84,25 @@ const { data: post } = await useFetch(() => `/api/posts/${id.value}`)
|
|
|
84
84
|
</script>
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
The auto-generated key is unique to each call site, so calling `useFetch` with the same URL and options in different components will **not** share state and each call performs its own request. Multiple instances of the same component do share state, since they use the same call site. To share the same `data`, `error` and `status` refs across different components, provide the same explicit `key` to each call:
|
|
88
|
+
|
|
89
|
+
::code-group
|
|
90
|
+
|
|
91
|
+
```vue [app/components/ComponentA.vue]
|
|
92
|
+
<script setup lang="ts">
|
|
93
|
+
// shares the data with ComponentB - only one request is made
|
|
94
|
+
const { data } = await useFetch('/api/random', { key: 'random' })
|
|
95
|
+
</script>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```vue [app/components/ComponentB.vue]
|
|
99
|
+
<script setup lang="ts">
|
|
100
|
+
// shares the data with ComponentA - only one request is made
|
|
101
|
+
const { data } = await useFetch('/api/random', { key: 'random' })
|
|
102
|
+
</script>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
::
|
|
88
106
|
|
|
89
107
|
::tip
|
|
90
108
|
Keyed state created using `useFetch` can be retrieved across your Nuxt application using [`useNuxtData`](/docs/4.x/api/composables/use-nuxt-data).
|
|
@@ -191,7 +209,7 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
191
209
|
|
|
192
210
|
| Option | Type | Default | Description |
|
|
193
211
|
|---------------------------------------------------------------------------|-------------------------------------------------------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
194
|
-
| `key` | `MaybeRefOrGetter<string>` | auto-gen | Unique key for de-duplication. If not provided, generated from URL and
|
|
212
|
+
| `key` | `MaybeRefOrGetter<string>` | auto-gen | Unique key for de-duplication. If not provided, generated from the URL, options and call site location in the source code. |
|
|
195
213
|
| `method` | `MaybeRefOrGetter<string>` | `'GET'` | HTTP request method. |
|
|
196
214
|
| `query` | `MaybeRefOrGetter<SearchParams>` | - | Query/search params to append to the URL. Alias: `params`. |
|
|
197
215
|
| `params` | `MaybeRefOrGetter<SearchParams>` | - | Alias for `query`. |
|
|
@@ -50,6 +50,6 @@ export default eventHandler(() => {
|
|
|
50
50
|
})
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
In API routes, using `createError` by passing an object with a short `statusText` is recommended because it can be accessed on the client side. Otherwise, a `message` passed to `createError` on an API route will not propagate to the client. Alternatively, you can use the `data` property to pass data back to the client. In any case, always consider avoiding to put dynamic user input to the message to avoid potential security issues.
|
|
53
|
+
In API routes, using `createError` by passing an object with a short `statusText` is recommended because it can be accessed on the client side. Otherwise, a `message` passed to `createError` on an API route will not propagate to the client. Alternatively, you can use the `data` property to pass data back to the client. When handling the error with `useFetch`, the custom data is available at `error.value.data.data`. In any case, always consider avoiding to put dynamic user input to the message to avoid potential security issues.
|
|
54
54
|
|
|
55
55
|
:read-more{to="/docs/4.x/getting-started/error-handling"}
|