@nuxt/docs 0.0.0 → 3.17.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.
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,98 @@
1
+ ---
2
+ title: "$fetch"
3
+ description: Nuxt uses ofetch to expose globally the $fetch helper for making HTTP requests.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/entry.ts
8
+ size: xs
9
+ ---
10
+
11
+ Nuxt uses [ofetch](https://github.com/unjs/ofetch) to expose globally the `$fetch` helper for making HTTP requests within your Vue app or API routes.
12
+
13
+ ::tip{icon="i-lucide-rocket"}
14
+ During server-side rendering, calling `$fetch` to fetch your internal [API routes](/docs/guide/directory-structure/server) will directly call the relevant function (emulating the request), **saving an additional API call**.
15
+ ::
16
+
17
+ ::note{color="blue" icon="i-lucide-info"}
18
+ Using `$fetch` in components without wrapping it with [`useAsyncData`](/docs/api/composables/use-async-data) causes fetching the data twice: initially on the server, then again on the client-side during hydration, because `$fetch` does not transfer state from the server to the client. Thus, the fetch will be executed on both sides because the client has to get the data again.
19
+ ::
20
+
21
+ ## Usage
22
+
23
+ We recommend to use [`useFetch`](/docs/api/composables/use-fetch) or [`useAsyncData`](/docs/api/composables/use-async-data) + `$fetch` to prevent double data fetching when fetching the component data.
24
+
25
+ ```vue [app.vue]
26
+ <script setup lang="ts">
27
+ // During SSR data is fetched twice, once on the server and once on the client.
28
+ const dataTwice = await $fetch('/api/item')
29
+
30
+ // During SSR data is fetched only on the server side and transferred to the client.
31
+ const { data } = await useAsyncData('item', () => $fetch('/api/item'))
32
+
33
+ // You can also useFetch as shortcut of useAsyncData + $fetch
34
+ const { data } = await useFetch('/api/item')
35
+ </script>
36
+ ```
37
+
38
+ :read-more{to="/docs/getting-started/data-fetching"}
39
+
40
+ You can use `$fetch` in any methods that are executed only on client-side.
41
+
42
+ ```vue [pages/contact.vue]
43
+ <script setup lang="ts">
44
+ async function contactForm() {
45
+ await $fetch('/api/contact', {
46
+ method: 'POST',
47
+ body: { hello: 'world '}
48
+ })
49
+ }
50
+ </script>
51
+
52
+ <template>
53
+ <button @click="contactForm">Contact</button>
54
+ </template>
55
+ ```
56
+
57
+ ::tip
58
+ `$fetch` is the preferred way to make HTTP calls in Nuxt instead of [@nuxt/http](https://github.com/nuxt/http) and [@nuxtjs/axios](https://github.com/nuxt-community/axios-module) that are made for Nuxt 2.
59
+ ::
60
+
61
+ ::note
62
+ If you use `$fetch` 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.
63
+ ::
64
+
65
+ ### Passing Headers and Cookies
66
+
67
+ When we call `$fetch` in the browser, user headers like `cookie` will be directly sent to the API.
68
+
69
+ However, during Server-Side Rendering, due to security risks such as **Server-Side Request Forgery (SSRF)** or **Authentication Misuse**, the `$fetch` wouldn't include the user's browser cookies, nor pass on cookies from the fetch response.
70
+
71
+ ::code-group
72
+
73
+ ```vue [pages/index.vue]
74
+ <script setup lang="ts">
75
+ // This will NOT forward headers or cookies during SSR
76
+ const { data } = await useAsyncData(() => $fetch('/api/cookies'))
77
+ </script>
78
+ ```
79
+
80
+ ```ts [server/api/cookies.ts]
81
+ export default defineEventHandler((event) => {
82
+ const foo = getCookie(event, 'foo')
83
+ // ... Do something with the cookie
84
+ })
85
+ ```
86
+ ::
87
+
88
+ If you need to forward headers and cookies on the server, you must manually pass them:
89
+
90
+ ```vue [pages/index.vue]
91
+ <script setup lang="ts">
92
+ // This will forward the user's headers and cookies to `/api/cookies`
93
+ const requestFetch = useRequestFetch()
94
+ const { data } = await useAsyncData(() => requestFetch('/api/cookies'))
95
+ </script>
96
+ ```
97
+
98
+ However, when calling `useFetch` with a relative URL on the server, Nuxt will use [`useRequestFetch`](/docs/api/composables/use-request-fetch) to proxy headers and cookies (with the exception of headers not meant to be forwarded, like `host`).
@@ -0,0 +1,3 @@
1
+ title: 'Utils'
2
+ titleTemplate: '%s · Nuxt Utils'
3
+ navigation.icon: i-lucide-square-function
@@ -0,0 +1,73 @@
1
+ ---
2
+ title: 'abortNavigation'
3
+ description: 'abortNavigation is a helper function that prevents navigation from taking place and throws an error if one is set as a parameter.'
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/router.ts
8
+ size: xs
9
+ ---
10
+
11
+ ::warning
12
+ `abortNavigation` is only usable inside a [route middleware handler](/docs/guide/directory-structure/middleware).
13
+ ::
14
+
15
+ ## Type
16
+
17
+ ```ts
18
+ abortNavigation(err?: Error | string): false
19
+ ```
20
+
21
+ ## Parameters
22
+
23
+ ### `err`
24
+
25
+ - **Type**: [`Error`](https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Error) | `string`
26
+
27
+ Optional error to be thrown by `abortNavigation`.
28
+
29
+ ## Examples
30
+
31
+ The example below shows how you can use `abortNavigation` in a route middleware to prevent unauthorized route access:
32
+
33
+ ```ts [middleware/auth.ts]
34
+ export default defineNuxtRouteMiddleware((to, from) => {
35
+ const user = useState('user')
36
+
37
+ if (!user.value.isAuthorized) {
38
+ return abortNavigation()
39
+ }
40
+
41
+ if (to.path !== '/edit-post') {
42
+ return navigateTo('/edit-post')
43
+ }
44
+ })
45
+ ```
46
+
47
+ ### `err` as a String
48
+
49
+ You can pass the error as a string:
50
+
51
+ ```ts [middleware/auth.ts]
52
+ export default defineNuxtRouteMiddleware((to, from) => {
53
+ const user = useState('user')
54
+
55
+ if (!user.value.isAuthorized) {
56
+ return abortNavigation('Insufficient permissions.')
57
+ }
58
+ })
59
+ ```
60
+
61
+ ### `err` as an Error Object
62
+
63
+ You can pass the error as an [`Error`](https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Error) object, e.g. caught by the `catch`-block:
64
+
65
+ ```ts [middleware/auth.ts]
66
+ export default defineNuxtRouteMiddleware((to, from) => {
67
+ try {
68
+ /* code that might throw an error */
69
+ } catch (err) {
70
+ return abortNavigation(err)
71
+ }
72
+ })
73
+ ```
@@ -0,0 +1,88 @@
1
+ ---
2
+ title: 'addRouteMiddleware'
3
+ description: 'addRouteMiddleware() is a helper function to dynamically add middleware in your application.'
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/router.ts
8
+ size: xs
9
+ ---
10
+
11
+ ::note
12
+ Route middleware are navigation guards stored in the [`middleware/`](/docs/guide/directory-structure/middleware) directory of your Nuxt application (unless [set otherwise](/docs/api/nuxt-config#middleware)).
13
+ ::
14
+
15
+ ## Type
16
+
17
+ ```ts
18
+ function addRouteMiddleware (name: string, middleware: RouteMiddleware, options?: AddRouteMiddlewareOptions): void
19
+ function addRouteMiddleware (middleware: RouteMiddleware): void
20
+
21
+ interface AddRouteMiddlewareOptions {
22
+ global?: boolean
23
+ }
24
+ ```
25
+
26
+ ## Parameters
27
+
28
+ ### `name`
29
+
30
+ - **Type:** `string` | `RouteMiddleware`
31
+
32
+ Can be either a string or a function of type `RouteMiddleware`. Function takes the next route `to` as the first argument and the current route `from` as the second argument, both of which are Vue route objects.
33
+
34
+ Learn more about available properties of [route objects](/docs/api/composables/use-route).
35
+
36
+ ### `middleware`
37
+
38
+ - **Type:** `RouteMiddleware`
39
+
40
+ The second argument is a function of type `RouteMiddleware`. Same as above, it provides `to` and `from` route objects. It becomes optional if the first argument in `addRouteMiddleware()` is already passed as a function.
41
+
42
+ ### `options`
43
+
44
+ - **Type:** `AddRouteMiddlewareOptions`
45
+
46
+ An optional `options` argument lets you set the value of `global` to `true` to indicate whether the router middleware is global or not (set to `false` by default).
47
+
48
+ ## Examples
49
+
50
+ ### Named Route Middleware
51
+
52
+ Named route middleware is defined by providing a string as the first argument and a function as the second:
53
+
54
+ ```ts [plugins/my-plugin.ts]
55
+ export default defineNuxtPlugin(() => {
56
+ addRouteMiddleware('named-middleware', () => {
57
+ console.log('named middleware added in Nuxt plugin')
58
+ })
59
+ })
60
+ ```
61
+
62
+ When defined in a plugin, it overrides any existing middleware of the same name located in the `middleware/` directory.
63
+
64
+ ### Global Route Middleware
65
+
66
+ Global route middleware can be defined in two ways:
67
+
68
+ - Pass a function directly as the first argument without a name. It will automatically be treated as global middleware and applied on every route change.
69
+
70
+ ```ts [plugins/my-plugin.ts]
71
+ export default defineNuxtPlugin(() => {
72
+ addRouteMiddleware((to, from) => {
73
+ console.log('anonymous global middleware that runs on every route change')
74
+ })
75
+ })
76
+ ```
77
+
78
+ - Set an optional, third argument `{ global: true }` to indicate whether the route middleware is global.
79
+
80
+ ```ts [plugins/my-plugin.ts]
81
+ export default defineNuxtPlugin(() => {
82
+ addRouteMiddleware('global-middleware', (to, from) => {
83
+ console.log('global middleware that runs on every route change')
84
+ },
85
+ { global: true }
86
+ )
87
+ })
88
+ ```
@@ -0,0 +1,92 @@
1
+ ---
2
+ title: "callOnce"
3
+ description: "Run a given function or block of code once during SSR or CSR."
4
+ navigation:
5
+ badge: New
6
+ links:
7
+ - label: Source
8
+ icon: i-simple-icons-github
9
+ to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/once.ts
10
+ size: xs
11
+ ---
12
+
13
+ ::important
14
+ This utility is available since [Nuxt v3.9](/blog/v3-9).
15
+ ::
16
+
17
+ ## Purpose
18
+
19
+ The `callOnce` function is designed to execute a given function or block of code only once during:
20
+ - server-side rendering but not hydration
21
+ - client-side navigation
22
+
23
+ This is useful for code that should be executed only once, such as logging an event or setting up a global state.
24
+
25
+ ## Usage
26
+
27
+ The default mode of `callOnce` is to run code only once. For example, if the code runs on the server it won't run again on the client. It also won't run again if you `callOnce` more than once on the client, for example by navigating back to this page.
28
+
29
+ ```vue [app.vue]
30
+ <script setup lang="ts">
31
+ const websiteConfig = useState('config')
32
+
33
+ await callOnce(async () => {
34
+ console.log('This will only be logged once')
35
+ websiteConfig.value = await $fetch('https://my-cms.com/api/website-config')
36
+ })
37
+ </script>
38
+ ```
39
+
40
+ It is also possible to run on every navigation while still avoiding the initial server/client double load. For this, it is possible to use the `navigation` mode:
41
+
42
+ ```vue [app.vue]
43
+ <script setup lang="ts">
44
+ const websiteConfig = useState('config')
45
+
46
+ await callOnce(async () => {
47
+ console.log('This will only be logged once and then on every client side navigation')
48
+ websiteConfig.value = await $fetch('https://my-cms.com/api/website-config')
49
+ }, { mode: 'navigation' })
50
+ </script>
51
+ ```
52
+
53
+ ::important
54
+ `navigation` mode is available since [Nuxt v3.15](/blog/v3-15).
55
+ ::
56
+
57
+ ::tip{to="/docs/getting-started/state-management#usage-with-pinia"}
58
+ `callOnce` is useful in combination with the [Pinia module](/modules/pinia) to call store actions.
59
+ ::
60
+
61
+ :read-more{to="/docs/getting-started/state-management"}
62
+
63
+ ::warning
64
+ Note that `callOnce` doesn't return anything. You should use [`useAsyncData`](/docs/api/composables/use-async-data) or [`useFetch`](/docs/api/composables/use-fetch) if you want to do data fetching during SSR.
65
+ ::
66
+
67
+ ::note
68
+ `callOnce` is a composable meant to be called directly in a setup function, plugin, or route middleware, because it needs to add data to the Nuxt payload to avoid re-calling the function on the client when the page hydrates.
69
+ ::
70
+
71
+ ## Type
72
+
73
+ ```ts
74
+ callOnce (key?: string, fn?: (() => any | Promise<any>), options?: CallOnceOptions): Promise<void>
75
+ callOnce(fn?: (() => any | Promise<any>), options?: CallOnceOptions): Promise<void>
76
+
77
+ type CallOnceOptions = {
78
+ /**
79
+ * Execution mode for the callOnce function
80
+ * @default 'render'
81
+ */
82
+ mode?: 'navigation' | 'render'
83
+ }
84
+ ```
85
+
86
+ ## Parameters
87
+
88
+ - `key`: A unique key ensuring that the code is run once. If you do not provide a key, then a key that is unique to the file and line number of the instance of `callOnce` will be generated for you.
89
+ - `fn`: The function to run once. It can be asynchronous.
90
+ - `options`: Setup the mode, either to re-execute on navigation (`navigation`) or just once for the lifetime of the app (`render`). Defaults to `render`.
91
+ - `render`: Executes once during initial render (either SSR or CSR) - Default mode
92
+ - `navigation`: Executes once during initial render and once per subsequent client-side navigation
@@ -0,0 +1,29 @@
1
+ ---
2
+ title: "clearError"
3
+ description: "The clearError composable clears all handled errors."
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/error.ts
8
+ size: xs
9
+ ---
10
+
11
+ Within your pages, components, and plugins, you can use `clearError` to clear all errors and redirect the user.
12
+
13
+ **Parameters:**
14
+
15
+ - `options?: { redirect?: string }`
16
+
17
+ You can provide an optional path to redirect to (for example, if you want to navigate to a 'safe' page).
18
+
19
+ ```js
20
+ // Without redirect
21
+ clearError()
22
+
23
+ // With redirect
24
+ clearError({ redirect: '/homepage' })
25
+ ```
26
+
27
+ Errors are set in state using [`useError()`](/docs/api/composables/use-error). The `clearError` composable will reset this state and calls the `app:error:cleared` hook with the provided options.
28
+
29
+ :read-more{to="/docs/getting-started/error-handling"}
@@ -0,0 +1,23 @@
1
+ ---
2
+ title: 'clearNuxtData'
3
+ description: Delete cached data, error status and pending promises of useAsyncData and useFetch.
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
+ ::note
12
+ This method is useful if you want to invalidate the data fetching for another page.
13
+ ::
14
+
15
+ ## Type
16
+
17
+ ```ts
18
+ clearNuxtData (keys?: string | string[] | ((key: string) => boolean)): void
19
+ ```
20
+
21
+ ## Parameters
22
+
23
+ * `keys`: One or an array of keys that are used in [`useAsyncData`](/docs/api/composables/use-async-data) to delete their cached data. If no keys are provided, **all data** will be invalidated.
@@ -0,0 +1,23 @@
1
+ ---
2
+ title: 'clearNuxtState'
3
+ description: Delete the cached state of useState.
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/state.ts
8
+ size: xs
9
+ ---
10
+
11
+ ::note
12
+ This method is useful if you want to invalidate the state of `useState`.
13
+ ::
14
+
15
+ ## Type
16
+
17
+ ```ts
18
+ clearNuxtState (keys?: string | string[] | ((key: string) => boolean)): void
19
+ ```
20
+
21
+ ## Parameters
22
+
23
+ - `keys`: One or an array of keys that are used in [`useState`](/docs/api/composables/use-state) to delete their cached state. If no keys are provided, **all state** will be invalidated.
@@ -0,0 +1,55 @@
1
+ ---
2
+ title: 'createError'
3
+ description: Create an error object with additional metadata.
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/error.ts
8
+ size: xs
9
+ ---
10
+
11
+ You can use this function to create an error object with additional metadata. It is usable in both the Vue and Nitro portions of your app, and is meant to be thrown.
12
+
13
+ ## Parameters
14
+
15
+ - `err`: `string | { cause, data, message, name, stack, statusCode, statusMessage, fatal }`
16
+
17
+ You can pass either a string or an object to the `createError` function. If you pass a string, it will be used as the error `message`, and the `statusCode` will default to `500`. If you pass an object, you can set multiple properties of the error, such as `statusCode`, `message`, and other error properties.
18
+
19
+ ## In Vue App
20
+
21
+ If you throw an error created with `createError`:
22
+
23
+ - on server-side, it will trigger a full-screen error page which you can clear with `clearError`.
24
+ - on client-side, it will throw a non-fatal error for you to handle. If you need to trigger a full-screen error page, then you can do this by setting `fatal: true`.
25
+
26
+ ### Example
27
+
28
+ ```vue [pages/movies/[slug\\].vue]
29
+ <script setup lang="ts">
30
+ const route = useRoute()
31
+ const { data } = await useFetch(`/api/movies/${route.params.slug}`)
32
+ if (!data.value) {
33
+ throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
34
+ }
35
+ </script>
36
+ ```
37
+
38
+ ## In API Routes
39
+
40
+ Use `createError` to trigger error handling in server API routes.
41
+
42
+ ### Example
43
+
44
+ ```ts [server/api/error.ts]
45
+ export default eventHandler(() => {
46
+ throw createError({
47
+ statusCode: 404,
48
+ statusMessage: 'Page Not Found'
49
+ })
50
+ })
51
+ ```
52
+
53
+ In API routes, using `createError` by passing an object with a short `statusMessage` 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.
54
+
55
+ :read-more{to="/docs/getting-started/error-handling"}
@@ -0,0 +1,53 @@
1
+ ---
2
+ title: "defineNuxtComponent"
3
+ description: defineNuxtComponent() is a helper function for defining type safe components with Options API.
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/component.ts
8
+ size: xs
9
+ ---
10
+
11
+ ::note
12
+ `defineNuxtComponent()` is a helper function for defining type safe Vue components using options API similar to [`defineComponent()`](https://vuejs.org/api/general.html#definecomponent). `defineNuxtComponent()` wrapper also adds support for `asyncData` and `head` component options.
13
+ ::
14
+
15
+ ::note
16
+ Using `<script setup lang="ts">` is the recommended way of declaring Vue components in Nuxt.
17
+ ::
18
+
19
+ :read-more{to=/docs/getting-started/data-fetching}
20
+
21
+ ## `asyncData()`
22
+
23
+ If you choose not to use `setup()` in your app, you can use the `asyncData()` method within your component definition:
24
+
25
+ ```vue [pages/index.vue]
26
+ <script lang="ts">
27
+ export default defineNuxtComponent({
28
+ async asyncData() {
29
+ return {
30
+ data: {
31
+ greetings: 'hello world!'
32
+ }
33
+ }
34
+ },
35
+ })
36
+ </script>
37
+ ```
38
+
39
+ ## `head()`
40
+
41
+ If you choose not to use `setup()` in your app, you can use the `head()` method within your component definition:
42
+
43
+ ```vue [pages/index.vue]
44
+ <script lang="ts">
45
+ export default defineNuxtComponent({
46
+ head(nuxtApp) {
47
+ return {
48
+ title: 'My site'
49
+ }
50
+ },
51
+ })
52
+ </script>
53
+ ```
@@ -0,0 +1,67 @@
1
+ ---
2
+ title: "defineNuxtRouteMiddleware"
3
+ description: "Create named route middleware using defineNuxtRouteMiddleware helper function."
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/router.ts
8
+ size: xs
9
+ ---
10
+
11
+ Route middleware are stored in the [`middleware/`](/docs/guide/directory-structure/middleware) of your Nuxt application (unless [set otherwise](/docs/api/nuxt-config#middleware)).
12
+
13
+ ## Type
14
+
15
+ ```ts
16
+ defineNuxtRouteMiddleware(middleware: RouteMiddleware) => RouteMiddleware
17
+
18
+ interface RouteMiddleware {
19
+ (to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard>
20
+ }
21
+ ```
22
+
23
+ ## Parameters
24
+
25
+ ### `middleware`
26
+
27
+ - **Type**: `RouteMiddleware`
28
+
29
+ A function that takes two Vue Router's route location objects as parameters: the next route `to` as the first, and the current route `from` as the second.
30
+
31
+ Learn more about available properties of `RouteLocationNormalized` in the **[Vue Router docs](https://router.vuejs.org/api/#RouteLocationNormalized)**.
32
+
33
+ ## Examples
34
+
35
+ ### Showing Error Page
36
+
37
+ You can use route middleware to throw errors and show helpful error messages:
38
+
39
+ ```ts [middleware/error.ts]
40
+ export default defineNuxtRouteMiddleware((to) => {
41
+ if (to.params.id === '1') {
42
+ throw createError({ statusCode: 404, statusMessage: 'Page Not Found' })
43
+ }
44
+ })
45
+ ```
46
+
47
+ The above route middleware will redirect a user to the custom error page defined in the `~/error.vue` file, and expose the error message and code passed from the middleware.
48
+
49
+ ### Redirection
50
+
51
+ Use [`useState`](/docs/api/composables/use-state) in combination with `navigateTo` helper function inside the route middleware to redirect users to different routes based on their authentication status:
52
+
53
+ ```ts [middleware/auth.ts]
54
+ export default defineNuxtRouteMiddleware((to, from) => {
55
+ const auth = useState('auth')
56
+
57
+ if (!auth.value.isAuthenticated) {
58
+ return navigateTo('/login')
59
+ }
60
+
61
+ if (to.path !== '/dashboard') {
62
+ return navigateTo('/dashboard')
63
+ }
64
+ })
65
+ ```
66
+
67
+ Both [navigateTo](/docs/api/utils/navigate-to) and [abortNavigation](/docs/api/utils/abort-navigation) are globally available helper functions that you can use inside `defineNuxtRouteMiddleware`.