@nuxt/docs 4.1.3 → 4.2.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 (99) hide show
  1. package/1.getting-started/02.installation.md +4 -4
  2. package/1.getting-started/03.configuration.md +4 -4
  3. package/1.getting-started/04.views.md +2 -2
  4. package/1.getting-started/05.assets.md +1 -1
  5. package/1.getting-started/06.styling.md +11 -11
  6. package/1.getting-started/07.routing.md +4 -0
  7. package/1.getting-started/09.transitions.md +6 -6
  8. package/1.getting-started/10.data-fetching.md +14 -14
  9. package/1.getting-started/11.state-management.md +2 -2
  10. package/1.getting-started/12.error-handling.md +4 -4
  11. package/1.getting-started/13.server.md +4 -4
  12. package/1.getting-started/14.layers.md +29 -10
  13. package/1.getting-started/17.testing.md +3 -3
  14. package/1.getting-started/18.upgrade.md +205 -48
  15. package/2.guide/1.directory-structure/1.app/1.components.md +4 -4
  16. package/2.guide/1.directory-structure/1.app/1.composables.md +1 -1
  17. package/2.guide/1.directory-structure/1.app/1.layouts.md +2 -2
  18. package/2.guide/1.directory-structure/1.app/1.middleware.md +4 -4
  19. package/2.guide/1.directory-structure/1.app/1.pages.md +13 -13
  20. package/2.guide/1.directory-structure/1.app/1.plugins.md +1 -5
  21. package/2.guide/1.directory-structure/1.node_modules.md +1 -1
  22. package/2.guide/1.directory-structure/1.public.md +1 -1
  23. package/2.guide/1.directory-structure/1.server.md +5 -5
  24. package/2.guide/1.directory-structure/2.env.md +1 -1
  25. package/2.guide/1.directory-structure/3.tsconfig.md +38 -7
  26. package/2.guide/2.concepts/1.auto-imports.md +2 -2
  27. package/2.guide/2.concepts/10.nuxt-lifecycle.md +3 -3
  28. package/2.guide/2.concepts/2.vuejs-development.md +3 -3
  29. package/2.guide/2.concepts/3.rendering.md +4 -4
  30. package/2.guide/2.concepts/7.esm.md +7 -3
  31. package/2.guide/2.concepts/8.typescript.md +15 -38
  32. package/2.guide/2.concepts/9.code-style.md +1 -1
  33. package/2.guide/3.going-further/1.experimental-features.md +94 -6
  34. package/2.guide/3.going-further/1.features.md +15 -3
  35. package/2.guide/3.going-further/1.internals.md +2 -2
  36. package/2.guide/3.going-further/2.hooks.md +1 -1
  37. package/2.guide/3.going-further/3.modules.md +112 -29
  38. package/2.guide/3.going-further/6.nuxt-app.md +5 -5
  39. package/2.guide/3.going-further/7.layers.md +46 -21
  40. package/2.guide/3.going-further/9.debugging.md +1 -1
  41. package/2.guide/4.recipes/1.custom-routing.md +4 -4
  42. package/2.guide/4.recipes/2.vite-plugin.md +41 -0
  43. package/2.guide/4.recipes/3.custom-usefetch.md +1 -1
  44. package/2.guide/5.best-practices/hydration.md +1 -1
  45. package/3.api/1.components/10.nuxt-picture.md +1 -1
  46. package/3.api/1.components/11.teleports.md +1 -1
  47. package/3.api/1.components/12.nuxt-route-announcer.md +1 -1
  48. package/3.api/1.components/13.nuxt-time.md +5 -1
  49. package/3.api/1.components/2.nuxt-page.md +1 -1
  50. package/3.api/1.components/4.nuxt-link.md +11 -11
  51. package/3.api/1.components/5.nuxt-loading-indicator.md +1 -1
  52. package/3.api/1.components/6.nuxt-error-boundary.md +1 -1
  53. package/3.api/2.composables/use-app-config.md +1 -1
  54. package/3.api/2.composables/use-async-data.md +80 -13
  55. package/3.api/2.composables/use-cookie.md +7 -7
  56. package/3.api/2.composables/use-fetch.md +6 -2
  57. package/3.api/2.composables/use-head-safe.md +37 -20
  58. package/3.api/2.composables/use-head.md +136 -36
  59. package/3.api/2.composables/use-hydration.md +24 -18
  60. package/3.api/2.composables/use-lazy-async-data.md +58 -9
  61. package/3.api/2.composables/use-lazy-fetch.md +65 -9
  62. package/3.api/2.composables/use-nuxt-app.md +7 -7
  63. package/3.api/2.composables/use-nuxt-data.md +1 -1
  64. package/3.api/2.composables/use-request-fetch.md +1 -1
  65. package/3.api/2.composables/use-route.md +1 -1
  66. package/3.api/2.composables/use-router.md +15 -15
  67. package/3.api/2.composables/use-runtime-hook.md +1 -1
  68. package/3.api/2.composables/use-state.md +1 -1
  69. package/3.api/3.utils/abort-navigation.md +2 -2
  70. package/3.api/3.utils/define-lazy-hydration-component.md +4 -4
  71. package/3.api/3.utils/define-nuxt-component.md +1 -1
  72. package/3.api/3.utils/define-nuxt-plugin.md +1 -1
  73. package/3.api/3.utils/define-nuxt-route-middleware.md +1 -1
  74. package/3.api/3.utils/define-page-meta.md +8 -8
  75. package/3.api/3.utils/navigate-to.md +5 -5
  76. package/3.api/3.utils/on-before-route-leave.md +1 -1
  77. package/3.api/3.utils/on-before-route-update.md +1 -1
  78. package/3.api/3.utils/refresh-cookie.md +1 -1
  79. package/3.api/3.utils/update-app-config.md +2 -2
  80. package/3.api/5.kit/1.modules.md +88 -2
  81. package/3.api/5.kit/11.nitro.md +4 -0
  82. package/3.api/5.kit/14.builder.md +66 -10
  83. package/3.api/5.kit/15.examples.md +3 -5
  84. package/3.api/5.kit/2.programmatic.md +2 -2
  85. package/3.api/5.kit/5.components.md +1 -0
  86. package/3.api/5.kit/9.head.md +132 -0
  87. package/3.api/6.advanced/1.hooks.md +7 -7
  88. package/3.api/6.nuxt-config.md +100 -29
  89. package/5.community/3.reporting-bugs.md +3 -3
  90. package/5.community/4.contribution.md +1 -1
  91. package/5.community/5.framework-contribution.md +8 -8
  92. package/5.community/6.roadmap.md +4 -4
  93. package/6.bridge/4.plugins-and-middleware.md +1 -1
  94. package/7.migration/2.configuration.md +2 -2
  95. package/7.migration/20.module-authors.md +1 -1
  96. package/7.migration/5.plugins-and-middleware.md +1 -1
  97. package/7.migration/6.pages-and-layouts.md +2 -2
  98. package/README.md +1 -1
  99. package/package.json +1 -1
@@ -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
 
@@ -47,7 +47,7 @@ export function defineNuxtModule<TOptions extends ModuleOptions> (
47
47
 
48
48
  export function defineNuxtModule<TOptions extends ModuleOptions> (): {
49
49
  with: <TOptionsDefaults extends Partial<TOptions>> (
50
- definition: ModuleDefinition<TOptions, TOptionsDefaults, true> | NuxtModule<TOptions, TOptionsDefaults, true>
50
+ definition: ModuleDefinition<TOptions, TOptionsDefaults, true> | NuxtModule<TOptions, TOptionsDefaults, true>,
51
51
  ) => NuxtModule<TOptions, TOptionsDefaults, true>
52
52
  }
53
53
  ```
@@ -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](/docs/4.x/api/kit/modules#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. |
@@ -239,8 +240,93 @@ export default defineNuxtModule({
239
240
  })
240
241
  ```
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
+ },
321
+ })
322
+ ```
323
+
242
324
  ## `installModule`
243
325
 
326
+ ::callout{type="warning"}
327
+ **Deprecated:** Use the [`moduleDependencies`](/docs/4.x/api/kit/modules#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
@@ -197,6 +197,10 @@ 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
@@ -14,6 +14,10 @@ Nuxt have builders based on [Vite](https://github.com/nuxt/nuxt/tree/main/packag
14
14
 
15
15
  Extends the Vite configuration. Callback function can be called multiple times, when applying to both client and server builds.
16
16
 
17
+ ::warning
18
+ This hook is now deprecated, and we recommend using a Vite plugin instead with a `config` hook, or &mdash; for environment-specific configuration &mdash; the `applyToEnvironment` hook.
19
+ ::
20
+
17
21
  ### Usage
18
22
 
19
23
  ```ts twoslash
@@ -30,6 +34,45 @@ export default defineNuxtModule({
30
34
  })
31
35
  ```
32
36
 
37
+ For environment-specific configuration in Nuxt 5+, use `addVitePlugin()` instead:
38
+
39
+ ```ts twoslash
40
+ import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
41
+
42
+ export default defineNuxtModule({
43
+ setup () {
44
+ // For global configuration (affects all environments)
45
+ addVitePlugin(() => ({
46
+ name: 'my-global-plugin',
47
+ config (config) {
48
+ // This runs before environment setup
49
+ config.optimizeDeps ||= {}
50
+ config.optimizeDeps.include ||= []
51
+ config.optimizeDeps.include.push('cross-fetch')
52
+ },
53
+ }))
54
+
55
+ // For environment-specific configuration
56
+ addVitePlugin(() => ({
57
+ name: 'my-client-plugin',
58
+ applyToEnvironment (environment) {
59
+ return environment.name === 'client'
60
+ },
61
+ configEnvironment (name, config) {
62
+ // This only affects the client environment
63
+ config.optimizeDeps ||= {}
64
+ config.optimizeDeps.include ||= []
65
+ config.optimizeDeps.include.push('client-only-package')
66
+ },
67
+ }))
68
+ },
69
+ })
70
+ ```
71
+
72
+ ::warning
73
+ **Important:** The `config` hook runs before `applyToEnvironment` and modifies the global configuration. Use `configEnvironment` for environment-specific configuration changes.
74
+ ::
75
+
33
76
  ### Type
34
77
 
35
78
  ```ts twoslash
@@ -40,8 +83,8 @@ import type { ExtendViteConfigOptions } from '@nuxt/kit'
40
83
  function extendViteConfig (callback: ((config: ViteConfig) => void), options?: ExtendViteConfigOptions): void
41
84
  ```
42
85
 
43
- ::read-more{to="https://vite.dev/config" target="_blank" icon="i-simple-icons-vite"}
44
- Checkout Vite website for more information about its configuration.
86
+ ::read-more{to="https://vite.dev/config/" target="_blank" icon="i-simple-icons-vite"}
87
+ Check out the Vite website for more information about its configuration.
45
88
  ::
46
89
 
47
90
  ### Parameters
@@ -54,8 +97,8 @@ Checkout Vite website for more information about its configuration.
54
97
  | --------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------ |
55
98
  | `dev` | `boolean` | `false` | If set to `true`, the callback function will be called when building in development mode. |
56
99
  | `build` | `boolean` | `false` | If set to `true`, the callback function will be called when building in production mode. |
57
- | `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle. |
58
- | `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle. |
100
+ | `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle. **Deprecated in Nuxt 5+.** Use `addVitePlugin()` with `applyToEnvironment()` instead. |
101
+ | `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle. **Deprecated in Nuxt 5+.** Use `addVitePlugin()` with `applyToEnvironment()` instead. |
59
102
  | `prepend` | `boolean` | `false` | If set to `true`, the callback function will be prepended to the array with `unshift()` instead of `push()`. |
60
103
 
61
104
  ## `extendWebpackConfig`
@@ -89,8 +132,8 @@ import type { ExtendWebpackConfigOptions } from '@nuxt/kit'
89
132
  function extendWebpackConfig (callback: ((config: WebpackConfig) => void), options?: ExtendWebpackConfigOptions): void
90
133
  ```
91
134
 
92
- ::read-more{to="https://webpack.js.org/configuration" target="_blank" icon="i-simple-icons-webpack"}
93
- Checkout webpack website for more information about its configuration.
135
+ ::read-more{to="https://webpack.js.org/configuration/" target="_blank" icon="i-simple-icons-webpack"}
136
+ Check out webpack website for more information about its configuration.
94
137
  ::
95
138
 
96
139
  ### Parameters
@@ -111,6 +154,10 @@ Checkout webpack website for more information about its configuration.
111
154
 
112
155
  Append Vite plugin to the config.
113
156
 
157
+ ::warning
158
+ In Nuxt 5+, plugins registered with `server: false` or `client: false` options will not have their `config` or `configResolved` hooks called. Instead, use the `applyToEnvironment()` method instead for environment-specific plugins.
159
+ ::
160
+
114
161
  ### Usage
115
162
 
116
163
  ```ts twoslash
@@ -131,6 +178,15 @@ export default defineNuxtModule({
131
178
  },
132
179
  setup (options) {
133
180
  addVitePlugin(svg4VuePlugin(options.svg4vue))
181
+
182
+ // or, to add a vite plugin to only one environment
183
+ addVitePlugin(() => ({
184
+ name: 'my-client-plugin',
185
+ applyToEnvironment (environment) {
186
+ return environment.name === 'client'
187
+ },
188
+ // ... rest of your client-only plugin
189
+ }))
134
190
  },
135
191
  })
136
192
  ```
@@ -146,7 +202,7 @@ function addVitePlugin (pluginOrGetter: VitePlugin | VitePlugin[] | (() => ViteP
146
202
  ```
147
203
 
148
204
  ::tip
149
- See [Vite website](https://vite.dev/guide/api-plugin.html) for more information about Vite plugins. You can also use [this repository](https://github.com/vitejs/awesome-vite#plugins) to find a plugin that suits your needs.
205
+ See [Vite website](https://vite.dev/guide/api-plugin) for more information about Vite plugins. You can also use [this repository](https://github.com/vitejs/awesome-vite#plugins) to find a plugin that suits your needs.
150
206
  ::
151
207
 
152
208
  ### Parameters
@@ -159,8 +215,8 @@ See [Vite website](https://vite.dev/guide/api-plugin.html) for more information
159
215
  | --------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------ |
160
216
  | `dev` | `boolean` | `false` | If set to `true`, the callback function will be called when building in development mode. |
161
217
  | `build` | `boolean` | `false` | If set to `true`, the callback function will be called when building in production mode. |
162
- | `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle. |
163
- | `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle. |
218
+ | `server` | `boolean` | `false` | If set to `true`, the callback function will be called when building the server bundle. **Deprecated in Nuxt 5+.** Use `applyToEnvironment()` instead. |
219
+ | `client` | `boolean` | `false` | If set to `true`, the callback function will be called when building the client bundle. **Deprecated in Nuxt 5+.** Use `applyToEnvironment()` instead. |
164
220
  | `prepend` | `boolean` | `false` | If set to `true`, the callback function will be prepended to the array with `unshift()` instead of `push()`. |
165
221
 
166
222
  ## `addWebpackPlugin`
@@ -205,7 +261,7 @@ function addWebpackPlugin (pluginOrGetter: WebpackPluginInstance | WebpackPlugin
205
261
  ```
206
262
 
207
263
  ::tip
208
- See [webpack website](https://webpack.js.org/concepts/plugins) for more information about webpack plugins. You can also use [this collection](https://webpack.js.org/awesome-webpack/#webpack-plugins) to find a plugin that suits your needs.
264
+ See [webpack website](https://webpack.js.org/concepts/plugins/) for more information about webpack plugins. You can also use [this collection](https://webpack.js.org/awesome-webpack/#webpack-plugins) to find a plugin that suits your needs.
209
265
  ::
210
266
 
211
267
  ### Parameters
@@ -22,11 +22,9 @@ import { buildNuxt, loadNuxt } from '@nuxt/kit'
22
22
  async function getViteConfig () {
23
23
  const nuxt = await loadNuxt({ cwd: process.cwd(), dev: false, overrides: { ssr: false } })
24
24
  return new Promise((resolve, reject) => {
25
- nuxt.hook('vite:extendConfig', (config, { isClient }) => {
26
- if (isClient) {
27
- resolve(config)
28
- throw new Error('_stop_')
29
- }
25
+ nuxt.hook('vite:extend', (config) => {
26
+ resolve(config)
27
+ throw new Error('_stop_')
30
28
  })
31
29
  buildNuxt(nuxt).catch((err) => {
32
30
  if (!err.toString().includes('_stop_')) {
@@ -8,7 +8,7 @@ links:
8
8
  size: xs
9
9
  ---
10
10
 
11
- Programmatic usage can be helpful when you want to use Nuxt programmatically, for example, when building a [CLI tool](https://github.com/nuxt/cli) or [test utils](https://github.com/nuxt/nuxt/tree/main/packages/test-utils).
11
+ Programmatic usage can be helpful when you want to use Nuxt programmatically, for example, when building a [CLI tool](https://github.com/nuxt/cli) or [test utils](https://github.com/nuxt/test-utils).
12
12
 
13
13
  ## `loadNuxt`
14
14
 
@@ -31,7 +31,7 @@ function loadNuxt (loadOptions?: LoadNuxtOptions): Promise<Nuxt>
31
31
 
32
32
  ## `buildNuxt`
33
33
 
34
- Build Nuxt programmatically. It will invoke the builder (currently [@nuxt/vite-builder](https://github.com/nuxt/nuxt/tree/main/packages/vite) or [@nuxt/webpack-builder](https://github.com/nuxt/nuxt/tree/main/packages/webpack)) to bundle the application.
34
+ Build Nuxt programmatically. It will invoke the builder (currently [@nuxt/vite-builder](https://github.com/nuxt/nuxt/blob/main/packages/vite) or [@nuxt/webpack-builder](https://github.com/nuxt/nuxt/blob/main/packages/webpack)) to bundle the application.
35
35
 
36
36
  ### Type
37
37
 
@@ -113,6 +113,7 @@ function addComponent (options: AddComponentOptions): void
113
113
  | ------------------ | ---------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
114
114
  | `name` | `string` | `true` | Component name. |
115
115
  | `filePath` | `string` | `true` | Path to the component. |
116
+ | `declarationPath` | `string` | `false` | Path to component's declaration file. It is used to generate components' [type templates](/docs/4.x/api/kit/templates#addtypetemplate); if not provided, `filePath` is used instead. |
116
117
  | `pascalName` | `string` | `false` | Pascal case component name. If not provided, it will be generated from the component name. |
117
118
  | `kebabName` | `string` | `false` | Kebab case component name. If not provided, it will be generated from the component name. |
118
119
  | `export` | `string` | `false` | Specify named or default export. If not provided, it will be set to `'default'`. |
@@ -0,0 +1,132 @@
1
+ ---
2
+ title: Head
3
+ description: Nuxt Kit provides utilities to help you manage head configuration in modules.
4
+ links:
5
+ - label: Source
6
+ icon: i-simple-icons-github
7
+ to: https://github.com/nuxt/nuxt/blob/main/packages/kit/src/head.ts
8
+ size: xs
9
+ ---
10
+
11
+ ## `setGlobalHead`
12
+
13
+ Sets global head configuration for your Nuxt application. This utility allows modules to programmatically configure meta tags, links, scripts, and other head elements that will be applied across all pages.
14
+
15
+ The provided head configuration will be merged with any existing head configuration using deep merging, with your provided values taking precedence.
16
+
17
+ ::tip
18
+ This is particularly useful for modules that need to inject global meta tags, stylesheets, or scripts into the application head.
19
+ ::
20
+
21
+ ### Type
22
+
23
+ ```ts twoslash
24
+ // @errors: 2391
25
+ // ---cut---
26
+ import type { SerializableHead } from '@unhead/vue/types'
27
+
28
+ interface AppHeadMetaObject extends SerializableHead {
29
+ charset?: string
30
+ viewport?: string
31
+ }
32
+
33
+ function setGlobalHead (head: AppHeadMetaObject): void
34
+ ```
35
+
36
+ ### Parameters
37
+
38
+ #### `head`
39
+
40
+ **Type**: `AppHeadMetaObject`
41
+
42
+ An object containing head configuration. All properties are optional and will be merged with existing configuration:
43
+
44
+ - `charset`: Character encoding for the document
45
+ - `viewport`: Viewport meta tag configuration
46
+ - `meta`: Array of meta tag objects
47
+ - `link`: Array of link tag objects (stylesheets, icons, etc.)
48
+ - `style`: Array of inline style tag objects
49
+ - `script`: Array of script tag objects
50
+ - `noscript`: Array of noscript tag objects
51
+ - `title`: Default page title
52
+ - `titleTemplate`: Template for formatting page titles
53
+ - `bodyAttrs`: Attributes to add to the `<body>` tag
54
+ - `htmlAttrs`: Attributes to add to the `<html>` tag
55
+
56
+ ### Examples
57
+
58
+ #### Adding Global Meta Tags
59
+
60
+ ```ts
61
+ import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
62
+
63
+ export default defineNuxtModule({
64
+ setup () {
65
+ setGlobalHead({
66
+ meta: [
67
+ { name: 'theme-color', content: '#ffffff' },
68
+ { name: 'author', content: 'Your Name' },
69
+ ],
70
+ })
71
+ },
72
+ })
73
+ ```
74
+
75
+ #### Injecting Global Stylesheets
76
+
77
+ ```ts
78
+ import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
79
+
80
+ export default defineNuxtModule({
81
+ setup () {
82
+ setGlobalHead({
83
+ link: [
84
+ {
85
+ rel: 'stylesheet',
86
+ href: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
87
+ },
88
+ ],
89
+ })
90
+ },
91
+ })
92
+ ```
93
+
94
+ #### Adding Global Scripts
95
+
96
+ ```ts
97
+ import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
98
+
99
+ export default defineNuxtModule({
100
+ setup () {
101
+ setGlobalHead({
102
+ script: [
103
+ {
104
+ src: 'https://cdn.example.com/analytics.js',
105
+ async: true,
106
+ defer: true,
107
+ },
108
+ ],
109
+ })
110
+ },
111
+ })
112
+ ```
113
+
114
+ #### Setting HTML Attributes
115
+
116
+ ```ts
117
+ import { defineNuxtModule, setGlobalHead } from '@nuxt/kit'
118
+
119
+ export default defineNuxtModule({
120
+ setup () {
121
+ setGlobalHead({
122
+ htmlAttrs: {
123
+ lang: 'en',
124
+ dir: 'ltr',
125
+ },
126
+ bodyAttrs: {
127
+ class: 'custom-body-class',
128
+ },
129
+ })
130
+ },
131
+ })
132
+ ```
@@ -15,20 +15,20 @@ Hook | Arguments | Environment | Description
15
15
  `app:error` | `err` | Server & Client | Called when a fatal error occurs.
16
16
  `app:error:cleared` | `{ redirect? }` | Server & Client | Called when a fatal error occurs.
17
17
  `vue:setup` | - | Server & Client | Called when the setup of Nuxt root is initialized. This callback must be synchronous.
18
- `vue:error` | `err, target, info` | Server & Client | Called when a vue error propagates to the root component. [Learn More](https://vuejs.org/api/composition-api-lifecycle.html#onerrorcaptured).
18
+ `vue:error` | `err, target, info` | Server & Client | Called when a vue error propagates to the root component. [Learn More](https://vuejs.org/api/composition-api-lifecycle#onerrorcaptured).
19
19
  `app:rendered` | `renderContext` | Server | Called when SSR rendering is done.
20
20
  `app:redirected` | - | Server | Called before SSR redirection.
21
21
  `app:beforeMount` | `vueApp` | Client | Called before mounting the app, called only on client side.
22
22
  `app:mounted` | `vueApp` | Client | Called when Vue app is initialized and mounted in browser.
23
- `app:suspense:resolve` | `appComponent` | Client | On [Suspense](https://vuejs.org/guide/built-ins/suspense.html#suspense) resolved event.
23
+ `app:suspense:resolve` | `appComponent` | Client | On [Suspense](https://vuejs.org/guide/built-ins/suspense#suspense) resolved event.
24
24
  `app:manifest:update` | `{ id, timestamp }` | Client | Called when there is a newer version of your app detected.
25
25
  `app:data:refresh` | `keys?` | Client | Called when `refreshNuxtData` is called.
26
26
  `link:prefetch` | `to` | Client | Called when a `<NuxtLink>` is observed to be prefetched.
27
- `page:start` | `pageComponent?` | Client | Called on [Suspense](https://vuejs.org/guide/built-ins/suspense.html#suspense) inside of `NuxtPage` pending event.
28
- `page:finish` | `pageComponent?` | Client | Called on [Suspense](https://vuejs.org/guide/built-ins/suspense.html#suspense) inside of `NuxtPage` resolved event.
27
+ `page:start` | `pageComponent?` | Client | Called on [Suspense](https://vuejs.org/guide/built-ins/suspense#suspense) inside of `NuxtPage` pending event.
28
+ `page:finish` | `pageComponent?` | Client | Called on [Suspense](https://vuejs.org/guide/built-ins/suspense#suspense) inside of `NuxtPage` resolved event.
29
29
  `page:loading:start` | - | Client | Called when the `setup()` of the new page is running.
30
30
  `page:loading:end` | - | Client | Called after `page:finish`
31
- `page:transition:finish`| `pageComponent?` | Client | After page transition [onAfterLeave](https://vuejs.org/guide/built-ins/transition.html#javascript-hooks) event.
31
+ `page:transition:finish`| `pageComponent?` | Client | After page transition [onAfterLeave](https://vuejs.org/guide/built-ins/transition#javascript-hooks) event.
32
32
  `dev:ssr-logs` | `logs` | Client | Called with an array of server-side logs that have been passed to the client (if `features.devLogs` is enabled).
33
33
  `page:view-transition:start` | `transition` | Client | Called after `document.startViewTransition` is called when [experimental viewTransition support is enabled](/docs/4.x/getting-started/transitions#view-transitions-api-experimental).
34
34
 
@@ -75,8 +75,8 @@ Hook | Arguments | Description
75
75
  `schema:beforeWrite` | `schema` | Called before writing the given schema.
76
76
  `schema:written` | - | Called after the schema is written.
77
77
  `vite:extend` | `viteBuildContext` | Allows extending Vite default context.
78
- `vite:extendConfig` | `viteInlineConfig, env` | Allows extending Vite default config.
79
- `vite:configResolved` | `viteInlineConfig, env` | Allows reading the resolved Vite config.
78
+ `vite:extendConfig` | `viteInlineConfig, env` | Allows extending Vite default config. **Deprecated in Nuxt 5+.** In Nuxt 5, this operates on a shared configuration rather than separate client/server configs.
79
+ `vite:configResolved` | `viteInlineConfig, env` | Allows reading the resolved Vite config. **Deprecated in Nuxt 5+.** In Nuxt 5, this operates on a shared configuration rather than separate client/server configs.
80
80
  `vite:serverCreated` | `viteServer, env` | Called when the Vite server is created.
81
81
  `vite:compiled` | - | Called after Vite server is compiled.
82
82
  `webpack:config` | `webpackConfigs` | Called before configuring the webpack compiler.