@nuxt/docs 3.17.1 → 3.17.3

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 (34) hide show
  1. package/1.getting-started/01.introduction.md +1 -1
  2. package/1.getting-started/02.installation.md +8 -0
  3. package/1.getting-started/03.configuration.md +1 -1
  4. package/1.getting-started/07.routing.md +1 -1
  5. package/1.getting-started/15.prerendering.md +2 -2
  6. package/1.getting-started/16.deployment.md +2 -2
  7. package/1.getting-started/18.upgrade.md +42 -3
  8. package/2.guide/1.concepts/1.auto-imports.md +1 -1
  9. package/2.guide/1.concepts/3.rendering.md +2 -2
  10. package/2.guide/1.concepts/4.server-engine.md +1 -1
  11. package/2.guide/2.directory-structure/1.server.md +6 -6
  12. package/2.guide/3.going-further/1.events.md +82 -0
  13. package/2.guide/3.going-further/11.nightly-release-channel.md +1 -3
  14. package/2.guide/3.going-further/2.hooks.md +1 -20
  15. package/2.guide/3.going-further/3.modules.md +1 -1
  16. package/3.api/2.composables/use-request-url.md +2 -2
  17. package/3.api/3.utils/navigate-to.md +1 -1
  18. package/3.api/5.kit/1.modules.md +105 -92
  19. package/3.api/5.kit/10.runtime-config.md +2 -0
  20. package/3.api/5.kit/10.templates.md +187 -163
  21. package/3.api/5.kit/11.nitro.md +207 -168
  22. package/3.api/5.kit/12.resolving.md +79 -133
  23. package/3.api/5.kit/13.logging.md +19 -29
  24. package/3.api/5.kit/14.builder.md +140 -373
  25. package/3.api/5.kit/2.programmatic.md +11 -64
  26. package/3.api/5.kit/3.compatibility.md +88 -135
  27. package/3.api/5.kit/4.autoimports.md +3 -3
  28. package/3.api/5.kit/5.components.md +22 -3
  29. package/3.api/5.kit/6.context.md +92 -48
  30. package/3.api/5.kit/7.pages.md +95 -197
  31. package/3.api/5.kit/8.layout.md +68 -49
  32. package/3.api/5.kit/9.plugins.md +140 -139
  33. package/3.api/6.advanced/1.hooks.md +1 -1
  34. package/package.json +1 -1
@@ -22,6 +22,8 @@ function useRuntimeConfig (): Record<string, unknown>
22
22
 
23
23
  It is also possible to update runtime configuration. This will be merged with the existing runtime configuration, and if Nitro has already been initialized it will trigger an HMR event to reload the Nitro runtime config.
24
24
 
25
+ ### Type
26
+
25
27
  ```ts
26
28
  function updateRuntimeConfig (config: Record<string, unknown>): void | Promise<void>
27
29
  ```
@@ -12,105 +12,79 @@ Templates allows to generate extra files during development and build time. Thes
12
12
 
13
13
  ## `addTemplate`
14
14
 
15
- Renders given template during build into the project buildDir.
15
+ Renders given template during build into the virtual file system, and optionally to disk in the project `buildDir`
16
16
 
17
- ### Type
18
-
19
- ```ts
20
- function addTemplate (template: NuxtTemplate | string): ResolvedNuxtTemplate
21
-
22
- interface NuxtTemplate {
23
- src?: string
24
- filename?: string
25
- dst?: string
26
- options?: Record<string, any>
27
- getContents?: (data: Record<string, any>) => string | Promise<string>
28
- write?: boolean
29
- }
30
-
31
- interface ResolvedNuxtTemplate {
32
- src: string
33
- filename: string
34
- dst: string
35
- options: Record<string, any>
36
- getContents: (data: Record<string, any>) => string | Promise<string>
37
- write: boolean
38
- filename: string
39
- dst: string
40
- }
41
- ```
42
-
43
- ### Parameters
44
-
45
- #### `template`
46
-
47
- **Type**: `NuxtTemplate | string`
48
-
49
- **Required**: `true`
50
-
51
- A template object or a string with the path to the template. If a string is provided, it will be converted to a template object with `src` set to the string value. If a template object is provided, it must have the following properties:
52
-
53
- - `src` (optional)
17
+ ### Usage
54
18
 
55
- **Type**: `string`
56
-
57
- Path to the template. If `src` is not provided, `getContents` must be provided instead.
58
-
59
- - `filename` (optional)
60
-
61
- **Type**: `string`
62
-
63
- Filename of the template. If `filename` is not provided, it will be generated from the `src` path. In this case, the `src` option is required.
64
-
65
- - `dst` (optional)
66
-
67
- **Type**: `string`
19
+ ```ts twoslash
20
+ import { addTemplate, defineNuxtModule } from '@nuxt/kit'
21
+ import { defu } from 'defu'
68
22
 
69
- Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option.
23
+ export default defineNuxtModule({
24
+ setup(options, nuxt) {
25
+ const globalMeta = defu(nuxt.options.app.head, {
26
+ charset: options.charset,
27
+ viewport: options.viewport
28
+ })
70
29
 
71
- - `options` (optional)
30
+ addTemplate({
31
+ filename: 'meta.config.mjs',
32
+ getContents: () => 'export default ' + JSON.stringify({ globalMeta, mixinKey: 'setup' })
33
+ })
34
+ }
35
+ })
36
+ ```
72
37
 
73
- **Type**: `Options`
38
+ ### Type
74
39
 
75
- Options to pass to the template.
40
+ ```ts twoslash
41
+ // @errors: 2391
42
+ import type { NuxtTemplate, ResolvedNuxtTemplate } from '@nuxt/schema'
43
+ // ---cut---
44
+ function addTemplate (template: NuxtTemplate | string): ResolvedNuxtTemplate
45
+ ```
76
46
 
77
- - `getContents` (optional)
47
+ ### Parameters
78
48
 
79
- **Type**: `(data: Options) => string | Promise<string>`
49
+ **template**: A template object or a string with the path to the template. If a string is provided, it will be converted to a template object with `src` set to the string value. If a template object is provided, it must have the following properties:
80
50
 
81
- A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. If `src` is provided, this function will be ignored.
51
+ | Property | Type | Required | Description |
52
+ | ---------- | -------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
53
+ | `src` | `string` | `false` | Path to the template. If `src` is not provided, `getContents` must be provided instead. |
54
+ | `filename` | `string` | `false` | Filename of the template. If `filename` is not provided, it will be generated from the `src` path. In this case, the `src` option is required. |
55
+ | `dst` | `string` | `false` | Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option. |
56
+ | `options` | `Options` | `false` | Options to pass to the template. |
57
+ | `getContents` | `(data: Options) => string \| Promise<string>`{lang="ts"} | `false` | A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. If `src` is provided, this function will be ignored. |
58
+ | `write` | `boolean` | `false` | If set to `true`, the template will be written to the destination file. Otherwise, the template will be used only in virtual filesystem. |
82
59
 
83
- - `write` (optional)
84
-
85
- **Type**: `boolean`
60
+ ### Examples
86
61
 
87
- If set to `true`, the template will be written to the destination file. Otherwise, the template will be used only in virtual filesystem.
62
+ #### Creating a Virtual File for Runtime Plugin
88
63
 
89
- ### Examples
64
+ In this example, we merge an object inside a module and consume the result in a runtime plugin.
90
65
 
91
- ::code-group
92
-
93
- ```ts [module.ts]
94
- // https://github.com/nuxt/bridge
66
+ ```ts twoslash [module.ts]
95
67
  import { addTemplate, defineNuxtModule } from '@nuxt/kit'
96
68
  import { defu } from 'defu'
97
69
 
98
70
  export default defineNuxtModule({
99
- setup(options, nuxt) {
71
+ setup (options, nuxt) {
100
72
  const globalMeta = defu(nuxt.options.app.head, {
101
73
  charset: options.charset,
102
- viewport: options.viewport
74
+ viewport: options.viewport,
103
75
  })
104
76
 
105
77
  addTemplate({
106
78
  filename: 'meta.config.mjs',
107
- getContents: () => 'export default ' + JSON.stringify({ globalMeta, mixinKey: 'setup' })
79
+ getContents: () => 'export default ' + JSON.stringify({ globalMeta, mixinKey: 'setup' }),
108
80
  })
109
- }
81
+ },
110
82
  })
111
83
  ```
112
84
 
113
- ```ts [plugin.ts]
85
+ In the module above, we generate a virtual file named `meta.config.mjs`. In the runtime plugin, we can import it using the `#build` alias:
86
+
87
+ ```ts [runtime/plugin.ts]
114
88
  import { createHead as createServerHead } from '@unhead/vue/server'
115
89
  import { createHead as createClientHead } from '@unhead/vue/client'
116
90
  import { defineNuxtPlugin } from '#imports'
@@ -126,147 +100,183 @@ export default defineNuxtPlugin((nuxtApp) => {
126
100
  })
127
101
  ```
128
102
 
129
- ::
130
-
131
103
  ## `addTypeTemplate`
132
104
 
133
105
  Renders given template during build into the project buildDir, then registers it as types.
134
106
 
107
+ ### Usage
108
+
109
+ ```ts twoslash
110
+ import { addTypeTemplate, defineNuxtModule } from '@nuxt/kit'
111
+
112
+ export default defineNuxtModule({
113
+ setup () {
114
+ addTypeTemplate({
115
+ filename: 'types/markdown.d.ts',
116
+ getContents: () => `declare module '*.md' {
117
+ import type { ComponentOptions } from 'vue'
118
+ const Component: ComponentOptions
119
+ export default Component
120
+ }`,
121
+ })
122
+ },
123
+ })
124
+ ```
125
+
135
126
  ### Type
136
127
 
137
128
  ```ts
138
- function addTypeTemplate (template: NuxtTypeTemplate | string): ResolvedNuxtTemplate
139
-
140
- interface NuxtTemplate {
141
- src?: string
142
- filename?: string
143
- dst?: string
144
- options?: Record<string, any>
145
- getContents?: (data: Record<string, any>) => string | Promise<string>
146
- }
147
-
148
- interface ResolvedNuxtTemplate {
149
- src: string
150
- filename: string
151
- dst: string
152
- options: Record<string, any>
153
- getContents: (data: Record<string, any>) => string | Promise<string>
154
- write: boolean
155
- filename: string
156
- dst: string
157
- }
129
+ function addTypeTemplate (template: NuxtTypeTemplate | string, context?: { nitro?: boolean, nuxt?: boolean }): ResolvedNuxtTemplate
158
130
  ```
159
131
 
160
132
  ### Parameters
161
133
 
162
- #### `template`
163
-
164
- **Type**: `NuxtTypeTemplate | string`
165
-
166
- **Required**: `true`
134
+ **template**: A template object or a string with the path to the template. If a string is provided, it will be converted to a template object with `src` set to the string value. If a template object is provided, it must have the following properties:
167
135
 
168
- A template object or a string with the path to the template. If a string is provided, it will be converted to a template object with `src` set to the string value. If a template object is provided, it must have the following properties:
136
+ | Property | Type | Required | Description |
137
+ | ---------- | -------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
138
+ | `src` | `string` | `false` | Path to the template. If `src` is not provided, `getContents` must be provided instead. |
139
+ | `filename` | `string` | `false` | Filename of the template. If `filename` is not provided, it will be generated from the `src` path. In this case, the `src` option is required. |
140
+ | `dst` | `string` | `false` | Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option. |
141
+ | `options` | `Options` | `false` | Options to pass to the template. |
142
+ | `getContents` | `(data: Options) => string \| Promise<string>`{lang="ts"} | `false` | A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. If `src` is provided, this function will be ignored. |
169
143
 
170
- - `src` (optional)
144
+ **context**: An optional context object can be passed to control where the type is added. If omitted, the type will only be added to the Nuxt context. This object supports the following properties:
171
145
 
172
- **Type**: `string`
146
+ | Property | Type | Required | Description |
147
+ | ---------- | -------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
148
+ | `nuxt` | `boolean` | `false` | If set to `true`, the type will be added to the Nuxt context. |
149
+ | `nitro` | `boolean` | `false` | If set to `true`, the type will be added to the Nitro context. |
173
150
 
174
- Path to the template. If `src` is not provided, `getContents` must be provided instead.
151
+ ### Examples
175
152
 
176
- - `filename` (optional)
153
+ #### Adding Type Templates to the Nitro Context
177
154
 
178
- **Type**: `string`
155
+ By default, -- only adds the type declarations to the Nuxt context. To also add them to the Nitro context, set nitro to true.
179
156
 
180
- Filename of the template. If `filename` is not provided, it will be generated from the `src` path. In this case, the `src` option is required.
157
+ ```ts twoslash
158
+ import { addTypeTemplate, defineNuxtModule } from '@nuxt/kit'
181
159
 
182
- - `dst` (optional)
160
+ export default defineNuxtModule({
161
+ setup () {
162
+ addTypeTemplate({
163
+ filename: 'types/auth.d.ts',
164
+ getContents: () => `declare module '#auth-utils' {
165
+ interface User {
166
+ id: string;
167
+ name: string;
168
+ }
183
169
 
184
- **Type**: `string`
170
+ }`,
171
+ }, {
172
+ nitro: true,
173
+ })
174
+ },
175
+ })
176
+ ```
185
177
 
186
- Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option.
178
+ This allows the `#auth-utils` module to be used within the Nitro context.
187
179
 
188
- - `options` (optional)
180
+ ```ts [server/api/auth.ts]
181
+ import type { User } from '#auth-utils'
189
182
 
190
- **Type**: `Options`
183
+ export default eventHandler(() => {
184
+ const user: User = {
185
+ id: '123',
186
+ name: 'John Doe',
187
+ }
191
188
 
192
- Options to pass to the template.
189
+ // do something with the user
193
190
 
194
- - `getContents` (optional)
191
+ return user
192
+ })
193
+ ```
195
194
 
196
- **Type**: `(data: Options) => string | Promise<string>`
195
+ ## `addServerTemplate`
197
196
 
198
- A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. If `src` is provided, this function will be ignored.
197
+ Adds a virtual file that can be used within the Nuxt Nitro server build.
199
198
 
200
- ### Examples
199
+ ### Usage
201
200
 
202
- ```ts
203
- // https://github.com/Hebilicious/nuxtpress
204
- import { addTypeTemplate, defineNuxtModule } from "@nuxt/kit"
201
+ ```ts twoslash
202
+ import { addServerTemplate, defineNuxtModule } from '@nuxt/kit'
205
203
 
206
204
  export default defineNuxtModule({
207
- setup() {
208
- addTypeTemplate({
209
- filename: "types/markdown.d.ts",
210
- getContents: () => /* ts */`
211
- declare module '*.md' {
212
- import type { ComponentOptions } from 'vue'
213
- const Component: ComponentOptions
214
- export default Component
215
- }`
205
+ setup () {
206
+ addServerTemplate({
207
+ filename: '#my-module/test.mjs',
208
+ getContents () {
209
+ return 'export const test = 123'
210
+ },
216
211
  })
217
- }
218
- }
212
+ },
213
+ })
219
214
  ```
220
215
 
221
- ## `updateTemplates`
222
-
223
- Regenerate templates that match the filter. If no filter is provided, all templates will be regenerated.
224
-
225
216
  ### Type
226
217
 
227
- ```ts
228
- async function updateTemplates (options: UpdateTemplatesOptions): void
229
-
230
- interface UpdateTemplatesOptions {
231
- filter?: (template: ResolvedNuxtTemplate) => boolean
232
- }
233
-
234
- interface ResolvedNuxtTemplate {
235
- src: string
236
- filename: string
237
- dst: string
238
- options: Record<string, any>
239
- getContents: (data: Record<string, any>) => string | Promise<string>
240
- write: boolean
241
- filename: string
242
- dst: string
243
- }
218
+ ```ts twoslash
219
+ // @errors: 2391
220
+ import type { NuxtServerTemplate } from '@nuxt/schema'
221
+ // ---cut---
222
+ function addServerTemplate (template: NuxtServerTemplate): NuxtServerTemplate
244
223
  ```
245
224
 
246
225
  ### Parameters
247
226
 
248
- #### `options`
227
+ **template**: A template object. It must have the following properties:
249
228
 
250
- **Type**: `UpdateTemplatesOptions`
229
+ | Property | Type | Required | Description |
230
+ | ------------- | ---------------------------------------------| -------- | --------------------------------------------------------------------------------------------------------------- |
231
+ | `filename` | `string` | `true` | Filename of the template. |
232
+ | `getContents` | `() => string \| Promise<string>`{lang="ts"} | `true` | A function that will be called with the `options` object. It should return a string or a promise that resolves to a string. |
251
233
 
252
- **Default**: `{}`
234
+ ### Examples
235
+
236
+ ### Creating a Virtual File for Nitro
253
237
 
254
- Options to pass to the template. This object can have the following property:
238
+ In this example, we create a virtual file that can be used within the Nuxt Nitro server build.
255
239
 
256
- - `filter` (optional)
240
+ ```ts twoslash
241
+ import { addServerTemplate, defineNuxtModule } from '@nuxt/kit'
242
+
243
+ export default defineNuxtModule({
244
+ setup () {
245
+ addServerTemplate({
246
+ filename: '#my-module/test.mjs',
247
+ getContents () {
248
+ return 'export const test = 123'
249
+ },
250
+ })
251
+ },
252
+ })
253
+ ```
257
254
 
258
- **Type**: `(template: ResolvedNuxtTemplate) => boolean`
255
+ And then in a runtime file
256
+
257
+ ```ts [server/api/test.ts]
258
+ import { test } from '#my-module/test.js'
259
+
260
+ export default eventHandler(() => {
261
+ return test
262
+ })
263
+ ```
264
+
265
+ ## `updateTemplates`
259
266
 
260
- A function that will be called with the `template` object. It should return a boolean indicating whether the template should be regenerated. If `filter` is not provided, all templates will be regenerated.
267
+ Regenerate templates that match the filter. If no filter is provided, all templates will be regenerated.
261
268
 
262
- ### Example
269
+ ### Usage
263
270
 
264
271
  ```ts
265
- // https://github.com/nuxt/nuxt
266
272
  import { defineNuxtModule, updateTemplates } from '@nuxt/kit'
273
+ import { resolve } from 'pathe'
267
274
 
268
275
  export default defineNuxtModule({
269
- setup(options, nuxt) {
276
+ setup (options, nuxt) {
277
+ const updateTemplatePaths = [
278
+ resolve(nuxt.options.srcDir, 'pages'),
279
+ ]
270
280
  // watch and rebuild routes template list when one of the pages changes
271
281
  nuxt.hook('builder:watch', async (event, relativePath) => {
272
282
  if (event === 'change') { return }
@@ -274,10 +284,24 @@ export default defineNuxtModule({
274
284
  const path = resolve(nuxt.options.srcDir, relativePath)
275
285
  if (updateTemplatePaths.some(dir => path.startsWith(dir))) {
276
286
  await updateTemplates({
277
- filter: template => template.filename === 'routes.mjs'
287
+ filter: template => template.filename === 'routes.mjs',
278
288
  })
279
289
  }
280
290
  })
281
- }
291
+ },
282
292
  })
283
293
  ```
294
+
295
+ ### Type
296
+
297
+ ```ts
298
+ async function updateTemplates (options: UpdateTemplatesOptions): void
299
+ ```
300
+
301
+ ### Parameters
302
+
303
+ **options**: Options to pass to the template. This object can have the following property:
304
+
305
+ | Property | Type | Required | Description |
306
+ | ---------- | -------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
307
+ | `filter` | `(template: ResolvedNuxtTemplate) => boolean`{lang="ts"} | `false` | A function that will be called with the `template` object. It should return a boolean indicating whether the template should be regenerated. If `filter` is not provided, all templates will be regenerated. |