@nuxt/docs 0.0.0 → 3.17.1

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.
Files changed (224) hide show
  1. package/.navigation.yml +2 -0
  2. package/1.getting-started/.navigation.yml +3 -0
  3. package/1.getting-started/01.introduction.md +81 -0
  4. package/1.getting-started/02.installation.md +109 -0
  5. package/1.getting-started/03.configuration.md +226 -0
  6. package/1.getting-started/04.views.md +163 -0
  7. package/1.getting-started/05.assets.md +48 -0
  8. package/1.getting-started/06.styling.md +565 -0
  9. package/1.getting-started/07.routing.md +149 -0
  10. package/1.getting-started/08.seo-meta.md +360 -0
  11. package/1.getting-started/09.transitions.md +473 -0
  12. package/1.getting-started/10.data-fetching.md +795 -0
  13. package/1.getting-started/11.state-management.md +223 -0
  14. package/1.getting-started/12.error-handling.md +233 -0
  15. package/1.getting-started/13.server.md +94 -0
  16. package/1.getting-started/14.layers.md +92 -0
  17. package/1.getting-started/15.prerendering.md +194 -0
  18. package/1.getting-started/16.deployment.md +130 -0
  19. package/1.getting-started/17.testing.md +728 -0
  20. package/1.getting-started/18.upgrade.md +997 -0
  21. package/2.guide/.navigation.yml +2 -0
  22. package/2.guide/0.index.md +22 -0
  23. package/2.guide/1.concepts/.navigation.yml +3 -0
  24. package/2.guide/1.concepts/1.auto-imports.md +205 -0
  25. package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
  26. package/2.guide/1.concepts/2.vuejs-development.md +103 -0
  27. package/2.guide/1.concepts/3.rendering.md +255 -0
  28. package/2.guide/1.concepts/4.server-engine.md +62 -0
  29. package/2.guide/1.concepts/5.modules.md +48 -0
  30. package/2.guide/1.concepts/7.esm.md +299 -0
  31. package/2.guide/1.concepts/8.typescript.md +97 -0
  32. package/2.guide/1.concepts/9.code-style.md +22 -0
  33. package/2.guide/2.directory-structure/.navigation.yml +3 -0
  34. package/2.guide/2.directory-structure/0.nuxt.md +20 -0
  35. package/2.guide/2.directory-structure/0.output.md +18 -0
  36. package/2.guide/2.directory-structure/1.assets.md +16 -0
  37. package/2.guide/2.directory-structure/1.components.md +608 -0
  38. package/2.guide/2.directory-structure/1.composables.md +121 -0
  39. package/2.guide/2.directory-structure/1.content.md +64 -0
  40. package/2.guide/2.directory-structure/1.layouts.md +180 -0
  41. package/2.guide/2.directory-structure/1.middleware.md +209 -0
  42. package/2.guide/2.directory-structure/1.modules.md +66 -0
  43. package/2.guide/2.directory-structure/1.node_modules.md +12 -0
  44. package/2.guide/2.directory-structure/1.pages.md +440 -0
  45. package/2.guide/2.directory-structure/1.plugins.md +299 -0
  46. package/2.guide/2.directory-structure/1.public.md +27 -0
  47. package/2.guide/2.directory-structure/1.server.md +546 -0
  48. package/2.guide/2.directory-structure/1.shared.md +104 -0
  49. package/2.guide/2.directory-structure/1.utils.md +49 -0
  50. package/2.guide/2.directory-structure/2.env.md +75 -0
  51. package/2.guide/2.directory-structure/2.gitignore.md +37 -0
  52. package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
  53. package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
  54. package/2.guide/2.directory-structure/3.app-config.md +177 -0
  55. package/2.guide/2.directory-structure/3.app.md +72 -0
  56. package/2.guide/2.directory-structure/3.error.md +55 -0
  57. package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
  58. package/2.guide/2.directory-structure/3.package.md +32 -0
  59. package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
  60. package/2.guide/3.going-further/.navigation.yml +3 -0
  61. package/2.guide/3.going-further/1.experimental-features.md +689 -0
  62. package/2.guide/3.going-further/1.features.md +103 -0
  63. package/2.guide/3.going-further/1.internals.md +81 -0
  64. package/2.guide/3.going-further/10.runtime-config.md +174 -0
  65. package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
  66. package/2.guide/3.going-further/2.hooks.md +98 -0
  67. package/2.guide/3.going-further/3.modules.md +811 -0
  68. package/2.guide/3.going-further/4.kit.md +51 -0
  69. package/2.guide/3.going-further/6.nuxt-app.md +64 -0
  70. package/2.guide/3.going-further/7.layers.md +227 -0
  71. package/2.guide/3.going-further/9.debugging.md +115 -0
  72. package/2.guide/3.going-further/index.md +4 -0
  73. package/2.guide/4.recipes/.navigation.yml +3 -0
  74. package/2.guide/4.recipes/1.custom-routing.md +181 -0
  75. package/2.guide/4.recipes/2.vite-plugin.md +65 -0
  76. package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
  77. package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
  78. package/3.api/.navigation.yml +3 -0
  79. package/3.api/1.components/.navigation.yml +3 -0
  80. package/3.api/1.components/1.client-only.md +76 -0
  81. package/3.api/1.components/1.dev-only.md +51 -0
  82. package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
  83. package/3.api/1.components/10.nuxt-picture.md +27 -0
  84. package/3.api/1.components/11.teleports.md +40 -0
  85. package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
  86. package/3.api/1.components/13.nuxt-time.md +173 -0
  87. package/3.api/1.components/2.nuxt-page.md +154 -0
  88. package/3.api/1.components/3.nuxt-layout.md +156 -0
  89. package/3.api/1.components/4.nuxt-link.md +322 -0
  90. package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
  91. package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
  92. package/3.api/1.components/7.nuxt-welcome.md +25 -0
  93. package/3.api/1.components/8.nuxt-island.md +70 -0
  94. package/3.api/1.components/9.nuxt-img.md +43 -0
  95. package/3.api/2.composables/.navigation.yml +3 -0
  96. package/3.api/2.composables/on-prehydrate.md +60 -0
  97. package/3.api/2.composables/use-app-config.md +19 -0
  98. package/3.api/2.composables/use-async-data.md +212 -0
  99. package/3.api/2.composables/use-cookie.md +233 -0
  100. package/3.api/2.composables/use-error.md +32 -0
  101. package/3.api/2.composables/use-fetch.md +217 -0
  102. package/3.api/2.composables/use-head-safe.md +55 -0
  103. package/3.api/2.composables/use-head.md +69 -0
  104. package/3.api/2.composables/use-hydration.md +68 -0
  105. package/3.api/2.composables/use-lazy-async-data.md +47 -0
  106. package/3.api/2.composables/use-lazy-fetch.md +55 -0
  107. package/3.api/2.composables/use-loading-indicator.md +77 -0
  108. package/3.api/2.composables/use-nuxt-app.md +294 -0
  109. package/3.api/2.composables/use-nuxt-data.md +112 -0
  110. package/3.api/2.composables/use-preview-mode.md +118 -0
  111. package/3.api/2.composables/use-request-event.md +23 -0
  112. package/3.api/2.composables/use-request-fetch.md +52 -0
  113. package/3.api/2.composables/use-request-header.md +34 -0
  114. package/3.api/2.composables/use-request-headers.md +37 -0
  115. package/3.api/2.composables/use-request-url.md +41 -0
  116. package/3.api/2.composables/use-response-header.md +48 -0
  117. package/3.api/2.composables/use-route-announcer.md +60 -0
  118. package/3.api/2.composables/use-route.md +52 -0
  119. package/3.api/2.composables/use-router.md +92 -0
  120. package/3.api/2.composables/use-runtime-config.md +142 -0
  121. package/3.api/2.composables/use-runtime-hook.md +43 -0
  122. package/3.api/2.composables/use-seo-meta.md +80 -0
  123. package/3.api/2.composables/use-server-seo-meta.md +27 -0
  124. package/3.api/2.composables/use-state.md +48 -0
  125. package/3.api/3.utils/$fetch.md +98 -0
  126. package/3.api/3.utils/.navigation.yml +3 -0
  127. package/3.api/3.utils/abort-navigation.md +73 -0
  128. package/3.api/3.utils/add-route-middleware.md +88 -0
  129. package/3.api/3.utils/call-once.md +92 -0
  130. package/3.api/3.utils/clear-error.md +29 -0
  131. package/3.api/3.utils/clear-nuxt-data.md +23 -0
  132. package/3.api/3.utils/clear-nuxt-state.md +23 -0
  133. package/3.api/3.utils/create-error.md +55 -0
  134. package/3.api/3.utils/define-nuxt-component.md +53 -0
  135. package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
  136. package/3.api/3.utils/define-page-meta.md +234 -0
  137. package/3.api/3.utils/define-route-rules.md +52 -0
  138. package/3.api/3.utils/navigate-to.md +230 -0
  139. package/3.api/3.utils/on-before-route-leave.md +11 -0
  140. package/3.api/3.utils/on-before-route-update.md +11 -0
  141. package/3.api/3.utils/on-nuxt-ready.md +25 -0
  142. package/3.api/3.utils/prefetch-components.md +28 -0
  143. package/3.api/3.utils/preload-components.md +23 -0
  144. package/3.api/3.utils/preload-route-components.md +41 -0
  145. package/3.api/3.utils/prerender-routes.md +46 -0
  146. package/3.api/3.utils/refresh-cookie.md +46 -0
  147. package/3.api/3.utils/refresh-nuxt-data.md +91 -0
  148. package/3.api/3.utils/reload-nuxt-app.md +74 -0
  149. package/3.api/3.utils/set-page-layout.md +24 -0
  150. package/3.api/3.utils/set-response-status.md +36 -0
  151. package/3.api/3.utils/show-error.md +31 -0
  152. package/3.api/3.utils/update-app-config.md +27 -0
  153. package/3.api/4.commands/.navigation.yml +3 -0
  154. package/3.api/4.commands/add.md +112 -0
  155. package/3.api/4.commands/analyze.md +41 -0
  156. package/3.api/4.commands/build-module.md +42 -0
  157. package/3.api/4.commands/build.md +46 -0
  158. package/3.api/4.commands/cleanup.md +38 -0
  159. package/3.api/4.commands/dev.md +59 -0
  160. package/3.api/4.commands/devtools.md +38 -0
  161. package/3.api/4.commands/generate.md +41 -0
  162. package/3.api/4.commands/info.md +33 -0
  163. package/3.api/4.commands/init.md +46 -0
  164. package/3.api/4.commands/module.md +84 -0
  165. package/3.api/4.commands/prepare.md +36 -0
  166. package/3.api/4.commands/preview.md +43 -0
  167. package/3.api/4.commands/typecheck.md +42 -0
  168. package/3.api/4.commands/upgrade.md +37 -0
  169. package/3.api/5.kit/.navigation.yml +3 -0
  170. package/3.api/5.kit/1.modules.md +172 -0
  171. package/3.api/5.kit/10.runtime-config.md +27 -0
  172. package/3.api/5.kit/10.templates.md +283 -0
  173. package/3.api/5.kit/11.nitro.md +409 -0
  174. package/3.api/5.kit/12.resolving.md +268 -0
  175. package/3.api/5.kit/13.logging.md +65 -0
  176. package/3.api/5.kit/14.builder.md +491 -0
  177. package/3.api/5.kit/15.examples.md +41 -0
  178. package/3.api/5.kit/2.programmatic.md +125 -0
  179. package/3.api/5.kit/3.compatibility.md +230 -0
  180. package/3.api/5.kit/4.autoimports.md +144 -0
  181. package/3.api/5.kit/5.components.md +127 -0
  182. package/3.api/5.kit/6.context.md +130 -0
  183. package/3.api/5.kit/7.pages.md +295 -0
  184. package/3.api/5.kit/8.layout.md +80 -0
  185. package/3.api/5.kit/9.plugins.md +263 -0
  186. package/3.api/6.advanced/.navigation.yml +1 -0
  187. package/3.api/6.advanced/1.hooks.md +105 -0
  188. package/3.api/6.advanced/2.import-meta.md +60 -0
  189. package/3.api/6.nuxt-config.md +12 -0
  190. package/3.api/index.md +31 -0
  191. package/5.community/.navigation.yml +3 -0
  192. package/5.community/2.getting-help.md +48 -0
  193. package/5.community/3.reporting-bugs.md +50 -0
  194. package/5.community/4.contribution.md +205 -0
  195. package/5.community/5.framework-contribution.md +142 -0
  196. package/5.community/6.roadmap.md +79 -0
  197. package/5.community/7.changelog.md +92 -0
  198. package/6.bridge/.navigation.yml +3 -0
  199. package/6.bridge/1.overview.md +137 -0
  200. package/6.bridge/10.configuration.md +96 -0
  201. package/6.bridge/2.typescript.md +46 -0
  202. package/6.bridge/3.bridge-composition-api.md +132 -0
  203. package/6.bridge/4.plugins-and-middleware.md +65 -0
  204. package/6.bridge/5.nuxt3-compatible-api.md +204 -0
  205. package/6.bridge/6.meta.md +117 -0
  206. package/6.bridge/7.runtime-config.md +38 -0
  207. package/6.bridge/8.nitro.md +102 -0
  208. package/6.bridge/9.vite.md +37 -0
  209. package/7.migration/.navigation.yml +3 -0
  210. package/7.migration/1.overview.md +24 -0
  211. package/7.migration/10.bundling.md +28 -0
  212. package/7.migration/11.server.md +17 -0
  213. package/7.migration/2.configuration.md +240 -0
  214. package/7.migration/20.module-authors.md +94 -0
  215. package/7.migration/3.auto-imports.md +18 -0
  216. package/7.migration/4.meta.md +127 -0
  217. package/7.migration/5.plugins-and-middleware.md +80 -0
  218. package/7.migration/6.pages-and-layouts.md +233 -0
  219. package/7.migration/7.component-options.md +156 -0
  220. package/7.migration/8.runtime-config.md +58 -0
  221. package/LICENSE +21 -0
  222. package/README.md +11 -0
  223. package/package.json +16 -4
  224. package/dist/.gitkeep +0 -0
@@ -0,0 +1,217 @@
1
+ ---
2
+ title: 'useFetch'
3
+ description: 'Fetch data from an API endpoint with an SSR-friendly composable.'
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/fetch.ts
8
+ size: xs
9
+ ---
10
+
11
+ This composable provides a convenient wrapper around [`useAsyncData`](/docs/api/composables/use-async-data) and [`$fetch`](/docs/api/utils/dollarfetch).
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
+
14
+ ::note
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.
16
+ ::
17
+
18
+ ## Usage
19
+
20
+ ```vue [pages/modules.vue]
21
+ <script setup lang="ts">
22
+ const { data, status, error, refresh, clear } = await useFetch('/api/modules', {
23
+ pick: ['title']
24
+ })
25
+ </script>
26
+ ```
27
+
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-usefetch) for more information on how to make a custom async data fetcher.
30
+ ::
31
+
32
+ ::note
33
+ `data`, `status`, and `error` are Vue refs, and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` and `clear` are plain functions.
34
+ ::
35
+
36
+ Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ofetch](https://github.com/unjs/ofetch) and is using [unjs/ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
37
+
38
+ ```ts
39
+ const param1 = ref('value1')
40
+ const { data, status, error, refresh } = await useFetch('/api/modules', {
41
+ query: { param1, param2: 'value2' }
42
+ })
43
+ ```
44
+
45
+ The above example results in `https://api.nuxt.com/modules?param1=value1&param2=value2`.
46
+
47
+ You can also use [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interceptors):
48
+
49
+ ```ts
50
+ const { data, status, error, refresh, clear } = await useFetch('/api/auth/login', {
51
+ onRequest({ request, options }) {
52
+ // Set the request headers
53
+ // note that this relies on ofetch >= 1.4.0 - you may need to refresh your lockfile
54
+ options.headers.set('Authorization', '...')
55
+ },
56
+ onRequestError({ request, options, error }) {
57
+ // Handle the request errors
58
+ },
59
+ onResponse({ request, response, options }) {
60
+ // Process the response data
61
+ localStorage.setItem('token', response._data.token)
62
+ },
63
+ onResponseError({ request, response, options }) {
64
+ // Handle the response errors
65
+ }
66
+ })
67
+ ```
68
+
69
+ ### Reactive Keys and Shared State
70
+
71
+ You can use a computed ref or a plain ref as the URL, allowing for dynamic data fetching that automatically updates when the URL changes:
72
+
73
+ ```vue [pages/[id\\].vue]
74
+ <script setup lang="ts">
75
+ const route = useRoute()
76
+ const id = computed(() => route.params.id)
77
+
78
+ // When the route changes and id updates, the data will be automatically refetched
79
+ const { data: post } = await useFetch(() => `/api/posts/${id.value}`)
80
+ </script>
81
+ ```
82
+
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
+
85
+ ::warning
86
+ `useFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useFetch`.
87
+ ::
88
+
89
+ ::warning
90
+ If you encounter the `data` variable destructured from a `useFetch` returns a string and not a JSON parsed object then make sure your component doesn't include an import statement like `import { useFetch } from '@vueuse/core`.
91
+ ::
92
+
93
+ :video-accordion{title="Watch the video from Alexander Lichter to avoid using useFetch the wrong way" videoId="njsGVmcWviY"}
94
+
95
+ :link-example{to="/docs/examples/advanced/use-custom-fetch-composable"}
96
+
97
+ :read-more{to="/docs/getting-started/data-fetching"}
98
+
99
+ :link-example{to="/docs/examples/features/data-fetching"}
100
+
101
+ ## Params
102
+
103
+ - `URL`: The URL to fetch.
104
+ - `Options` (extends [unjs/ofetch](https://github.com/unjs/ofetch) options & [AsyncDataOptions](/docs/api/composables/use-async-data#params)):
105
+ - `method`: Request method.
106
+ - `query`: Adds query search params to URL using [ufo](https://github.com/unjs/ufo)
107
+ - `params`: Alias for `query`
108
+ - `body`: Request body - automatically stringified (if an object is passed).
109
+ - `headers`: Request headers.
110
+ - `baseURL`: Base URL for the request.
111
+ - `timeout`: Milliseconds to automatically abort request
112
+ - `cache`: Handles cache control according to [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/fetch#cache)
113
+ - You can pass boolean to disable the cache or you can pass one of the following values: `default`, `no-store`, `reload`, `no-cache`, `force-cache`, and `only-if-cached`.
114
+
115
+ ::note
116
+ 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.
117
+ ::
118
+
119
+ - `Options` (from [`useAsyncData`](/docs/api/composables/use-async-data)):
120
+ - `key`: a unique key to ensure that data fetching can be properly de-duplicated across requests, if not provided, it will be automatically generated based on URL and fetch options
121
+ - `server`: whether to fetch the data on the server (defaults to `true`)
122
+ - `lazy`: whether to resolve the async function after loading the route, instead of blocking client-side navigation (defaults to `false`)
123
+ - `immediate`: when set to `false`, will prevent the request from firing immediately. (defaults to `true`)
124
+ - `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
125
+ - `transform`: a function that can be used to alter `handler` function result after resolving
126
+ - `getCachedData`: Provide a function which returns cached data. A `null` or `undefined` return value will trigger a fetch. By default, this is:
127
+ ```ts
128
+ const getDefaultCachedData = (key, nuxtApp, ctx) => nuxtApp.isHydrating
129
+ ? nuxtApp.payload.data[key]
130
+ : nuxtApp.static.data[key]
131
+ ```
132
+ Which only caches data when `experimental.payloadExtraction` of `nuxt.config` is enabled.
133
+ - `pick`: only pick specified keys in this array from the `handler` function result
134
+ - `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`. (You can [see an example here](/docs/getting-started/data-fetching#watch) of using `watch`.)
135
+ - `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.
136
+ - `dedupe`: avoid fetching same key more than once at a time (defaults to `cancel`). Possible options:
137
+ - `cancel` - cancels existing requests when a new one is made
138
+ - `defer` - does not make new requests at all if there is a pending request
139
+
140
+ ::note
141
+ 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`.
142
+ ::
143
+
144
+ ::note
145
+ If you use `useFetch` to call an (external) HTTPS URL with a self-signed certificate in development, you will need to set `NODE_TLS_REJECT_UNAUTHORIZED=0` in your environment.
146
+ ::
147
+
148
+ :video-accordion{title="Watch a video from Alexander Lichter about client-side caching with getCachedData" videoId="aQPR0xn-MMk"}
149
+
150
+ ## Return Values
151
+
152
+ - `data`: the result of the asynchronous function that is passed in.
153
+ - `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
154
+ - `error`: an error object if the data fetching failed.
155
+ - `status`: a string indicating the status of the data request:
156
+ - `idle`: when the request has not started, such as:
157
+ - when `execute` has not yet been called and `{ immediate: false }` is set
158
+ - when rendering HTML on the server and `{ server: false }` is set
159
+ - `pending`: the request is in progress
160
+ - `success`: the request has completed successfully
161
+ - `error`: the request has failed
162
+ - `clear`: a function which will set `data` to `undefined`, set `error` to `null`, set `status` to `'idle'`, and mark any currently pending requests as cancelled.
163
+
164
+ By default, Nuxt waits until a `refresh` is finished before it can be executed again.
165
+
166
+ ::note
167
+ If you have not fetched data on the server (for example, with `server: false`), then the data _will not_ be fetched until hydration completes. This means even if you await `useFetch` on client-side, `data` will remain null within `<script setup>`.
168
+ ::
169
+
170
+ ## Type
171
+
172
+ ```ts [Signature]
173
+ function useFetch<DataT, ErrorT>(
174
+ url: string | Request | Ref<string | Request> | (() => string | Request),
175
+ options?: UseFetchOptions<DataT>
176
+ ): Promise<AsyncData<DataT, ErrorT>>
177
+
178
+ type UseFetchOptions<DataT> = {
179
+ key?: string
180
+ method?: string
181
+ query?: SearchParams
182
+ params?: SearchParams
183
+ body?: RequestInit['body'] | Record<string, any>
184
+ headers?: Record<string, string> | [key: string, value: string][] | Headers
185
+ baseURL?: string
186
+ server?: boolean
187
+ lazy?: boolean
188
+ immediate?: boolean
189
+ getCachedData?: (key: string, nuxtApp: NuxtApp, ctx: AsyncDataRequestContext) => DataT | undefined
190
+ deep?: boolean
191
+ dedupe?: 'cancel' | 'defer'
192
+ default?: () => DataT
193
+ transform?: (input: DataT) => DataT | Promise<DataT>
194
+ pick?: string[]
195
+ watch?: WatchSource[] | false
196
+ }
197
+
198
+ type AsyncDataRequestContext = {
199
+ /** The reason for this data request */
200
+ cause: 'initial' | 'refresh:manual' | 'refresh:hook' | 'watch'
201
+ }
202
+
203
+ type AsyncData<DataT, ErrorT> = {
204
+ data: Ref<DataT | null>
205
+ refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
206
+ execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
207
+ clear: () => void
208
+ error: Ref<ErrorT | null>
209
+ status: Ref<AsyncDataRequestStatus>
210
+ }
211
+
212
+ interface AsyncDataExecuteOptions {
213
+ dedupe?: 'cancel' | 'defer'
214
+ }
215
+
216
+ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
217
+ ```
@@ -0,0 +1,55 @@
1
+ ---
2
+ title: useHeadSafe
3
+ description: The recommended way to provide head data with user input.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/unjs/unhead/blob/main/packages/vue/src/composables.ts
8
+ size: xs
9
+ ---
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.
12
+
13
+ ## Usage
14
+
15
+ You can pass all the same values as [`useHead`](/docs/api/composables/use-head)
16
+
17
+ ```ts
18
+ useHeadSafe({
19
+ script: [
20
+ { id: 'xss-script', innerHTML: 'alert("xss")' }
21
+ ],
22
+ meta: [
23
+ { 'http-equiv': 'refresh', content: '0;javascript:alert(1)' }
24
+ ]
25
+ })
26
+ // Will safely generate
27
+ // <script id="xss-script"></script>
28
+ // <meta content="0;javascript:alert(1)">
29
+ ```
30
+
31
+ ::read-more{to="https://unhead.unjs.io/docs/typescript/head/api/composables/use-head-safe" target="_blank"}
32
+ Read more on the `Unhead` documentation.
33
+ ::
34
+
35
+ ## Type
36
+
37
+ ```ts
38
+ useHeadSafe(input: MaybeComputedRef<HeadSafe>): void
39
+ ```
40
+
41
+ The list of allowed values is:
42
+
43
+ ```ts
44
+ const WhitelistAttributes = {
45
+ htmlAttrs: ['class', 'style', 'lang', 'dir'],
46
+ bodyAttrs: ['class', 'style'],
47
+ meta: ['name', 'property', 'charset', 'content', 'media'],
48
+ noscript: ['textContent'],
49
+ style: ['media', 'textContent', 'nonce', 'title', 'blocking'],
50
+ script: ['type', 'textContent', 'nonce', 'blocking'],
51
+ link: ['color', 'crossorigin', 'fetchpriority', 'href', 'hreflang', 'imagesrcset', 'imagesizes', 'integrity', 'media', 'referrerpolicy', 'rel', 'sizes', 'type'],
52
+ }
53
+ ```
54
+
55
+ See [@unhead/vue](https://github.com/unjs/unhead/blob/main/packages/vue/src/types/safeSchema.ts) for more detailed types.
@@ -0,0 +1,69 @@
1
+ ---
2
+ title: useHead
3
+ description: useHead customizes the head properties of individual pages of your Nuxt app.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/unjs/unhead/blob/main/packages/vue/src/composables.ts
8
+ size: xs
9
+ ---
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).
12
+
13
+ :read-more{to="/docs/getting-started/seo-meta"}
14
+
15
+ ## Type
16
+
17
+ ```ts
18
+ useHead(meta: MaybeComputedRef<MetaObject>): void
19
+ ```
20
+
21
+ Below are the non-reactive types for [`useHead`](/docs/api/composables/use-head) .
22
+
23
+ ```ts
24
+ interface MetaObject {
25
+ title?: string
26
+ titleTemplate?: string | ((title?: string) => string)
27
+ base?: Base
28
+ link?: Link[]
29
+ meta?: Meta[]
30
+ style?: Style[]
31
+ script?: Script[]
32
+ noscript?: Noscript[]
33
+ htmlAttrs?: HtmlAttributes
34
+ bodyAttrs?: BodyAttributes
35
+ }
36
+ ```
37
+
38
+ See [@unhead/vue](https://github.com/unjs/unhead/blob/main/packages/vue/src/types/schema.ts) for more detailed types.
39
+
40
+ ::note
41
+ The properties of `useHead` can be dynamic, accepting `ref`, `computed` and `reactive` properties. `meta` parameter can also accept a function returning an object to make the entire object reactive.
42
+ ::
43
+
44
+ ## Params
45
+
46
+ ### `meta`
47
+
48
+ **Type**: `MetaObject`
49
+
50
+ An object accepting the following head metadata:
51
+
52
+ - `meta`: Each element in the array is mapped to a newly-created `<meta>` tag, where object properties are mapped to the corresponding attributes.
53
+ - **Type**: `Array<Record<string, any>>`
54
+ - `link`: Each element in the array is mapped to a newly-created `<link>` tag, where object properties are mapped to the corresponding attributes.
55
+ - **Type**: `Array<Record<string, any>>`
56
+ - `style`: Each element in the array is mapped to a newly-created `<style>` tag, where object properties are mapped to the corresponding attributes.
57
+ - **Type**: `Array<Record<string, any>>`
58
+ - `script`: Each element in the array is mapped to a newly-created `<script>` tag, where object properties are mapped to the corresponding attributes.
59
+ - **Type**: `Array<Record<string, any>>`
60
+ - `noscript`: Each element in the array is mapped to a newly-created `<noscript>` tag, where object properties are mapped to the corresponding attributes.
61
+ - **Type**: `Array<Record<string, any>>`
62
+ - `titleTemplate`: Configures dynamic template to customize the page title on an individual page.
63
+ - **Type**: `string` | `((title: string) => string)`
64
+ - `title`: Sets static page title on an individual page.
65
+ - **Type**: `string`
66
+ - `bodyAttrs`: Sets attributes of the `<body>` tag. Each object property is mapped to the corresponding attribute.
67
+ - **Type**: `Record<string, any>`
68
+ - `htmlAttrs`: Sets attributes of the `<html>` tag. Each object property is mapped to the corresponding attribute.
69
+ - **Type**: `Record<string, any>`
@@ -0,0 +1,68 @@
1
+ ---
2
+ title: 'useHydration'
3
+ description: 'Allows full control of the hydration cycle to set and receive data from the server.'
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/hydrate.ts
8
+ size: xs
9
+ ---
10
+
11
+ ::note
12
+ This is an advanced composable, primarily designed for use within plugins, mostly used by Nuxt modules.
13
+ ::
14
+
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.
17
+ ::
18
+
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.
20
+
21
+ The data returned from the `get` function on the server is stored in `nuxtApp.payload` under the unique key provided as the first parameter to `useHydration`. During hydration, this data is then retrieved on the client, preventing redundant computations or API calls.
22
+
23
+ ## Usage
24
+
25
+ ::code-group
26
+
27
+ ```ts [Without useHydration]
28
+ export default defineNuxtPlugin((nuxtApp) => {
29
+ const myStore = new MyStore()
30
+
31
+ if (import.meta.server) {
32
+ nuxt.hooks.hook('app:rendered', () => {
33
+ nuxtApp.payload.myStoreState = myStore.getState()
34
+ })
35
+ }
36
+
37
+ if (import.meta.client) {
38
+ nuxt.hooks.hook('app:created', () => {
39
+ myStore.setState(nuxtApp.payload.myStoreState)
40
+ })
41
+ }
42
+ })
43
+ ```
44
+
45
+ ```ts [With useHydration]
46
+ export default defineNuxtPlugin((nuxtApp) => {
47
+ const myStore = new MyStore()
48
+
49
+ useHydration(
50
+ 'myStoreState',
51
+ () => myStore.getState(),
52
+ (data) => myStore.setState(data)
53
+ )
54
+ })
55
+ ```
56
+ ::
57
+
58
+ ## Type
59
+
60
+ ```ts [signature]
61
+ useHydration <T> (key: string, get: () => T, set: (value: T) => void) => void
62
+ ```
63
+
64
+ ## Parameters
65
+
66
+ - `key`: A unique key that identifies the data in your Nuxt application.
67
+ - `get`: A function executed **only on the server** (called when SSR rendering is done) to set the initial value.
68
+ - `set`: A function executed **only on the client** (called when initial vue instance is created) to receive the data.
@@ -0,0 +1,47 @@
1
+ ---
2
+ title: useLazyAsyncData
3
+ description: This wrapper around useAsyncData triggers navigation immediately.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts
8
+ size: xs
9
+ ---
10
+
11
+ ## Description
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`.
14
+
15
+ ::note
16
+ `useLazyAsyncData` has the same signature as [`useAsyncData`](/docs/api/composables/use-async-data).
17
+ ::
18
+
19
+ :read-more{to="/docs/api/composables/use-async-data"}
20
+
21
+ ## Example
22
+
23
+ ```vue [pages/index.vue]
24
+ <script setup lang="ts">
25
+ /* Navigation will occur before fetching is complete.
26
+ Handle 'pending' and 'error' states directly within your component's template
27
+ */
28
+ const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
29
+
30
+ watch(count, (newCount) => {
31
+ // Because count might start out null, you won't have access
32
+ // to its contents immediately, but you can watch it.
33
+ })
34
+ </script>
35
+
36
+ <template>
37
+ <div>
38
+ {{ status === 'pending' ? 'Loading' : count }}
39
+ </div>
40
+ </template>
41
+ ```
42
+
43
+ ::warning
44
+ `useLazyAsyncData` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyAsyncData`.
45
+ ::
46
+
47
+ :read-more{to="/docs/getting-started/data-fetching"}
@@ -0,0 +1,55 @@
1
+ ---
2
+ title: 'useLazyFetch'
3
+ description: This wrapper around useFetch triggers navigation immediately.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/fetch.ts
8
+ size: xs
9
+ ---
10
+
11
+ ## Description
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`.
14
+
15
+ ::note
16
+ `useLazyFetch` has the same signature as [`useFetch`](/docs/api/composables/use-fetch).
17
+ ::
18
+
19
+ ::note
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
+ ::
22
+
23
+ :read-more{to="/docs/api/composables/use-fetch"}
24
+
25
+ ## Example
26
+
27
+ ```vue [pages/index.vue]
28
+ <script setup lang="ts">
29
+ /* Navigation will occur before fetching is complete.
30
+ * Handle 'pending' and 'error' states directly within your component's template
31
+ */
32
+ const { status, data: posts } = await useLazyFetch('/api/posts')
33
+ watch(posts, (newPosts) => {
34
+ // Because posts might start out null, you won't have access
35
+ // to its contents immediately, but you can watch it.
36
+ })
37
+ </script>
38
+
39
+ <template>
40
+ <div v-if="status === 'pending'">
41
+ Loading ...
42
+ </div>
43
+ <div v-else>
44
+ <div v-for="post in posts">
45
+ <!-- do something -->
46
+ </div>
47
+ </div>
48
+ </template>
49
+ ```
50
+
51
+ ::note
52
+ `useLazyFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useLazyFetch`.
53
+ ::
54
+
55
+ :read-more{to="/docs/getting-started/data-fetching"}
@@ -0,0 +1,77 @@
1
+ ---
2
+ title: 'useLoadingIndicator'
3
+ description: This composable gives you access to the loading state of the app page.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/loading-indicator.ts
8
+ size: xs
9
+ ---
10
+
11
+ ## Description
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.
15
+
16
+ ## Parameters
17
+
18
+ - `duration`: Duration of the loading bar, in milliseconds (default `2000`).
19
+ - `throttle`: Throttle the appearing and hiding, in milliseconds (default `200`).
20
+ - `estimatedProgress`: By default Nuxt will back off as it approaches 100%. You can provide a custom function to customize the progress estimation, which is a function that receives the duration of the loading bar (above) and the elapsed time. It should return a value between 0 and 100.
21
+
22
+ ## Properties
23
+
24
+ ### `isLoading`
25
+
26
+ - **type**: `Ref<boolean>`
27
+ - **description**: The loading state
28
+
29
+ ### `error`
30
+
31
+ - **type**: `Ref<boolean>`
32
+ - **description**: The error state
33
+
34
+ ### `progress`
35
+
36
+ - **type**: `Ref<number>`
37
+ - **description**: The progress state. From `0` to `100`.
38
+
39
+ ## Methods
40
+
41
+ ### `start()`
42
+
43
+ Set `isLoading` to true and start to increase the `progress` value. `start` accepts a `{ force: true }` option to skip the interval and show the loading state immediately.
44
+
45
+ ### `set()`
46
+
47
+ Set the `progress` value to a specific value. `set` accepts a `{ force: true }` option to skip the interval and show the loading state immediately.
48
+
49
+ ### `finish()`
50
+
51
+ Set the `progress` value to `100`, stop all timers and intervals then reset the loading state `500` ms later. `finish` accepts a `{ force: true }` option to skip the interval before the state is reset, and `{ error: true }` to change the loading bar color and set the error property to true.
52
+
53
+ ### `clear()`
54
+
55
+ Used by `finish()`. Clear all timers and intervals used by the composable.
56
+
57
+ ## Example
58
+
59
+ ```vue
60
+ <script setup lang="ts">
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
+ </script>
68
+ ```
69
+
70
+ ```vue
71
+ <script setup lang="ts">
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
+ </script>
77
+ ```