@nuxt/docs 4.1.2 → 4.2.0
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.
- package/1.getting-started/01.introduction.md +6 -6
- package/1.getting-started/02.installation.md +3 -2
- package/1.getting-started/03.configuration.md +36 -36
- package/1.getting-started/04.views.md +9 -9
- package/1.getting-started/05.assets.md +15 -9
- package/1.getting-started/06.styling.md +58 -48
- package/1.getting-started/07.routing.md +17 -17
- package/1.getting-started/08.seo-meta.md +59 -46
- package/1.getting-started/09.transitions.md +49 -44
- package/1.getting-started/10.data-fetching.md +104 -81
- package/1.getting-started/11.state-management.md +26 -19
- package/1.getting-started/12.error-handling.md +22 -20
- package/1.getting-started/13.server.md +8 -8
- package/1.getting-started/14.layers.md +22 -16
- package/1.getting-started/15.prerendering.md +32 -32
- package/1.getting-started/16.deployment.md +10 -10
- package/1.getting-started/17.testing.md +44 -44
- package/1.getting-started/18.upgrade.md +241 -110
- package/2.guide/0.index.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/.navigation.yml +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/0.nuxt.md +3 -3
- package/2.guide/1.directory-structure/0.output.md +18 -0
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/.navigation.yml +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.assets.md +4 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.components.md +45 -28
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.composables.md +13 -13
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.layouts.md +19 -15
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.middleware.md +31 -25
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.pages.md +39 -37
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.plugins.md +25 -25
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.utils.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app-config.md +18 -18
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.error.md +6 -6
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.content.md +7 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.modules.md +8 -8
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.node_modules.md +2 -2
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.public.md +2 -2
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.server.md +35 -35
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.shared.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.env.md +8 -8
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.gitignore.md +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtignore.md +5 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtrc.md +4 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.nuxt-config.md +3 -3
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.package.md +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.tsconfig.md +3 -3
- package/2.guide/{1.concepts → 2.concepts}/1.auto-imports.md +22 -22
- package/2.guide/{1.concepts → 2.concepts}/10.nuxt-lifecycle.md +17 -17
- package/2.guide/{1.concepts → 2.concepts}/2.vuejs-development.md +13 -13
- package/2.guide/{1.concepts → 2.concepts}/3.rendering.md +24 -22
- package/2.guide/{1.concepts → 2.concepts}/4.server-engine.md +6 -6
- package/2.guide/{1.concepts → 2.concepts}/5.modules.md +5 -5
- package/2.guide/{1.concepts → 2.concepts}/7.esm.md +22 -16
- package/2.guide/{1.concepts → 2.concepts}/8.typescript.md +12 -12
- package/2.guide/3.going-further/1.events.md +3 -3
- package/2.guide/3.going-further/1.experimental-features.md +334 -85
- package/2.guide/3.going-further/1.features.md +58 -11
- package/2.guide/3.going-further/1.internals.md +25 -25
- package/2.guide/3.going-further/10.runtime-config.md +12 -12
- package/2.guide/3.going-further/11.nightly-release-channel.md +1 -1
- package/2.guide/3.going-further/2.hooks.md +14 -14
- package/2.guide/3.going-further/3.modules.md +126 -108
- package/2.guide/3.going-further/4.kit.md +7 -7
- package/2.guide/3.going-further/6.nuxt-app.md +8 -8
- package/2.guide/3.going-further/7.layers.md +76 -59
- package/2.guide/3.going-further/9.debugging.md +2 -2
- package/2.guide/4.recipes/1.custom-routing.md +30 -30
- package/2.guide/4.recipes/2.vite-plugin.md +45 -4
- package/2.guide/4.recipes/3.custom-usefetch.md +13 -13
- package/2.guide/4.recipes/4.sessions-and-authentication.md +35 -21
- package/2.guide/5.best-practices/hydration.md +4 -4
- package/2.guide/5.best-practices/performance.md +17 -17
- package/3.api/1.components/1.client-only.md +6 -3
- package/3.api/1.components/1.nuxt-client-fallback.md +10 -7
- package/3.api/1.components/10.nuxt-picture.md +1 -1
- package/3.api/1.components/11.teleports.md +5 -2
- package/3.api/1.components/12.nuxt-route-announcer.md +9 -9
- package/3.api/1.components/13.nuxt-time.md +49 -18
- package/3.api/1.components/2.nuxt-page.md +6 -6
- package/3.api/1.components/3.nuxt-layout.md +15 -10
- package/3.api/1.components/4.nuxt-link.md +42 -22
- package/3.api/1.components/5.nuxt-loading-indicator.md +3 -3
- package/3.api/1.components/6.nuxt-error-boundary.md +12 -10
- package/3.api/2.composables/on-prehydrate.md +2 -2
- package/3.api/2.composables/use-app-config.md +1 -1
- package/3.api/2.composables/use-async-data.md +26 -22
- package/3.api/2.composables/use-cookie.md +29 -21
- package/3.api/2.composables/use-error.md +2 -2
- package/3.api/2.composables/use-fetch.md +62 -32
- package/3.api/2.composables/use-head-safe.md +7 -7
- package/3.api/2.composables/use-head.md +5 -5
- package/3.api/2.composables/use-hydration.md +6 -6
- package/3.api/2.composables/use-lazy-async-data.md +4 -4
- package/3.api/2.composables/use-lazy-fetch.md +4 -4
- package/3.api/2.composables/use-loading-indicator.md +12 -12
- package/3.api/2.composables/use-nuxt-app.md +22 -22
- package/3.api/2.composables/use-nuxt-data.md +8 -8
- package/3.api/2.composables/use-preview-mode.md +15 -18
- package/3.api/2.composables/use-request-event.md +1 -1
- package/3.api/2.composables/use-request-fetch.md +3 -3
- package/3.api/2.composables/use-request-header.md +1 -1
- package/3.api/2.composables/use-request-headers.md +4 -4
- package/3.api/2.composables/use-request-url.md +1 -1
- package/3.api/2.composables/use-response-header.md +9 -10
- package/3.api/2.composables/use-route-announcer.md +4 -4
- package/3.api/2.composables/use-route.md +1 -1
- package/3.api/2.composables/use-router.md +10 -8
- package/3.api/2.composables/use-runtime-config.md +11 -11
- package/3.api/2.composables/use-runtime-hook.md +2 -2
- package/3.api/2.composables/use-seo-meta.md +4 -4
- package/3.api/2.composables/use-server-seo-meta.md +6 -6
- package/3.api/2.composables/use-state.md +5 -5
- package/3.api/3.utils/$fetch.md +10 -8
- package/3.api/3.utils/abort-navigation.md +3 -3
- package/3.api/3.utils/add-route-middleware.md +5 -5
- package/3.api/3.utils/call-once.md +6 -6
- package/3.api/3.utils/clear-error.md +3 -3
- package/3.api/3.utils/clear-nuxt-data.md +3 -3
- package/3.api/3.utils/clear-nuxt-state.md +3 -3
- package/3.api/3.utils/create-error.md +2 -2
- package/3.api/3.utils/define-lazy-hydration-component.md +13 -13
- package/3.api/3.utils/define-nuxt-component.md +5 -5
- package/3.api/3.utils/define-nuxt-plugin.md +13 -13
- package/3.api/3.utils/define-nuxt-route-middleware.md +5 -5
- package/3.api/3.utils/define-page-meta.md +23 -23
- package/3.api/3.utils/define-route-rules.md +7 -7
- package/3.api/3.utils/navigate-to.md +11 -11
- package/3.api/3.utils/prefetch-components.md +1 -1
- package/3.api/3.utils/preload-components.md +1 -1
- package/3.api/3.utils/preload-route-components.md +2 -2
- package/3.api/3.utils/prerender-routes.md +3 -3
- package/3.api/3.utils/refresh-cookie.md +4 -4
- package/3.api/3.utils/refresh-nuxt-data.md +12 -7
- package/3.api/3.utils/reload-nuxt-app.md +3 -3
- package/3.api/3.utils/set-page-layout.md +1 -1
- package/3.api/3.utils/set-response-status.md +3 -3
- package/3.api/3.utils/show-error.md +5 -5
- package/3.api/3.utils/update-app-config.md +4 -3
- package/3.api/4.commands/add.md +1 -1
- package/3.api/4.commands/analyze.md +2 -1
- package/3.api/4.commands/build.md +2 -1
- package/3.api/4.commands/dev.md +5 -4
- package/3.api/4.commands/generate.md +3 -2
- package/3.api/4.commands/init.md +3 -2
- package/3.api/4.commands/module.md +4 -4
- package/3.api/4.commands/prepare.md +7 -2
- package/3.api/4.commands/preview.md +5 -4
- package/3.api/4.commands/test.md +40 -0
- package/3.api/4.commands/typecheck.md +5 -3
- package/3.api/4.commands/upgrade.md +3 -3
- package/3.api/5.kit/1.modules.md +123 -37
- package/3.api/5.kit/10.runtime-config.md +1 -1
- package/3.api/5.kit/10.templates.md +8 -6
- package/3.api/5.kit/11.nitro.md +66 -62
- package/3.api/5.kit/12.resolving.md +2 -2
- package/3.api/5.kit/14.builder.md +61 -4
- package/3.api/5.kit/15.examples.md +5 -7
- package/3.api/5.kit/16.layers.md +26 -26
- package/3.api/5.kit/3.compatibility.md +14 -14
- package/3.api/5.kit/4.autoimports.md +13 -13
- package/3.api/5.kit/5.components.md +8 -7
- package/3.api/5.kit/6.context.md +3 -3
- package/3.api/5.kit/7.pages.md +7 -7
- package/3.api/5.kit/8.layout.md +2 -2
- package/3.api/5.kit/9.head.md +132 -0
- package/3.api/5.kit/9.plugins.md +5 -4
- package/3.api/6.advanced/1.hooks.md +5 -5
- package/3.api/6.advanced/2.import-meta.md +3 -3
- package/3.api/6.nuxt-config.md +365 -859
- package/3.api/index.md +7 -7
- package/5.community/2.getting-help.md +1 -1
- package/5.community/3.reporting-bugs.md +3 -3
- package/5.community/4.contribution.md +11 -11
- package/5.community/5.framework-contribution.md +4 -4
- package/5.community/6.roadmap.md +2 -2
- package/6.bridge/1.overview.md +13 -13
- package/6.bridge/10.configuration.md +2 -1
- package/6.bridge/2.typescript.md +3 -3
- package/6.bridge/3.bridge-composition-api.md +8 -8
- package/6.bridge/4.plugins-and-middleware.md +9 -9
- package/6.bridge/5.nuxt3-compatible-api.md +20 -17
- package/6.bridge/6.meta.md +20 -19
- package/6.bridge/7.runtime-config.md +1 -1
- package/6.bridge/8.nitro.md +3 -3
- package/6.bridge/9.vite.md +4 -4
- package/7.migration/1.overview.md +2 -2
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +23 -21
- package/7.migration/20.module-authors.md +7 -7
- package/7.migration/3.auto-imports.md +3 -3
- package/7.migration/4.meta.md +21 -18
- package/7.migration/5.plugins-and-middleware.md +8 -8
- package/7.migration/6.pages-and-layouts.md +28 -24
- package/7.migration/7.component-options.md +18 -18
- package/7.migration/8.runtime-config.md +7 -7
- package/package.json +1 -1
- package/2.guide/2.directory-structure/0.output.md +0 -18
- /package/2.guide/{1.concepts → 2.concepts}/.navigation.yml +0 -0
- /package/2.guide/{1.concepts → 2.concepts}/9.code-style.md +0 -0
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
This composable provides a convenient wrapper around [`useAsyncData`](/docs/api/composables/use-async-data) and [`$fetch`](/docs/api/utils/dollarfetch).
|
|
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
12
|
It automatically generates a key based on URL and fetch options, provides type hints for request url based on server routes, and infers API response type.
|
|
13
13
|
|
|
14
14
|
::note
|
|
@@ -20,13 +20,13 @@ It automatically generates a key based on URL and fetch options, provides type h
|
|
|
20
20
|
```vue [app/pages/modules.vue]
|
|
21
21
|
<script setup lang="ts">
|
|
22
22
|
const { data, status, error, refresh, clear } = await useFetch('/api/modules', {
|
|
23
|
-
pick: ['title']
|
|
23
|
+
pick: ['title'],
|
|
24
24
|
})
|
|
25
25
|
</script>
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
::warning
|
|
29
|
-
If you're using a custom useFetch wrapper, do not await it in the composable, as that can cause unexpected behavior. Please follow [this recipe](/docs/guide/recipes/custom-usefetch#custom-
|
|
29
|
+
If you're using a custom useFetch wrapper, do not await it in the composable, as that can cause unexpected behavior. Please follow [this recipe](/docs/4.x/guide/recipes/custom-usefetch#custom-usefetchuseasyncdata) for more information on how to make a custom async data fetcher.
|
|
30
30
|
::
|
|
31
31
|
|
|
32
32
|
::note
|
|
@@ -38,7 +38,7 @@ Using the `query` option, you can add search parameters to your query. This opti
|
|
|
38
38
|
```ts
|
|
39
39
|
const param1 = ref('value1')
|
|
40
40
|
const { data, status, error, refresh } = await useFetch('/api/modules', {
|
|
41
|
-
query: { param1, param2: 'value2' }
|
|
41
|
+
query: { param1, param2: 'value2' },
|
|
42
42
|
})
|
|
43
43
|
```
|
|
44
44
|
|
|
@@ -48,21 +48,21 @@ You can also use [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interce
|
|
|
48
48
|
|
|
49
49
|
```ts
|
|
50
50
|
const { data, status, error, refresh, clear } = await useFetch('/api/auth/login', {
|
|
51
|
-
onRequest({ request, options }) {
|
|
51
|
+
onRequest ({ request, options }) {
|
|
52
52
|
// Set the request headers
|
|
53
53
|
// note that this relies on ofetch >= 1.4.0 - you may need to refresh your lockfile
|
|
54
54
|
options.headers.set('Authorization', '...')
|
|
55
55
|
},
|
|
56
|
-
onRequestError({ request, options, error }) {
|
|
56
|
+
onRequestError ({ request, options, error }) {
|
|
57
57
|
// Handle the request errors
|
|
58
58
|
},
|
|
59
|
-
onResponse({ request, response, options }) {
|
|
59
|
+
onResponse ({ request, response, options }) {
|
|
60
60
|
// Process the response data
|
|
61
61
|
localStorage.setItem('token', response._data.token)
|
|
62
62
|
},
|
|
63
|
-
onResponseError({ request, response, options }) {
|
|
63
|
+
onResponseError ({ request, response, options }) {
|
|
64
64
|
// Handle the response errors
|
|
65
|
-
}
|
|
65
|
+
},
|
|
66
66
|
})
|
|
67
67
|
```
|
|
68
68
|
|
|
@@ -83,7 +83,7 @@ const { data: post } = await useFetch(() => `/api/posts/${id.value}`)
|
|
|
83
83
|
When using `useFetch` with the same URL and options in multiple components, they will share the same `data`, `error` and `status` refs. This ensures consistency across components.
|
|
84
84
|
|
|
85
85
|
::tip
|
|
86
|
-
Keyed state created using `useFetch` can be retrieved across your Nuxt application using [`useNuxtData`](/docs/api/composables/use-nuxt-data).
|
|
86
|
+
Keyed state created using `useFetch` can be retrieved across your Nuxt application using [`useNuxtData`](/docs/4.x/api/composables/use-nuxt-data).
|
|
87
87
|
::
|
|
88
88
|
|
|
89
89
|
::warning
|
|
@@ -96,35 +96,62 @@ If you encounter the `data` variable destructured from a `useFetch` returns a st
|
|
|
96
96
|
|
|
97
97
|
:video-accordion{title="Watch the video from Alexander Lichter to avoid using useFetch the wrong way" videoId="njsGVmcWviY"}
|
|
98
98
|
|
|
99
|
-
:read-more{to="/docs/getting-started/data-fetching"}
|
|
99
|
+
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
100
|
+
|
|
101
|
+
### Reactive Fetch Options
|
|
102
|
+
|
|
103
|
+
Fetch options can be provided as reactive, supporting `computed`, `ref` and [computed getters](https://vuejs.org/guide/essentials/computed.html). When a reactive fetch option is updated it will trigger a refetch using the updated resolved reactive value.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const searchQuery = ref('initial')
|
|
107
|
+
const { data } = await useFetch('/api/search', {
|
|
108
|
+
query: { q: searchQuery },
|
|
109
|
+
})
|
|
110
|
+
// triggers a refetch: /api/search?q=new%20search
|
|
111
|
+
searchQuery.value = 'new search'
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
If needed, you can opt out of this behavior using `watch: false`:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
const searchQuery = ref('initial')
|
|
118
|
+
const { data } = await useFetch('/api/search', {
|
|
119
|
+
query: { q: searchQuery },
|
|
120
|
+
watch: false,
|
|
121
|
+
})
|
|
122
|
+
// does not trigger a refetch
|
|
123
|
+
searchQuery.value = 'new search'
|
|
124
|
+
```
|
|
100
125
|
|
|
101
126
|
## Type
|
|
102
127
|
|
|
103
128
|
```ts [Signature]
|
|
104
|
-
function useFetch<DataT, ErrorT>(
|
|
129
|
+
export function useFetch<DataT, ErrorT> (
|
|
105
130
|
url: string | Request | Ref<string | Request> | (() => string | Request),
|
|
106
131
|
options?: UseFetchOptions<DataT>
|
|
107
132
|
): Promise<AsyncData<DataT, ErrorT>>
|
|
108
133
|
|
|
109
134
|
type UseFetchOptions<DataT> = {
|
|
110
135
|
key?: MaybeRefOrGetter<string>
|
|
111
|
-
method?: string
|
|
112
|
-
query?: SearchParams
|
|
113
|
-
params?: SearchParams
|
|
114
|
-
body?: RequestInit['body'] | Record<string, any
|
|
115
|
-
headers?: Record<string, string> | [key: string, value: string][] | Headers
|
|
116
|
-
baseURL?: string
|
|
136
|
+
method?: MaybeRefOrGetter<string>
|
|
137
|
+
query?: MaybeRefOrGetter<SearchParams>
|
|
138
|
+
params?: MaybeRefOrGetter<SearchParams>
|
|
139
|
+
body?: MaybeRefOrGetter<RequestInit['body'] | Record<string, any>>
|
|
140
|
+
headers?: MaybeRefOrGetter<Record<string, string> | [key: string, value: string][] | Headers>
|
|
141
|
+
baseURL?: MaybeRefOrGetter<string>
|
|
117
142
|
server?: boolean
|
|
118
143
|
lazy?: boolean
|
|
119
144
|
immediate?: boolean
|
|
120
145
|
getCachedData?: (key: string, nuxtApp: NuxtApp, ctx: AsyncDataRequestContext) => DataT | undefined
|
|
121
146
|
deep?: boolean
|
|
122
147
|
dedupe?: 'cancel' | 'defer'
|
|
148
|
+
timeout?: number
|
|
123
149
|
default?: () => DataT
|
|
124
150
|
transform?: (input: DataT) => DataT | Promise<DataT>
|
|
125
151
|
pick?: string[]
|
|
126
152
|
$fetch?: typeof globalThis.$fetch
|
|
127
153
|
watch?: MultiWatchSources | false
|
|
154
|
+
timeout?: MaybeRefOrGetter<number>
|
|
128
155
|
}
|
|
129
156
|
|
|
130
157
|
type AsyncDataRequestContext = {
|
|
@@ -143,6 +170,8 @@ type AsyncData<DataT, ErrorT> = {
|
|
|
143
170
|
|
|
144
171
|
interface AsyncDataExecuteOptions {
|
|
145
172
|
dedupe?: 'cancel' | 'defer'
|
|
173
|
+
timeout?: number
|
|
174
|
+
signal?: AbortSignal
|
|
146
175
|
}
|
|
147
176
|
|
|
148
177
|
type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
@@ -152,30 +181,31 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
152
181
|
|
|
153
182
|
- `URL` (`string | Request | Ref<string | Request> | () => string | Request`): The URL or request to fetch. Can be a string, a Request object, a Vue ref, or a function returning a string/Request. Supports reactivity for dynamic endpoints.
|
|
154
183
|
|
|
155
|
-
- `options` (object): Configuration for the fetch request. Extends [unjs/ofetch](https://github.com/unjs/ofetch) options and [`AsyncDataOptions`](/docs/api/composables/use-async-data#params). All options can be a static value, a `ref`, or a computed value.
|
|
184
|
+
- `options` (object): Configuration for the fetch request. Extends [unjs/ofetch](https://github.com/unjs/ofetch) options and [`AsyncDataOptions`](/docs/4.x/api/composables/use-async-data#params). All options can be a static value, a `ref`, or a computed value.
|
|
156
185
|
|
|
157
186
|
| Option | Type | Default | Description |
|
|
158
187
|
| ---| --- | --- | --- |
|
|
159
188
|
| `key` | `MaybeRefOrGetter<string>` | auto-gen | Unique key for de-duplication. If not provided, generated from URL and options. |
|
|
160
|
-
| `method` | `string
|
|
161
|
-
| `query` | `
|
|
162
|
-
| `params` | `
|
|
163
|
-
| `body` | `RequestInit['body'] \| Record<string, any
|
|
164
|
-
| `headers` | `Record<string, string> \| [key, value][] \| Headers
|
|
165
|
-
| `baseURL` | `string
|
|
166
|
-
| `timeout` | `number
|
|
189
|
+
| `method` | `MaybeRefOrGetter<string>` | `'GET'` | HTTP request method. |
|
|
190
|
+
| `query` | `MaybeRefOrGetter<SearchParams>` | - | Query/search params to append to the URL. Alias: `params`. |
|
|
191
|
+
| `params` | `MaybeRefOrGetter<SearchParams>` | - | Alias for `query`. |
|
|
192
|
+
| `body` | `MaybeRefOrGetter<RequestInit['body'] \| Record<string, any>>` | - | Request body. Objects are automatically stringified. |
|
|
193
|
+
| `headers` | `MaybeRefOrGetter<Record<string, string> \| [key, value][] \| Headers>` | - | Request headers. |
|
|
194
|
+
| `baseURL` | `MaybeRefOrGetter<string>` | - | Base URL for the request. |
|
|
195
|
+
| `timeout` | `MaybeRefOrGetter<number>` | - | Timeout in milliseconds to abort the request. |
|
|
167
196
|
| `cache` | `boolean \| string` | - | Cache control. Boolean disables cache, or use Fetch API values: `default`, `no-store`, etc. |
|
|
168
197
|
| `server` | `boolean` | `true` | Whether to fetch on the server. |
|
|
169
198
|
| `lazy` | `boolean` | `false` | If true, resolves after route loads (does not block navigation). |
|
|
170
199
|
| `immediate` | `boolean` | `true` | If false, prevents request from firing immediately. |
|
|
171
200
|
| `default` | `() => DataT` | - | Factory for default value of `data` before async resolves. |
|
|
201
|
+
| `timeout` | `number` | - | A number in milliseconds to wait before timing out the request (defaults to `undefined`, which means no timeout) |
|
|
172
202
|
| `transform` | `(input: DataT) => DataT \| Promise<DataT>` | - | Function to transform the result after resolving. |
|
|
173
203
|
| `getCachedData`| `(key, nuxtApp, ctx) => DataT \| undefined` | - | Function to return cached data. See below for default. |
|
|
174
204
|
| `pick` | `string[]` | - | Only pick specified keys from the result. |
|
|
175
205
|
| `watch` | `MultiWatchSources \| false` | - | Array of reactive sources to watch and auto-refresh. `false` disables watching. |
|
|
176
206
|
| `deep` | `boolean` | `false` | Return data in a deep ref object. |
|
|
177
207
|
| `dedupe` | `'cancel' \| 'defer'` | `'cancel'` | Avoid fetching same key more than once at a time. |
|
|
178
|
-
| `$fetch` | `typeof globalThis.$fetch` | - | Custom $fetch implementation. |
|
|
208
|
+
| `$fetch` | `typeof globalThis.$fetch` | - | Custom $fetch implementation. See [Custom useFetch in Nuxt](/docs/4.x/guide/recipes/custom-usefetch) |
|
|
179
209
|
|
|
180
210
|
::note
|
|
181
211
|
All fetch options can be given a `computed` or `ref` value. These will be watched and new requests made automatically with any new values if they are updated.
|
|
@@ -184,9 +214,9 @@ All fetch options can be given a `computed` or `ref` value. These will be watche
|
|
|
184
214
|
**getCachedData default:**
|
|
185
215
|
|
|
186
216
|
```ts
|
|
187
|
-
const getDefaultCachedData = (key, nuxtApp, ctx) => nuxtApp.isHydrating
|
|
188
|
-
|
|
189
|
-
|
|
217
|
+
const getDefaultCachedData = (key, nuxtApp, ctx) => nuxtApp.isHydrating
|
|
218
|
+
? nuxtApp.payload.data[key]
|
|
219
|
+
: nuxtApp.static.data[key]
|
|
190
220
|
```
|
|
191
221
|
This only caches data when `experimental.payloadExtraction` in `nuxt.config` is enabled.
|
|
192
222
|
|
|
@@ -214,6 +244,6 @@ If you have not fetched data on the server (for example, with `server: false`),
|
|
|
214
244
|
|
|
215
245
|
### Examples
|
|
216
246
|
|
|
217
|
-
:link-example{to="/docs/examples/advanced/use-custom-fetch-composable"}
|
|
247
|
+
:link-example{to="/docs/4.x/examples/advanced/use-custom-fetch-composable"}
|
|
218
248
|
|
|
219
|
-
:link-example{to="/docs/examples/features/data-fetching"}
|
|
249
|
+
:link-example{to="/docs/4.x/examples/features/data-fetching"}
|
|
@@ -8,20 +8,20 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
The `useHeadSafe` composable is a wrapper around the [`useHead`](/docs/api/composables/use-head) composable that restricts the input to only allow safe values.
|
|
11
|
+
The `useHeadSafe` composable is a wrapper around the [`useHead`](/docs/4.x/api/composables/use-head) composable that restricts the input to only allow safe values.
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
|
-
You can pass all the same values as [`useHead`](/docs/api/composables/use-head)
|
|
15
|
+
You can pass all the same values as [`useHead`](/docs/4.x/api/composables/use-head)
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
useHeadSafe({
|
|
19
19
|
script: [
|
|
20
|
-
{ id: 'xss-script', innerHTML: 'alert("xss")' }
|
|
20
|
+
{ id: 'xss-script', innerHTML: 'alert("xss")' },
|
|
21
21
|
],
|
|
22
22
|
meta: [
|
|
23
|
-
{ 'http-equiv': 'refresh', content: '0;javascript:alert(1)' }
|
|
24
|
-
]
|
|
23
|
+
{ 'http-equiv': 'refresh', 'content': '0;javascript:alert(1)' },
|
|
24
|
+
],
|
|
25
25
|
})
|
|
26
26
|
// Will safely generate
|
|
27
27
|
// <script id="xss-script"></script>
|
|
@@ -34,8 +34,8 @@ Read more on the `Unhead` documentation.
|
|
|
34
34
|
|
|
35
35
|
## Type
|
|
36
36
|
|
|
37
|
-
```ts
|
|
38
|
-
useHeadSafe(input: MaybeComputedRef<HeadSafe>): void
|
|
37
|
+
```ts [Signature]
|
|
38
|
+
export function useHeadSafe (input: MaybeComputedRef<HeadSafe>): void
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
The list of allowed values is:
|
|
@@ -8,17 +8,17 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
The [`useHead`](/docs/api/composables/use-head) composable function allows you to manage your head tags in a programmatic and reactive way, powered by [Unhead](https://unhead.unjs.io). If the data comes from a user or other untrusted source, we recommend you check out [`useHeadSafe`](/docs/api/composables/use-head-safe).
|
|
11
|
+
The [`useHead`](/docs/4.x/api/composables/use-head) composable function allows you to manage your head tags in a programmatic and reactive way, powered by [Unhead](https://unhead.unjs.io). If the data comes from a user or other untrusted source, we recommend you check out [`useHeadSafe`](/docs/4.x/api/composables/use-head-safe).
|
|
12
12
|
|
|
13
|
-
:read-more{to="/docs/getting-started/seo-meta"}
|
|
13
|
+
:read-more{to="/docs/4.x/getting-started/seo-meta"}
|
|
14
14
|
|
|
15
15
|
## Type
|
|
16
16
|
|
|
17
|
-
```ts
|
|
18
|
-
useHead(meta: MaybeComputedRef<MetaObject>): void
|
|
17
|
+
```ts [Signature]
|
|
18
|
+
export function useHead (meta: MaybeComputedRef<MetaObject>): void
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
Below are the non-reactive types for [`useHead`](/docs/api/composables/use-head) .
|
|
21
|
+
Below are the non-reactive types for [`useHead`](/docs/4.x/api/composables/use-head) .
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
interface MetaObject {
|
|
@@ -13,7 +13,7 @@ This is an advanced composable, primarily designed for use within plugins, mostl
|
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
::note
|
|
16
|
-
`useHydration` is designed to **ensure state synchronization and restoration during SSR**. If you need to create a globally reactive state that is SSR-friendly in Nuxt, [`useState`](/docs/api/composables/use-state) is the recommended choice.
|
|
16
|
+
`useHydration` is designed to **ensure state synchronization and restoration during SSR**. If you need to create a globally reactive state that is SSR-friendly in Nuxt, [`useState`](/docs/4.x/api/composables/use-state) is the recommended choice.
|
|
17
17
|
::
|
|
18
18
|
|
|
19
19
|
`useHydration` is a built-in composable that provides a way to set data on the server side every time a new HTTP request is made and receive that data on the client side. This way `useHydration` allows you to take full control of the hydration cycle.
|
|
@@ -47,9 +47,9 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
47
47
|
const myStore = new MyStore()
|
|
48
48
|
|
|
49
49
|
useHydration(
|
|
50
|
-
'myStoreState',
|
|
51
|
-
() => myStore.getState(),
|
|
52
|
-
|
|
50
|
+
'myStoreState',
|
|
51
|
+
() => myStore.getState(),
|
|
52
|
+
data => myStore.setState(data),
|
|
53
53
|
)
|
|
54
54
|
})
|
|
55
55
|
```
|
|
@@ -57,8 +57,8 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
57
57
|
|
|
58
58
|
## Type
|
|
59
59
|
|
|
60
|
-
```ts [
|
|
61
|
-
useHydration
|
|
60
|
+
```ts [Signature]
|
|
61
|
+
export function useHydration<T> (key: string, get: () => T, set: (value: T) => void): void
|
|
62
62
|
```
|
|
63
63
|
|
|
64
64
|
## Parameters
|
|
@@ -10,13 +10,13 @@ links:
|
|
|
10
10
|
|
|
11
11
|
## Description
|
|
12
12
|
|
|
13
|
-
By default, [`useAsyncData`](/docs/api/composables/use-async-data) blocks navigation until its async handler is resolved. `useLazyAsyncData` provides a wrapper around [`useAsyncData`](/docs/api/composables/use-async-data) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
13
|
+
By default, [`useAsyncData`](/docs/4.x/api/composables/use-async-data) blocks navigation until its async handler is resolved. `useLazyAsyncData` provides a wrapper around [`useAsyncData`](/docs/4.x/api/composables/use-async-data) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
14
14
|
|
|
15
15
|
::note
|
|
16
|
-
`useLazyAsyncData` has the same signature as [`useAsyncData`](/docs/api/composables/use-async-data).
|
|
16
|
+
`useLazyAsyncData` has the same signature as [`useAsyncData`](/docs/4.x/api/composables/use-async-data).
|
|
17
17
|
::
|
|
18
18
|
|
|
19
|
-
:read-more{to="/docs/api/composables/use-async-data"}
|
|
19
|
+
:read-more{to="/docs/4.x/api/composables/use-async-data"}
|
|
20
20
|
|
|
21
21
|
## Example
|
|
22
22
|
|
|
@@ -44,4 +44,4 @@ watch(count, (newCount) => {
|
|
|
44
44
|
`useLazyAsyncData` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyAsyncData`.
|
|
45
45
|
::
|
|
46
46
|
|
|
47
|
-
:read-more{to="/docs/getting-started/data-fetching"}
|
|
47
|
+
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -10,17 +10,17 @@ links:
|
|
|
10
10
|
|
|
11
11
|
## Description
|
|
12
12
|
|
|
13
|
-
By default, [`useFetch`](/docs/api/composables/use-fetch) blocks navigation until its async handler is resolved. `useLazyFetch` provides a wrapper around [`useFetch`](/docs/api/composables/use-fetch) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
13
|
+
By default, [`useFetch`](/docs/4.x/api/composables/use-fetch) blocks navigation until its async handler is resolved. `useLazyFetch` provides a wrapper around [`useFetch`](/docs/4.x/api/composables/use-fetch) that triggers navigation before the handler is resolved by setting the `lazy` option to `true`.
|
|
14
14
|
|
|
15
15
|
::note
|
|
16
|
-
`useLazyFetch` has the same signature as [`useFetch`](/docs/api/composables/use-fetch).
|
|
16
|
+
`useLazyFetch` has the same signature as [`useFetch`](/docs/4.x/api/composables/use-fetch).
|
|
17
17
|
::
|
|
18
18
|
|
|
19
19
|
::note
|
|
20
20
|
Awaiting `useLazyFetch` in this mode only ensures the call is initialized. On client-side navigation, data may not be immediately available, and you should make sure to handle the pending state in your app.
|
|
21
21
|
::
|
|
22
22
|
|
|
23
|
-
:read-more{to="/docs/api/composables/use-fetch"}
|
|
23
|
+
:read-more{to="/docs/4.x/api/composables/use-fetch"}
|
|
24
24
|
|
|
25
25
|
## Example
|
|
26
26
|
|
|
@@ -52,4 +52,4 @@ watch(posts, (newPosts) => {
|
|
|
52
52
|
`useLazyFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyFetch`.
|
|
53
53
|
::
|
|
54
54
|
|
|
55
|
-
:read-more{to="/docs/getting-started/data-fetching"}
|
|
55
|
+
:read-more{to="/docs/4.x/getting-started/data-fetching"}
|
|
@@ -10,8 +10,8 @@ links:
|
|
|
10
10
|
|
|
11
11
|
## Description
|
|
12
12
|
|
|
13
|
-
A composable which returns the loading state of the page. Used by [`<NuxtLoadingIndicator>`](/docs/api/components/nuxt-loading-indicator) and controllable.
|
|
14
|
-
It hooks into [`page:loading:start`](/docs/api/advanced/hooks#app-hooks-runtime) and [`page:loading:end`](/docs/api/advanced/hooks#app-hooks-runtime) to change its state.
|
|
13
|
+
A composable which returns the loading state of the page. Used by [`<NuxtLoadingIndicator>`](/docs/4.x/api/components/nuxt-loading-indicator) and controllable.
|
|
14
|
+
It hooks into [`page:loading:start`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) and [`page:loading:end`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) to change its state.
|
|
15
15
|
|
|
16
16
|
## Parameters
|
|
17
17
|
|
|
@@ -58,20 +58,20 @@ Used by `finish()`. Clear all timers and intervals used by the composable.
|
|
|
58
58
|
|
|
59
59
|
```vue
|
|
60
60
|
<script setup lang="ts">
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const { progress, isLoading, start, finish, clear } = useLoadingIndicator({
|
|
62
|
+
duration: 2000,
|
|
63
|
+
throttle: 200,
|
|
64
|
+
// This is how progress is calculated by default
|
|
65
|
+
estimatedProgress: (duration, elapsed) => (2 / Math.PI * 100) * Math.atan(elapsed / duration * 100 / 50),
|
|
66
|
+
})
|
|
67
67
|
</script>
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
```vue
|
|
71
71
|
<script setup lang="ts">
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
const { start, set } = useLoadingIndicator()
|
|
73
|
+
// same as set(0, { force: true })
|
|
74
|
+
// set the progress to 0, and show loading immediately
|
|
75
|
+
start({ force: true })
|
|
76
76
|
</script>
|
|
77
77
|
```
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
`useNuxtApp` is a built-in composable that provides a way to access shared runtime context of Nuxt, also known as the [Nuxt context](/docs/guide/going-further/nuxt-app#the-nuxt-context), which is available on both client and server side (but not within Nitro routes). It helps you access the Vue app instance, runtime hooks, runtime config variables and internal states, such as `ssrContext` and `payload`.
|
|
11
|
+
`useNuxtApp` is a built-in composable that provides a way to access shared runtime context of Nuxt, also known as the [Nuxt context](/docs/4.x/guide/going-further/nuxt-app#the-nuxt-context), which is available on both client and server side (but not within Nitro routes). It helps you access the Vue app instance, runtime hooks, runtime config variables and internal states, such as `ssrContext` and `payload`.
|
|
12
12
|
|
|
13
13
|
```vue [app/app.vue]
|
|
14
14
|
<script setup lang="ts">
|
|
@@ -16,11 +16,11 @@ const nuxtApp = useNuxtApp()
|
|
|
16
16
|
</script>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
If runtime context is unavailable in your scope, `useNuxtApp` will throw an exception when called. You can use [`tryUseNuxtApp`](#tryusenuxtapp) instead for composables that do not require `nuxtApp`, or to simply check if context is available or not without an exception.
|
|
19
|
+
If runtime context is unavailable in your scope, `useNuxtApp` will throw an exception when called. You can use [`tryUseNuxtApp`](/docs/4.x/api/composables/use-nuxt-app#tryusenuxtapp) instead for composables that do not require `nuxtApp`, or to simply check if context is available or not without an exception.
|
|
20
20
|
|
|
21
21
|
<!--
|
|
22
22
|
note
|
|
23
|
-
By default, the shared runtime context of Nuxt is namespaced under the [`buildId`](/docs/api/nuxt-config#buildid) option. It allows the support of multiple runtime contexts.
|
|
23
|
+
By default, the shared runtime context of Nuxt is namespaced under the [`buildId`](/docs/4.x/api/nuxt-config#buildid) option. It allows the support of multiple runtime contexts.
|
|
24
24
|
|
|
25
25
|
## Params
|
|
26
26
|
|
|
@@ -30,13 +30,13 @@ By default, the shared runtime context of Nuxt is namespaced under the [`buildId
|
|
|
30
30
|
|
|
31
31
|
### `provide (name, value)`
|
|
32
32
|
|
|
33
|
-
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/guide/directory-structure/plugins). Use the `provide` function to create Nuxt plugins to make values and helper methods available in your Nuxt application across all composables and components.
|
|
33
|
+
`nuxtApp` is a runtime context that you can extend using [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins). Use the `provide` function to create Nuxt plugins to make values and helper methods available in your Nuxt application across all composables and components.
|
|
34
34
|
|
|
35
35
|
`provide` function accepts `name` and `value` parameters.
|
|
36
36
|
|
|
37
|
-
```
|
|
37
|
+
```ts
|
|
38
38
|
const nuxtApp = useNuxtApp()
|
|
39
|
-
nuxtApp.provide('hello',
|
|
39
|
+
nuxtApp.provide('hello', name => `Hello ${name}!`)
|
|
40
40
|
|
|
41
41
|
// Prints "Hello name!"
|
|
42
42
|
console.log(nuxtApp.$hello('name'))
|
|
@@ -46,11 +46,11 @@ As you can see in the example above, `$hello` has become the new and custom part
|
|
|
46
46
|
|
|
47
47
|
### `hook(name, cb)`
|
|
48
48
|
|
|
49
|
-
Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue.js composables and [Nuxt plugins](/docs/guide/directory-structure/plugins) to hook into the rendering lifecycle.
|
|
49
|
+
Hooks available in `nuxtApp` allows you to customize the runtime aspects of your Nuxt application. You can use runtime hooks in Vue.js composables and [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins) to hook into the rendering lifecycle.
|
|
50
50
|
|
|
51
51
|
`hook` function is useful for adding custom logic by hooking into the rendering lifecycle at a specific point. `hook` function is mostly used when creating Nuxt plugins.
|
|
52
52
|
|
|
53
|
-
See [Runtime Hooks](/docs/api/advanced/hooks#app-hooks-runtime) for available runtime hooks called by Nuxt.
|
|
53
|
+
See [Runtime Hooks](/docs/4.x/api/advanced/hooks#app-hooks-runtime) for available runtime hooks called by Nuxt.
|
|
54
54
|
|
|
55
55
|
```ts [app/plugins/test.ts]
|
|
56
56
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
@@ -84,8 +84,8 @@ await nuxtApp.callHook('my-plugin:init')
|
|
|
84
84
|
|
|
85
85
|
Some useful methods:
|
|
86
86
|
- [`component()`](https://vuejs.org/api/application.html#app-component) - Registers a global component if passing both a name string and a component definition, or retrieves an already registered one if only the name is passed.
|
|
87
|
-
- [`directive()`](https://vuejs.org/api/application.html#app-directive) - Registers a global custom directive if passing both a name string and a directive definition, or retrieves an already registered one if only the name is passed[(example)](/docs/guide/directory-structure/plugins#vue-directives).
|
|
88
|
-
- [`use()`](https://vuejs.org/api/application.html#app-use) - Installs a **[Vue.js Plugin](https://vuejs.org/guide/reusability/plugins.html)** [(example)](/docs/guide/directory-structure/plugins#vue-plugins).
|
|
87
|
+
- [`directive()`](https://vuejs.org/api/application.html#app-directive) - Registers a global custom directive if passing both a name string and a directive definition, or retrieves an already registered one if only the name is passed[(example)](/docs/4.x/guide/directory-structure/plugins#vue-directives).
|
|
88
|
+
- [`use()`](https://vuejs.org/api/application.html#app-use) - Installs a **[Vue.js Plugin](https://vuejs.org/guide/reusability/plugins.html)** [(example)](/docs/4.x/guide/directory-structure/plugins#vue-plugins).
|
|
89
89
|
|
|
90
90
|
:read-more{icon="i-simple-icons-vuedotjs" to="https://vuejs.org/api/application.html#application-api"}
|
|
91
91
|
|
|
@@ -103,7 +103,7 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
103
103
|
`payload` exposes data and state variables from server side to client side. The following keys will be available on the client after they have been passed from the server side:
|
|
104
104
|
|
|
105
105
|
- `serverRendered` (boolean) - Indicates if response is server-side-rendered.
|
|
106
|
-
- `data` (object) - When you fetch the data from an API endpoint using either [`useFetch`](/docs/api/composables/use-fetch) or [`useAsyncData`](/docs/api/composables/use-async-data) , resulting payload can be accessed from the `payload.data`. This data is cached and helps you prevent fetching the same data in case an identical request is made more than once.
|
|
106
|
+
- `data` (object) - When you fetch the data from an API endpoint using either [`useFetch`](/docs/4.x/api/composables/use-fetch) or [`useAsyncData`](/docs/4.x/api/composables/use-async-data) , resulting payload can be accessed from the `payload.data`. This data is cached and helps you prevent fetching the same data in case an identical request is made more than once.
|
|
107
107
|
|
|
108
108
|
::code-group
|
|
109
109
|
```vue [app/app.vue]
|
|
@@ -112,17 +112,17 @@ Nuxt exposes the following properties through `ssrContext`:
|
|
|
112
112
|
</script>
|
|
113
113
|
```
|
|
114
114
|
```ts [server/api/count.ts]
|
|
115
|
-
export default defineEventHandler(event => {
|
|
115
|
+
export default defineEventHandler((event) => {
|
|
116
116
|
return { count: 1 }
|
|
117
117
|
})
|
|
118
118
|
```
|
|
119
119
|
::
|
|
120
120
|
|
|
121
|
-
After fetching the value of `count` using [`useAsyncData`](/docs/api/composables/use-async-data) in the example above, if you access `payload.data`, you will see `{ count: 1 }` recorded there.
|
|
121
|
+
After fetching the value of `count` using [`useAsyncData`](/docs/4.x/api/composables/use-async-data) in the example above, if you access `payload.data`, you will see `{ count: 1 }` recorded there.
|
|
122
122
|
|
|
123
|
-
When accessing the same `payload.data` from [`ssrcontext`](#ssrcontext), you can access the same value on the server side as well.
|
|
123
|
+
When accessing the same `payload.data` from [`ssrcontext`](/docs/4.x/api/composables/use-nuxt-app#ssrcontext), you can access the same value on the server side as well.
|
|
124
124
|
|
|
125
|
-
- `state` (object) - When you use [`useState`](/docs/api/composables/use-state) composable in Nuxt to set shared state, this state data is accessed through `payload.state.[name-of-your-state]`.
|
|
125
|
+
- `state` (object) - When you use [`useState`](/docs/4.x/api/composables/use-state) composable in Nuxt to set shared state, this state data is accessed through `payload.state.[name-of-your-state]`.
|
|
126
126
|
|
|
127
127
|
```ts [app/plugins/my-plugin.ts]
|
|
128
128
|
export const useColor = () => useState<string>('color', () => 'pink')
|
|
@@ -173,7 +173,7 @@ export default defineComponent({
|
|
|
173
173
|
// ...
|
|
174
174
|
}
|
|
175
175
|
})
|
|
176
|
-
}
|
|
176
|
+
},
|
|
177
177
|
})
|
|
178
178
|
```
|
|
179
179
|
|
|
@@ -204,7 +204,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
|
204
204
|
|
|
205
205
|
#### Usage
|
|
206
206
|
|
|
207
|
-
```
|
|
207
|
+
```ts
|
|
208
208
|
const result = nuxtApp.runWithContext(() => functionWithContext())
|
|
209
209
|
```
|
|
210
210
|
|
|
@@ -218,14 +218,14 @@ Vue.js Composition API (and Nuxt composables similarly) work by depending on an
|
|
|
218
218
|
|
|
219
219
|
What it does mean? The Composition API and Nuxt Composables are only available during lifecycle and in same tick before any async operation:
|
|
220
220
|
|
|
221
|
-
```
|
|
221
|
+
```ts
|
|
222
222
|
// --- Vue internal ---
|
|
223
223
|
const _vueInstance = null
|
|
224
224
|
const getCurrentInstance = () => _vueInstance
|
|
225
225
|
// ---
|
|
226
226
|
|
|
227
227
|
// Vue / Nuxt sets a global variable referencing to current component in _vueInstance when calling setup()
|
|
228
|
-
async function setup() {
|
|
228
|
+
async function setup () {
|
|
229
229
|
getCurrentInstance() // Works
|
|
230
230
|
await someAsyncOperation() // Vue unsets the context in same tick before async operation!
|
|
231
231
|
getCurrentInstance() // null
|
|
@@ -236,7 +236,7 @@ The classic solution to this, is caching the current instance on first call to a
|
|
|
236
236
|
|
|
237
237
|
To overcome this limitation, Vue does some behind the scenes work when compiling our application code and restores context after each call for `<script setup>`:
|
|
238
238
|
|
|
239
|
-
```
|
|
239
|
+
```ts
|
|
240
240
|
const __instance = getCurrentInstance() // Generated by Vue compiler
|
|
241
241
|
getCurrentInstance() // Works!
|
|
242
242
|
await someAsyncOperation() // Vue unsets the context
|
|
@@ -268,7 +268,7 @@ Using a new experimental feature, it is possible to enable native async context
|
|
|
268
268
|
Native async context support works currently in Bun and Node.
|
|
269
269
|
::
|
|
270
270
|
|
|
271
|
-
:read-more{to="/docs/guide/going-further/experimental-features#asynccontext"}
|
|
271
|
+
:read-more{to="/docs/4.x/guide/going-further/experimental-features#asynccontext"}
|
|
272
272
|
|
|
273
273
|
## tryUseNuxtApp
|
|
274
274
|
|
|
@@ -279,7 +279,7 @@ You can use it for composables that do not require `nuxtApp`, or to simply check
|
|
|
279
279
|
Example usage:
|
|
280
280
|
|
|
281
281
|
```ts [composable.ts]
|
|
282
|
-
export function useStandType() {
|
|
282
|
+
export function useStandType () {
|
|
283
283
|
// Always works on the client
|
|
284
284
|
if (tryUseNuxtApp()) {
|
|
285
285
|
return useRuntimeConfig().public.STAND_TYPE
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
::note
|
|
12
|
-
`useNuxtData` gives you access to the current cached value of [`useAsyncData`](/docs/api/composables/use-async-data) , [`useLazyAsyncData`](/docs/api/composables/use-lazy-async-data), [`useFetch`](/docs/api/composables/use-fetch) and [`useLazyFetch`](/docs/api/composables/use-lazy-fetch) with explicitly provided key.
|
|
12
|
+
`useNuxtData` gives you access to the current cached value of [`useAsyncData`](/docs/4.x/api/composables/use-async-data) , [`useLazyAsyncData`](/docs/4.x/api/composables/use-lazy-async-data), [`useFetch`](/docs/4.x/api/composables/use-fetch) and [`useLazyFetch`](/docs/4.x/api/composables/use-lazy-fetch) with explicitly provided key.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
@@ -50,10 +50,10 @@ const route = useRoute()
|
|
|
50
50
|
|
|
51
51
|
const { data } = useLazyFetch(`/api/posts/${route.params.id}`, {
|
|
52
52
|
key: `post-${route.params.id}`,
|
|
53
|
-
default() {
|
|
53
|
+
default () {
|
|
54
54
|
// Find the individual post from the cache and set it as the default value.
|
|
55
55
|
return posts.value.find(post => post.id === route.params.id)
|
|
56
|
-
}
|
|
56
|
+
},
|
|
57
57
|
})
|
|
58
58
|
</script>
|
|
59
59
|
```
|
|
@@ -80,10 +80,10 @@ let previousTodos = []
|
|
|
80
80
|
const { data: todos } = useNuxtData('todos')
|
|
81
81
|
|
|
82
82
|
async function addTodo () {
|
|
83
|
-
|
|
83
|
+
await $fetch('/api/addTodo', {
|
|
84
84
|
method: 'post',
|
|
85
85
|
body: {
|
|
86
|
-
todo: newTodo.value
|
|
86
|
+
todo: newTodo.value,
|
|
87
87
|
},
|
|
88
88
|
onRequest () {
|
|
89
89
|
// Store the previously cached value to restore if fetch fails.
|
|
@@ -99,7 +99,7 @@ async function addTodo () {
|
|
|
99
99
|
async onResponse () {
|
|
100
100
|
// Invalidate todos in the background if the request succeeded.
|
|
101
101
|
await refreshNuxtData('todos')
|
|
102
|
-
}
|
|
102
|
+
},
|
|
103
103
|
})
|
|
104
104
|
}
|
|
105
105
|
</script>
|
|
@@ -107,6 +107,6 @@ async function addTodo () {
|
|
|
107
107
|
|
|
108
108
|
## Type
|
|
109
109
|
|
|
110
|
-
```ts
|
|
111
|
-
useNuxtData<DataT = any> (key: string): { data: Ref<DataT | undefined> }
|
|
110
|
+
```ts [Signature]
|
|
111
|
+
export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | undefined> }
|
|
112
112
|
```
|