@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.
Files changed (201) hide show
  1. package/1.getting-started/01.introduction.md +6 -6
  2. package/1.getting-started/02.installation.md +3 -2
  3. package/1.getting-started/03.configuration.md +36 -36
  4. package/1.getting-started/04.views.md +9 -9
  5. package/1.getting-started/05.assets.md +15 -9
  6. package/1.getting-started/06.styling.md +58 -48
  7. package/1.getting-started/07.routing.md +17 -17
  8. package/1.getting-started/08.seo-meta.md +59 -46
  9. package/1.getting-started/09.transitions.md +49 -44
  10. package/1.getting-started/10.data-fetching.md +104 -81
  11. package/1.getting-started/11.state-management.md +26 -19
  12. package/1.getting-started/12.error-handling.md +22 -20
  13. package/1.getting-started/13.server.md +8 -8
  14. package/1.getting-started/14.layers.md +22 -16
  15. package/1.getting-started/15.prerendering.md +32 -32
  16. package/1.getting-started/16.deployment.md +10 -10
  17. package/1.getting-started/17.testing.md +44 -44
  18. package/1.getting-started/18.upgrade.md +241 -110
  19. package/2.guide/0.index.md +7 -7
  20. package/2.guide/{2.directory-structure → 1.directory-structure}/.navigation.yml +1 -1
  21. package/2.guide/{2.directory-structure → 1.directory-structure}/0.nuxt.md +3 -3
  22. package/2.guide/1.directory-structure/0.output.md +18 -0
  23. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/.navigation.yml +1 -1
  24. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.assets.md +4 -4
  25. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.components.md +45 -28
  26. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.composables.md +13 -13
  27. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.layouts.md +19 -15
  28. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.middleware.md +31 -25
  29. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.pages.md +39 -37
  30. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.plugins.md +25 -25
  31. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.utils.md +7 -7
  32. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app-config.md +18 -18
  33. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app.md +7 -7
  34. package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.error.md +6 -6
  35. package/2.guide/{2.directory-structure → 1.directory-structure}/1.content.md +7 -4
  36. package/2.guide/{2.directory-structure → 1.directory-structure}/1.modules.md +8 -8
  37. package/2.guide/{2.directory-structure → 1.directory-structure}/1.node_modules.md +2 -2
  38. package/2.guide/{2.directory-structure → 1.directory-structure}/1.public.md +2 -2
  39. package/2.guide/{2.directory-structure → 1.directory-structure}/1.server.md +35 -35
  40. package/2.guide/{2.directory-structure → 1.directory-structure}/1.shared.md +7 -7
  41. package/2.guide/{2.directory-structure → 1.directory-structure}/2.env.md +8 -8
  42. package/2.guide/{2.directory-structure → 1.directory-structure}/2.gitignore.md +1 -1
  43. package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtignore.md +5 -4
  44. package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtrc.md +4 -4
  45. package/2.guide/{2.directory-structure → 1.directory-structure}/3.nuxt-config.md +3 -3
  46. package/2.guide/{2.directory-structure → 1.directory-structure}/3.package.md +1 -1
  47. package/2.guide/{2.directory-structure → 1.directory-structure}/3.tsconfig.md +3 -3
  48. package/2.guide/{1.concepts → 2.concepts}/1.auto-imports.md +22 -22
  49. package/2.guide/{1.concepts → 2.concepts}/10.nuxt-lifecycle.md +17 -17
  50. package/2.guide/{1.concepts → 2.concepts}/2.vuejs-development.md +13 -13
  51. package/2.guide/{1.concepts → 2.concepts}/3.rendering.md +24 -22
  52. package/2.guide/{1.concepts → 2.concepts}/4.server-engine.md +6 -6
  53. package/2.guide/{1.concepts → 2.concepts}/5.modules.md +5 -5
  54. package/2.guide/{1.concepts → 2.concepts}/7.esm.md +22 -16
  55. package/2.guide/{1.concepts → 2.concepts}/8.typescript.md +12 -12
  56. package/2.guide/3.going-further/1.events.md +3 -3
  57. package/2.guide/3.going-further/1.experimental-features.md +334 -85
  58. package/2.guide/3.going-further/1.features.md +58 -11
  59. package/2.guide/3.going-further/1.internals.md +25 -25
  60. package/2.guide/3.going-further/10.runtime-config.md +12 -12
  61. package/2.guide/3.going-further/11.nightly-release-channel.md +1 -1
  62. package/2.guide/3.going-further/2.hooks.md +14 -14
  63. package/2.guide/3.going-further/3.modules.md +126 -108
  64. package/2.guide/3.going-further/4.kit.md +7 -7
  65. package/2.guide/3.going-further/6.nuxt-app.md +8 -8
  66. package/2.guide/3.going-further/7.layers.md +76 -59
  67. package/2.guide/3.going-further/9.debugging.md +2 -2
  68. package/2.guide/4.recipes/1.custom-routing.md +30 -30
  69. package/2.guide/4.recipes/2.vite-plugin.md +45 -4
  70. package/2.guide/4.recipes/3.custom-usefetch.md +13 -13
  71. package/2.guide/4.recipes/4.sessions-and-authentication.md +35 -21
  72. package/2.guide/5.best-practices/hydration.md +4 -4
  73. package/2.guide/5.best-practices/performance.md +17 -17
  74. package/3.api/1.components/1.client-only.md +6 -3
  75. package/3.api/1.components/1.nuxt-client-fallback.md +10 -7
  76. package/3.api/1.components/10.nuxt-picture.md +1 -1
  77. package/3.api/1.components/11.teleports.md +5 -2
  78. package/3.api/1.components/12.nuxt-route-announcer.md +9 -9
  79. package/3.api/1.components/13.nuxt-time.md +49 -18
  80. package/3.api/1.components/2.nuxt-page.md +6 -6
  81. package/3.api/1.components/3.nuxt-layout.md +15 -10
  82. package/3.api/1.components/4.nuxt-link.md +42 -22
  83. package/3.api/1.components/5.nuxt-loading-indicator.md +3 -3
  84. package/3.api/1.components/6.nuxt-error-boundary.md +12 -10
  85. package/3.api/2.composables/on-prehydrate.md +2 -2
  86. package/3.api/2.composables/use-app-config.md +1 -1
  87. package/3.api/2.composables/use-async-data.md +26 -22
  88. package/3.api/2.composables/use-cookie.md +29 -21
  89. package/3.api/2.composables/use-error.md +2 -2
  90. package/3.api/2.composables/use-fetch.md +62 -32
  91. package/3.api/2.composables/use-head-safe.md +7 -7
  92. package/3.api/2.composables/use-head.md +5 -5
  93. package/3.api/2.composables/use-hydration.md +6 -6
  94. package/3.api/2.composables/use-lazy-async-data.md +4 -4
  95. package/3.api/2.composables/use-lazy-fetch.md +4 -4
  96. package/3.api/2.composables/use-loading-indicator.md +12 -12
  97. package/3.api/2.composables/use-nuxt-app.md +22 -22
  98. package/3.api/2.composables/use-nuxt-data.md +8 -8
  99. package/3.api/2.composables/use-preview-mode.md +15 -18
  100. package/3.api/2.composables/use-request-event.md +1 -1
  101. package/3.api/2.composables/use-request-fetch.md +3 -3
  102. package/3.api/2.composables/use-request-header.md +1 -1
  103. package/3.api/2.composables/use-request-headers.md +4 -4
  104. package/3.api/2.composables/use-request-url.md +1 -1
  105. package/3.api/2.composables/use-response-header.md +9 -10
  106. package/3.api/2.composables/use-route-announcer.md +4 -4
  107. package/3.api/2.composables/use-route.md +1 -1
  108. package/3.api/2.composables/use-router.md +10 -8
  109. package/3.api/2.composables/use-runtime-config.md +11 -11
  110. package/3.api/2.composables/use-runtime-hook.md +2 -2
  111. package/3.api/2.composables/use-seo-meta.md +4 -4
  112. package/3.api/2.composables/use-server-seo-meta.md +6 -6
  113. package/3.api/2.composables/use-state.md +5 -5
  114. package/3.api/3.utils/$fetch.md +10 -8
  115. package/3.api/3.utils/abort-navigation.md +3 -3
  116. package/3.api/3.utils/add-route-middleware.md +5 -5
  117. package/3.api/3.utils/call-once.md +6 -6
  118. package/3.api/3.utils/clear-error.md +3 -3
  119. package/3.api/3.utils/clear-nuxt-data.md +3 -3
  120. package/3.api/3.utils/clear-nuxt-state.md +3 -3
  121. package/3.api/3.utils/create-error.md +2 -2
  122. package/3.api/3.utils/define-lazy-hydration-component.md +13 -13
  123. package/3.api/3.utils/define-nuxt-component.md +5 -5
  124. package/3.api/3.utils/define-nuxt-plugin.md +13 -13
  125. package/3.api/3.utils/define-nuxt-route-middleware.md +5 -5
  126. package/3.api/3.utils/define-page-meta.md +23 -23
  127. package/3.api/3.utils/define-route-rules.md +7 -7
  128. package/3.api/3.utils/navigate-to.md +11 -11
  129. package/3.api/3.utils/prefetch-components.md +1 -1
  130. package/3.api/3.utils/preload-components.md +1 -1
  131. package/3.api/3.utils/preload-route-components.md +2 -2
  132. package/3.api/3.utils/prerender-routes.md +3 -3
  133. package/3.api/3.utils/refresh-cookie.md +4 -4
  134. package/3.api/3.utils/refresh-nuxt-data.md +12 -7
  135. package/3.api/3.utils/reload-nuxt-app.md +3 -3
  136. package/3.api/3.utils/set-page-layout.md +1 -1
  137. package/3.api/3.utils/set-response-status.md +3 -3
  138. package/3.api/3.utils/show-error.md +5 -5
  139. package/3.api/3.utils/update-app-config.md +4 -3
  140. package/3.api/4.commands/add.md +1 -1
  141. package/3.api/4.commands/analyze.md +2 -1
  142. package/3.api/4.commands/build.md +2 -1
  143. package/3.api/4.commands/dev.md +5 -4
  144. package/3.api/4.commands/generate.md +3 -2
  145. package/3.api/4.commands/init.md +3 -2
  146. package/3.api/4.commands/module.md +4 -4
  147. package/3.api/4.commands/prepare.md +7 -2
  148. package/3.api/4.commands/preview.md +5 -4
  149. package/3.api/4.commands/test.md +40 -0
  150. package/3.api/4.commands/typecheck.md +5 -3
  151. package/3.api/4.commands/upgrade.md +3 -3
  152. package/3.api/5.kit/1.modules.md +123 -37
  153. package/3.api/5.kit/10.runtime-config.md +1 -1
  154. package/3.api/5.kit/10.templates.md +8 -6
  155. package/3.api/5.kit/11.nitro.md +66 -62
  156. package/3.api/5.kit/12.resolving.md +2 -2
  157. package/3.api/5.kit/14.builder.md +61 -4
  158. package/3.api/5.kit/15.examples.md +5 -7
  159. package/3.api/5.kit/16.layers.md +26 -26
  160. package/3.api/5.kit/3.compatibility.md +14 -14
  161. package/3.api/5.kit/4.autoimports.md +13 -13
  162. package/3.api/5.kit/5.components.md +8 -7
  163. package/3.api/5.kit/6.context.md +3 -3
  164. package/3.api/5.kit/7.pages.md +7 -7
  165. package/3.api/5.kit/8.layout.md +2 -2
  166. package/3.api/5.kit/9.head.md +132 -0
  167. package/3.api/5.kit/9.plugins.md +5 -4
  168. package/3.api/6.advanced/1.hooks.md +5 -5
  169. package/3.api/6.advanced/2.import-meta.md +3 -3
  170. package/3.api/6.nuxt-config.md +365 -859
  171. package/3.api/index.md +7 -7
  172. package/5.community/2.getting-help.md +1 -1
  173. package/5.community/3.reporting-bugs.md +3 -3
  174. package/5.community/4.contribution.md +11 -11
  175. package/5.community/5.framework-contribution.md +4 -4
  176. package/5.community/6.roadmap.md +2 -2
  177. package/6.bridge/1.overview.md +13 -13
  178. package/6.bridge/10.configuration.md +2 -1
  179. package/6.bridge/2.typescript.md +3 -3
  180. package/6.bridge/3.bridge-composition-api.md +8 -8
  181. package/6.bridge/4.plugins-and-middleware.md +9 -9
  182. package/6.bridge/5.nuxt3-compatible-api.md +20 -17
  183. package/6.bridge/6.meta.md +20 -19
  184. package/6.bridge/7.runtime-config.md +1 -1
  185. package/6.bridge/8.nitro.md +3 -3
  186. package/6.bridge/9.vite.md +4 -4
  187. package/7.migration/1.overview.md +2 -2
  188. package/7.migration/10.bundling.md +1 -1
  189. package/7.migration/11.server.md +3 -3
  190. package/7.migration/2.configuration.md +23 -21
  191. package/7.migration/20.module-authors.md +7 -7
  192. package/7.migration/3.auto-imports.md +3 -3
  193. package/7.migration/4.meta.md +21 -18
  194. package/7.migration/5.plugins-and-middleware.md +8 -8
  195. package/7.migration/6.pages-and-layouts.md +28 -24
  196. package/7.migration/7.component-options.md +18 -18
  197. package/7.migration/8.runtime-config.md +7 -7
  198. package/package.json +1 -1
  199. package/2.guide/2.directory-structure/0.output.md +0 -18
  200. /package/2.guide/{1.concepts → 2.concepts}/.navigation.yml +0 -0
  201. /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
- Modules are the building blocks of Nuxt. Kit provides a set of utilities to help you create and use modules. You can use these utilities to create your own modules or to reuse existing modules. For example, you can use the `defineNuxtModule` function to define a module and the `installModule` function to install a module programmatically.
11
+ Modules are the building blocks of Nuxt. Kit provides a set of utilities to help you create and use modules. You can use these utilities to create your own modules or to reuse existing modules. For example, you can use the `defineNuxtModule` function to define a module and specify dependencies using the `moduleDependencies` option.
12
12
 
13
13
  ## `defineNuxtModule`
14
14
 
@@ -22,16 +22,16 @@ import { defineNuxtModule } from '@nuxt/kit'
22
22
  export default defineNuxtModule({
23
23
  meta: {
24
24
  name: 'my-module',
25
- configKey: 'myModule'
25
+ configKey: 'myModule',
26
26
  },
27
27
  defaults: {
28
- enabled: true
28
+ enabled: true,
29
29
  },
30
30
  setup (options) {
31
31
  if (options.enabled) {
32
32
  console.log('My Nuxt module is enabled!')
33
33
  }
34
- }
34
+ },
35
35
  })
36
36
  ```
37
37
 
@@ -62,6 +62,7 @@ export function defineNuxtModule<TOptions extends ModuleOptions> (): {
62
62
  | `defaults` | `T \| ((nuxt: Nuxt) => T)`{lang="ts"} | `false` | Default options for the module. If a function is provided, it will be called with the Nuxt instance as the first argument. |
63
63
  | `schema` | `T` | `false` | Schema for the module options. If provided, options will be applied to the schema. |
64
64
  | `hooks` | `Partial<NuxtHooks>`{lang="ts"} | `false` | Hooks to be installed for the module. If provided, the module will install the hooks. |
65
+ | `moduleDependencies` | `Record<string, ModuleDependency> \| ((nuxt: Nuxt) => Record<string, ModuleDependency>)`{lang="ts"} | `false` | Dependencies on other modules with version constraints and configuration. Can be an object or a function that receives the Nuxt instance. See [example](#specifying-module-dependencies). |
65
66
  | `onInstall` | `(nuxt: Nuxt) => Awaitable<void>`{lang="ts"} | `false` | Lifecycle hook called when the module is first installed. Requires `meta.name` and `meta.version` to be defined. |
66
67
  | `onUpgrade` | `(options: T, nuxt: Nuxt, previousVersion: string) => Awaitable<void>`{lang="ts"} | `false` | Lifecycle hook called when the module is upgraded to a newer version. Requires `meta.name` and `meta.version` to be defined. |
67
68
  | `setup` | `(this: void, resolvedOptions: T, nuxt: Nuxt) => Awaitable<void \| false \| ModuleSetupInstallResult>`{lang="ts"} | `false` | Setup function for the module. If provided, the module will call the setup function. |
@@ -78,17 +79,17 @@ import { defineNuxtModule } from '@nuxt/kit'
78
79
  export default defineNuxtModule({
79
80
  meta: {
80
81
  name: 'my-module',
81
- configKey: 'myModule'
82
+ configKey: 'myModule',
82
83
  },
83
84
  defaults: {
84
85
  // Module options
85
- enabled: true
86
+ enabled: true,
86
87
  },
87
88
  setup (options) {
88
89
  if (options.enabled) {
89
90
  console.log('My Nuxt module is enabled!')
90
91
  }
91
- }
92
+ },
92
93
  })
93
94
  ```
94
95
 
@@ -97,8 +98,8 @@ Users can provide options for this module under the corresponding key in `nuxt.c
97
98
  ```ts
98
99
  export default defineNuxtConfig({
99
100
  myModule: {
100
- enabled: false
101
- }
101
+ enabled: false,
102
+ },
102
103
  })
103
104
  ```
104
105
 
@@ -116,7 +117,7 @@ export default defineNuxtModule({
116
117
  nuxt: '>=3.0.0', // or use '^3.0.0'
117
118
  },
118
119
  },
119
- async setup() {
120
+ setup () {
120
121
  const resolver = createResolver(import.meta.url)
121
122
  // Implement
122
123
  },
@@ -148,26 +149,26 @@ interface ModuleOptions {
148
149
  export default defineNuxtModule<ModuleOptions>().with({
149
150
  meta: {
150
151
  name: '@nuxtjs/my-api',
151
- configKey: 'myApi'
152
+ configKey: 'myApi',
152
153
  },
153
154
  defaults: {
154
155
  baseURL: 'https://api.example.com',
155
156
  timeout: 5000,
156
- retries: 3
157
+ retries: 3,
157
158
  },
158
- setup(resolvedOptions, nuxt) {
159
+ setup (resolvedOptions, nuxt) {
159
160
  // resolvedOptions is properly typed as:
160
161
  // {
161
162
  // apiKey: string // Required, no default provided
162
163
  // baseURL: string // Required, has default value
163
164
  // timeout: number // Optional, has default value
164
- // retries: number // Optional, has default value
165
+ // retries: number // Optional, has default value
165
166
  // }
166
-
167
+
167
168
  console.log(resolvedOptions.baseURL) // ✅ TypeScript knows this is always defined
168
169
  console.log(resolvedOptions.timeout) // ✅ TypeScript knows this is always defined
169
170
  console.log(resolvedOptions.retries) // ✅ TypeScript knows this is always defined
170
- }
171
+ },
171
172
  })
172
173
  ```
173
174
 
@@ -197,50 +198,135 @@ export default defineNuxtModule({
197
198
  meta: {
198
199
  name: 'my-awesome-module',
199
200
  version: '1.2.0', // Required for lifecycle hooks
200
- configKey: 'myAwesomeModule'
201
+ configKey: 'myAwesomeModule',
201
202
  },
202
203
  defaults: {
203
204
  apiKey: '',
204
- enabled: true
205
+ enabled: true,
205
206
  },
206
-
207
- onInstall(nuxt) {
207
+
208
+ onInstall (nuxt) {
208
209
  // This runs only when the module is first installed
209
210
  console.log('Setting up my-awesome-module for the first time!')
210
-
211
+
211
212
  // You might want to:
212
213
  // - Create initial configuration files
213
214
  // - Set up database schemas
214
215
  // - Display welcome messages
215
216
  // - Perform initial data migration
216
217
  },
217
-
218
- onUpgrade(options, nuxt, previousVersion) {
218
+
219
+ onUpgrade (options, nuxt, previousVersion) {
219
220
  // This runs when the module is upgraded to a newer version
220
221
  console.log(`Upgrading my-awesome-module from ${previousVersion} to 1.2.0`)
221
-
222
+
222
223
  // You might want to:
223
224
  // - Migrate configuration files
224
- // - Update database schemas
225
+ // - Update database schemas
225
226
  // - Clean up deprecated files
226
227
  // - Display upgrade notes
227
-
228
+
228
229
  if (semver.lt(previousVersion, '1.1.0')) {
229
230
  console.log('⚠️ Breaking changes in 1.1.0 - please check the migration guide')
230
231
  }
231
232
  },
232
-
233
- setup(options, nuxt) {
233
+
234
+ setup (options, nuxt) {
234
235
  // Regular setup logic runs on every build
235
236
  if (options.enabled) {
236
237
  // Configure the module
237
238
  }
238
- }
239
+ },
240
+ })
241
+ ```
242
+
243
+ #### Specifying Module Dependencies
244
+
245
+ You can use the `moduleDependencies` option to declare dependencies on other modules. This provides a robust way to ensure proper setup order, version compatibility, and configuration management.
246
+
247
+ The `moduleDependencies` option can be either an object or a function that receives the Nuxt instance:
248
+
249
+ ##### Example
250
+
251
+ ```ts
252
+ import { defineNuxtModule } from '@nuxt/kit'
253
+
254
+ export default defineNuxtModule({
255
+ meta: {
256
+ name: 'my-module',
257
+ },
258
+ moduleDependencies: {
259
+ '@nuxtjs/tailwindcss': {
260
+ // Specify a version constraint (semver format)
261
+ version: '>=6.0.0',
262
+ // Configuration that overrides user settings
263
+ overrides: {
264
+ exposeConfig: true,
265
+ },
266
+ // Configuration that sets defaults but respects user settings
267
+ defaults: {
268
+ config: {
269
+ darkMode: 'class',
270
+ },
271
+ },
272
+ },
273
+ '@nuxtjs/fontaine': {
274
+ // Optional dependencies won't be installed but ensure that options
275
+ // can be set if they _are_ installed
276
+ optional: true,
277
+ defaults: {
278
+ fonts: [
279
+ {
280
+ family: 'Roboto',
281
+ fallbacks: ['Impact'],
282
+ },
283
+ ],
284
+ },
285
+ },
286
+ },
287
+ setup (options, nuxt) {
288
+
289
+ },
290
+ })
291
+ ```
292
+
293
+ You can also use a function to dynamically determine dependencies based on the Nuxt configuration:
294
+
295
+ ```ts
296
+ import { defineNuxtModule } from '@nuxt/kit'
297
+
298
+ export default defineNuxtModule({
299
+ meta: {
300
+ name: 'my-module',
301
+ },
302
+ moduleDependencies (nuxt) {
303
+ const dependencies: Record<string, any> = {
304
+ '@nuxtjs/tailwindcss': {
305
+ version: '>=6.0.0',
306
+ },
307
+ }
308
+
309
+ // Conditionally add dependencies based on Nuxt config
310
+ if (nuxt.options.experimental?.someFeature) {
311
+ dependencies['@nuxtjs/fontaine'] = {
312
+ optional: true,
313
+ }
314
+ }
315
+
316
+ return dependencies
317
+ },
318
+ setup (options, nuxt) {
319
+ // Your setup logic runs after all dependencies are initialized
320
+ },
239
321
  })
240
322
  ```
241
323
 
242
324
  ## `installModule`
243
325
 
326
+ ::callout{type="warning"}
327
+ **Deprecated:** Use the [`moduleDependencies`](#specifying-module-dependencies) option in `defineNuxtModule` instead. The `installModule` function will be removed (or may become non-blocking) in a future version.
328
+ ::
329
+
244
330
  Install specified Nuxt module programmatically. This is helpful when your module depends on other modules. You can pass the module options as an object to `inlineOptions` and they will be passed to the module's `setup` function.
245
331
 
246
332
  ### Usage
@@ -248,7 +334,7 @@ Install specified Nuxt module programmatically. This is helpful when your module
248
334
  ```ts twoslash
249
335
  import { defineNuxtModule, installModule } from '@nuxt/kit'
250
336
 
251
- export default defineNuxtModule({
337
+ export default defineNuxtModule({
252
338
  async setup () {
253
339
  // will install @nuxtjs/fontaine with Roboto font and Impact fallback
254
340
  await installModule('@nuxtjs/fontaine', {
@@ -258,10 +344,10 @@ export default defineNuxtModule({
258
344
  family: 'Roboto',
259
345
  fallbacks: ['Impact'],
260
346
  fallbackName: 'fallback-a',
261
- }
262
- ]
347
+ },
348
+ ],
263
349
  })
264
- }
350
+ },
265
351
  })
266
352
  ```
267
353
 
@@ -284,7 +370,7 @@ async function installModule (moduleToInstall: string | NuxtModule, inlineOption
284
370
  ```ts
285
371
  import { defineNuxtModule, installModule } from '@nuxt/kit'
286
372
 
287
- export default defineNuxtModule({
373
+ export default defineNuxtModule({
288
374
  async setup (options, nuxt) {
289
375
  // will install @nuxtjs/fontaine with Roboto font and Impact fallback
290
376
  await installModule('@nuxtjs/fontaine', {
@@ -294,9 +380,9 @@ export default defineNuxtModule({
294
380
  family: 'Roboto',
295
381
  fallbacks: ['Impact'],
296
382
  fallbackName: 'fallback-a',
297
- }
298
- ]
383
+ },
384
+ ],
299
385
  })
300
- }
386
+ },
301
387
  })
302
388
  ```
@@ -10,7 +10,7 @@ links:
10
10
 
11
11
  ## `useRuntimeConfig`
12
12
 
13
- At build-time, it is possible to access the resolved Nuxt [runtime config](/docs/guide/going-further/runtime-config).
13
+ At build-time, it is possible to access the resolved Nuxt [runtime config](/docs/4.x/guide/going-further/runtime-config).
14
14
 
15
15
  ### Type
16
16
 
@@ -21,17 +21,17 @@ import { addTemplate, defineNuxtModule } from '@nuxt/kit'
21
21
  import { defu } from 'defu'
22
22
 
23
23
  export default defineNuxtModule({
24
- setup(options, nuxt) {
24
+ setup (options, nuxt) {
25
25
  const globalMeta = defu(nuxt.options.app.head, {
26
26
  charset: options.charset,
27
- viewport: options.viewport
27
+ viewport: options.viewport,
28
28
  })
29
29
 
30
30
  addTemplate({
31
31
  filename: 'meta.config.mjs',
32
- getContents: () => 'export default ' + JSON.stringify({ globalMeta, mixinKey: 'setup' })
32
+ getContents: () => 'export default ' + JSON.stringify({ globalMeta, mixinKey: 'setup' }),
33
33
  })
34
- }
34
+ },
35
35
  })
36
36
  ```
37
37
 
@@ -88,7 +88,7 @@ In the module above, we generate a virtual file named `meta.config.mjs`. In the
88
88
  import { createHead as createServerHead } from '@unhead/vue/server'
89
89
  import { createHead as createClientHead } from '@unhead/vue/client'
90
90
  import { defineNuxtPlugin } from '#imports'
91
- // @ts-ignore
91
+ // @ts-expect-error - virtual file
92
92
  import metaConfig from '#build/meta.config.mjs'
93
93
 
94
94
  export default defineNuxtPlugin((nuxtApp) => {
@@ -279,7 +279,9 @@ export default defineNuxtModule({
279
279
  ]
280
280
  // watch and rebuild routes template list when one of the pages changes
281
281
  nuxt.hook('builder:watch', async (event, relativePath) => {
282
- if (event === 'change') { return }
282
+ if (event === 'change') {
283
+ return
284
+ }
283
285
 
284
286
  const path = resolve(nuxt.options.srcDir, relativePath)
285
287
  if (updateTemplatePaths.some(dir => path.startsWith(dir))) {
@@ -17,17 +17,17 @@ Adds a Nitro server handler. Use this if you want to create server middleware or
17
17
  ### Usage
18
18
 
19
19
  ```ts twoslash
20
- import { createResolver, defineNuxtModule, addServerHandler } from '@nuxt/kit'
20
+ import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
21
21
 
22
22
  export default defineNuxtModule({
23
- setup(options) {
23
+ setup (options) {
24
24
  const { resolve } = createResolver(import.meta.url)
25
25
 
26
26
  addServerHandler({
27
27
  route: '/robots.txt',
28
- handler: resolve('./runtime/robots.get')
28
+ handler: resolve('./runtime/robots.get'),
29
29
  })
30
- }
30
+ },
31
31
  })
32
32
  ```
33
33
 
@@ -58,24 +58,24 @@ You can use `addServerHandler` to add a server handler from your module.
58
58
  ::code-group
59
59
 
60
60
  ```ts twoslash [module.ts]
61
- import { createResolver, defineNuxtModule, addServerHandler } from '@nuxt/kit'
61
+ import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
62
62
 
63
63
  export default defineNuxtModule({
64
- setup(options) {
64
+ setup (options) {
65
65
  const { resolve } = createResolver(import.meta.url)
66
66
 
67
67
  addServerHandler({
68
68
  route: '/robots.txt',
69
- handler: resolve('./runtime/robots.get')
69
+ handler: resolve('./runtime/robots.get'),
70
70
  })
71
- }
71
+ },
72
72
  })
73
73
  ```
74
74
 
75
75
  ```ts twoslash [runtime/robots.get.ts]
76
76
  export default defineEventHandler(() => {
77
77
  return {
78
- body: `User-agent: *\nDisallow: /`
78
+ body: `User-agent: *\nDisallow: /`,
79
79
  }
80
80
  })
81
81
  ```
@@ -97,19 +97,19 @@ Adds a Nitro server handler to be used only in development mode. This handler wi
97
97
 
98
98
  ```ts twoslash
99
99
  import { defineEventHandler } from 'h3'
100
- import { createResolver, defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
100
+ import { addDevServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
101
101
 
102
102
  export default defineNuxtModule({
103
- setup() {
103
+ setup () {
104
104
  addDevServerHandler({
105
105
  handler: defineEventHandler(() => {
106
106
  return {
107
- body: `Response generated at ${new Date().toISOString()}`
107
+ body: `Response generated at ${new Date().toISOString()}`,
108
108
  }
109
109
  }),
110
- route: '/_handler'
110
+ route: '/_handler',
111
111
  })
112
- }
112
+ },
113
113
  })
114
114
  ```
115
115
 
@@ -139,18 +139,18 @@ In some cases, you may want to create a server handler specifically for developm
139
139
 
140
140
  ```ts
141
141
  import { joinURL } from 'ufo'
142
- import { defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
142
+ import { addDevServerHandler, defineNuxtModule } from '@nuxt/kit'
143
143
 
144
144
  export default defineNuxtModule({
145
- async setup(options, nuxt) {
145
+ async setup (options, nuxt) {
146
146
  const route = joinURL(nuxt.options.app?.baseURL, '/_tailwind')
147
147
 
148
- // @ts-ignore
148
+ // @ts-expect-error - tailwind-config-viewer does not have correct types
149
149
  const createServer = await import('tailwind-config-viewer/server/index.js').then(r => r.default || r) as any
150
150
  const viewerDevMiddleware = createServer({ tailwindConfigProvider: () => options, routerPrefix: route }).asMiddleware()
151
151
 
152
152
  addDevServerHandler({ route, handler: viewerDevMiddleware })
153
- }
153
+ },
154
154
  })
155
155
  ```
156
156
 
@@ -172,14 +172,14 @@ Changes to the Nitro instance configuration are not applied.
172
172
  import { defineNuxtModule, useNitro } from '@nuxt/kit'
173
173
 
174
174
  export default defineNuxtModule({
175
- setup(options, nuxt) {
175
+ setup (options, nuxt) {
176
176
  const resolver = createResolver(import.meta.url)
177
177
 
178
178
  nuxt.hook('ready', () => {
179
179
  const nitro = useNitro()
180
180
  // Do something with Nitro instance
181
181
  })
182
- }
182
+ },
183
183
  })
184
184
  ```
185
185
 
@@ -197,16 +197,20 @@ Add plugin to extend Nitro's runtime behavior.
197
197
  You can read more about Nitro plugins in the [Nitro documentation](https://nitro.build/guide/plugins).
198
198
  ::
199
199
 
200
+ ::warning
201
+ It is necessary to explicitly import `defineNitroPlugin` from `nitropack/runtime` within your plugin file. The same requirement applies to utilities such as `useRuntimeConfig`.
202
+ ::
203
+
200
204
  ### Usage
201
205
 
202
206
  ```ts twoslash
203
- import { createResolver, defineNuxtModule, addServerPlugin } from '@nuxt/kit'
207
+ import { addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
204
208
 
205
209
  export default defineNuxtModule({
206
- setup() {
210
+ setup () {
207
211
  const { resolve } = createResolver(import.meta.url)
208
212
  addServerPlugin(resolve('./runtime/plugin.ts'))
209
- }
213
+ },
210
214
  })
211
215
  ```
212
216
 
@@ -227,30 +231,30 @@ function addServerPlugin (plugin: string): void
227
231
  ::code-group
228
232
 
229
233
  ```ts [module.ts]
230
- import { createResolver, defineNuxtModule, addServerPlugin } from '@nuxt/kit'
234
+ import { addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
231
235
 
232
236
  export default defineNuxtModule({
233
- setup() {
237
+ setup () {
234
238
  const { resolve } = createResolver(import.meta.url)
235
239
  addServerPlugin(resolve('./runtime/plugin.ts'))
236
- }
240
+ },
237
241
  })
238
242
  ```
239
243
 
240
244
  ```ts [runtime/plugin.ts]
241
245
  export default defineNitroPlugin((nitroApp) => {
242
- nitroApp.hooks.hook("request", (event) => {
243
- console.log("on request", event.path);
244
- });
245
-
246
- nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
247
- console.log("on response", event.path, { body });
248
- });
249
-
250
- nitroApp.hooks.hook("afterResponse", (event, { body }) => {
251
- console.log("on after response", event.path, { body });
252
- });
253
- });
246
+ nitroApp.hooks.hook('request', (event) => {
247
+ console.log('on request', event.path)
248
+ })
249
+
250
+ nitroApp.hooks.hook('beforeResponse', (event, { body }) => {
251
+ console.log('on response', event.path, { body })
252
+ })
253
+
254
+ nitroApp.hooks.hook('afterResponse', (event, { body }) => {
255
+ console.log('on after response', event.path, { body })
256
+ })
257
+ })
254
258
  ```
255
259
 
256
260
  ::
@@ -262,7 +266,7 @@ Add routes to be prerendered to Nitro.
262
266
  ### Usage
263
267
 
264
268
  ```ts
265
- import { defineNuxtModule, addPrerenderRoutes } from '@nuxt/kit'
269
+ import { addPrerenderRoutes, defineNuxtModule } from '@nuxt/kit'
266
270
 
267
271
  export default defineNuxtModule({
268
272
  meta: {
@@ -273,11 +277,11 @@ export default defineNuxtModule({
273
277
  sitemapUrl: '/sitemap.xml',
274
278
  prerender: true,
275
279
  },
276
- setup(options) {
280
+ setup (options) {
277
281
  if (options.prerender) {
278
282
  addPrerenderRoutes(options.sitemapUrl)
279
283
  }
280
- }
284
+ },
281
285
  })
282
286
  ```
283
287
 
@@ -300,22 +304,22 @@ Add imports to the server. It makes your imports available in Nitro without the
300
304
  ### Usage
301
305
 
302
306
  ```ts twoslash
303
- import { defineNuxtModule, createResolver, addServerImports } from '@nuxt/kit'
307
+ import { addServerImports, createResolver, defineNuxtModule } from '@nuxt/kit'
304
308
 
305
309
  export default defineNuxtModule({
306
- setup(options) {
310
+ setup (options) {
307
311
  const names = [
308
312
  'useStoryblok',
309
313
  'useStoryblokApi',
310
314
  'useStoryblokBridge',
311
315
  'renderRichText',
312
- 'RichTextSchema'
316
+ 'RichTextSchema',
313
317
  ]
314
318
 
315
- names.forEach((name) =>
316
- addServerImports({ name, as: name, from: '@storyblok/vue' })
319
+ names.forEach(name =>
320
+ addServerImports({ name, as: name, from: '@storyblok/vue' }),
317
321
  )
318
- }
322
+ },
319
323
  })
320
324
  ```
321
325
 
@@ -347,17 +351,17 @@ Add a directory to be scanned for auto-imports by Nitro.
347
351
  ### Usage
348
352
 
349
353
  ```ts twoslash
350
- import { defineNuxtModule, createResolver, addServerImportsDir } from '@nuxt/kit'
354
+ import { addServerImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
351
355
 
352
356
  export default defineNuxtModule({
353
357
  meta: {
354
358
  name: 'my-module',
355
359
  configKey: 'myModule',
356
360
  },
357
- setup(options) {
361
+ setup (options) {
358
362
  const { resolve } = createResolver(import.meta.url)
359
363
  addServerImportsDir(resolve('./runtime/server/composables'))
360
- }
364
+ },
361
365
  })
362
366
  ```
363
367
 
@@ -381,22 +385,22 @@ You can use `addServerImportsDir` to add a directory to be scanned by Nitro. Thi
381
385
  ::code-group
382
386
 
383
387
  ```ts twoslash [module.ts]
384
- import { defineNuxtModule, createResolver, addServerImportsDir } from '@nuxt/kit'
388
+ import { addServerImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
385
389
 
386
390
  export default defineNuxtModule({
387
391
  meta: {
388
392
  name: 'my-module',
389
393
  configKey: 'myModule',
390
394
  },
391
- setup(options) {
395
+ setup (options) {
392
396
  const { resolve } = createResolver(import.meta.url)
393
397
  addServerImportsDir(resolve('./runtime/server/composables'))
394
- }
398
+ },
395
399
  })
396
400
  ```
397
401
 
398
402
  ```ts twoslash [runtime/server/composables/index.ts]
399
- export function useApiSecret() {
403
+ export function useApiSecret () {
400
404
  const { apiSecret } = useRuntimeConfig()
401
405
  return apiSecret
402
406
  }
@@ -427,17 +431,17 @@ Only `~/server/api`, `~/server/routes`, `~/server/middleware`, and `~/server/uti
427
431
  ### Usage
428
432
 
429
433
  ```ts twoslash
430
- import { defineNuxtModule, createResolver, addServerScanDir } from '@nuxt/kit'
434
+ import { addServerScanDir, createResolver, defineNuxtModule } from '@nuxt/kit'
431
435
 
432
436
  export default defineNuxtModule({
433
437
  meta: {
434
438
  name: 'my-module',
435
439
  configKey: 'myModule',
436
440
  },
437
- setup(options) {
441
+ setup (options) {
438
442
  const { resolve } = createResolver(import.meta.url)
439
443
  addServerScanDir(resolve('./runtime/server'))
440
- }
444
+ },
441
445
  })
442
446
  ```
443
447
 
@@ -461,22 +465,22 @@ You can use `addServerScanDir` to add a directory to be scanned by Nitro. This i
461
465
  ::code-group
462
466
 
463
467
  ```ts twoslash [module.ts]
464
- import { defineNuxtModule, createResolver, addServerScanDir } from '@nuxt/kit'
468
+ import { addServerScanDir, createResolver, defineNuxtModule } from '@nuxt/kit'
465
469
 
466
470
  export default defineNuxtModule({
467
471
  meta: {
468
472
  name: 'my-module',
469
473
  configKey: 'myModule',
470
474
  },
471
- setup(options) {
475
+ setup (options) {
472
476
  const { resolve } = createResolver(import.meta.url)
473
477
  addServerScanDir(resolve('./runtime/server'))
474
- }
478
+ },
475
479
  })
476
480
  ```
477
481
 
478
482
  ```ts twoslash [runtime/server/utils/index.ts]
479
- export function hello() {
483
+ export function hello () {
480
484
  return 'Hello from server utils!'
481
485
  }
482
486
  ```
@@ -485,7 +489,7 @@ export function hello() {
485
489
  You can then use the `hello` function in your server code.
486
490
 
487
491
  ```ts twoslash [runtime/server/api/hello.ts]
488
- function hello() {
492
+ function hello () {
489
493
  return 'Hello from server utils!'
490
494
  }
491
495
  // ---cut---