@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
@@ -12,83 +12,67 @@ Nitro is an open source TypeScript framework to build ultra-fast web servers. Nu
12
12
 
13
13
  ## `addServerHandler`
14
14
 
15
- Adds a nitro server handler. Use it if you want to create server middleware or custom route.
15
+ Adds a Nitro server handler. Use this if you want to create server middleware or a custom route.
16
+
17
+ ### Usage
18
+
19
+ ```ts twoslash
20
+ import { createResolver, defineNuxtModule, addServerHandler } from '@nuxt/kit'
21
+
22
+ export default defineNuxtModule({
23
+ setup(options) {
24
+ const { resolve } = createResolver(import.meta.url)
25
+
26
+ addServerHandler({
27
+ route: '/robots.txt',
28
+ handler: resolve('./runtime/robots.get')
29
+ })
30
+ }
31
+ })
32
+ ```
16
33
 
17
34
  ### Type
18
35
 
19
36
  ```ts
20
37
  function addServerHandler (handler: NitroEventHandler): void
21
-
22
- export interface NitroEventHandler {
23
- handler: string;
24
- route?: string;
25
- middleware?: boolean;
26
- lazy?: boolean;
27
- method?: string;
28
- }
29
38
  ```
30
39
 
31
40
  ### Parameters
32
41
 
33
- #### `handler`
34
-
35
- **Type**: `NitroEventHandler`
36
-
37
- **Required**: `true`
38
-
39
- A handler object with the following properties:
40
-
41
- - `handler` (required)
42
-
43
- **Type**: `string`
44
-
45
- Path to event handler.
46
-
47
- - `route` (optional)
48
-
49
- **Type**: `string`
42
+ **handler**: A handler object with the following properties:
50
43
 
51
- Path prefix or route. If an empty string used, will be used as a middleware.
44
+ | Property | Type | Required | Description |
45
+ | ----------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
46
+ | `handler` | `string` | `true` | Path to event handler. |
47
+ | `route` | `string` | `false` | Path prefix or route. If an empty string used, will be used as a middleware. |
48
+ | `middleware`| `boolean` | `false` | Specifies this is a middleware handler. Middleware are called on every route and should normally return nothing to pass to the next handlers. |
49
+ | `lazy` | `boolean` | `false` | Use lazy loading to import the handler. This is useful when you only want to load the handler on demand. |
50
+ | `method` | `string` | `false` | Router method matcher. If handler name contains method name, it will be used as a default value. |
52
51
 
53
- - `middleware` (optional)
54
-
55
- **Type**: `boolean`
56
-
57
- Specifies this is a middleware handler. Middleware are called on every route and should normally return nothing to pass to the next handlers.
58
-
59
- - `lazy` (optional)
60
-
61
- **Type**: `boolean`
62
-
63
- Use lazy loading to import handler.
64
-
65
- - `method` (optional)
66
-
67
- **Type**: `string`
52
+ ### Examples
68
53
 
69
- Router method matcher. If handler name contains method name, it will be used as a default value.
54
+ #### Basic Usage
70
55
 
71
- ### Examples
56
+ You can use `addServerHandler` to add a server handler from your module.
72
57
 
73
58
  ::code-group
74
59
 
75
- ```ts [module.ts]
76
- // https://github.com/nuxt-modules/robots
60
+ ```ts twoslash [module.ts]
77
61
  import { createResolver, defineNuxtModule, addServerHandler } from '@nuxt/kit'
78
62
 
79
63
  export default defineNuxtModule({
80
64
  setup(options) {
81
- const resolver = createResolver(import.meta.url)
65
+ const { resolve } = createResolver(import.meta.url)
82
66
 
83
67
  addServerHandler({
84
68
  route: '/robots.txt',
85
- handler: resolver.resolve('./runtime/robots.get')
69
+ handler: resolve('./runtime/robots.get')
86
70
  })
87
71
  }
88
72
  })
89
73
  ```
90
74
 
91
- ```ts [runtime/robots.get.ts]
75
+ ```ts twoslash [runtime/robots.get.ts]
92
76
  export default defineEventHandler(() => {
93
77
  return {
94
78
  body: `User-agent: *\nDisallow: /`
@@ -98,75 +82,67 @@ export default defineEventHandler(() => {
98
82
 
99
83
  ::
100
84
 
101
- ## `addDevServerHandler`
85
+ When you access `/robots.txt`, it will return the following response:
102
86
 
103
- Adds a nitro server handler to be used only in development mode. This handler will be excluded from production build.
104
-
105
- ### Type
106
-
107
- ```ts
108
- function addDevServerHandler (handler: NitroDevEventHandler): void
109
-
110
- export interface NitroDevEventHandler {
111
- handler: EventHandler;
112
- route?: string;
113
- }
87
+ ```txt
88
+ User-agent: *
89
+ Disallow: /
114
90
  ```
115
91
 
116
- ### Parameters
117
-
118
- #### `handler`
119
-
120
- **Type**: `NitroEventHandler`
121
-
122
- **Required**: `true`
123
-
124
- A handler object with the following properties:
125
-
126
- - `handler` (required)
127
-
128
- **Type**: `string`
129
-
130
- The event handler.
131
-
132
- - `route` (optional)
133
-
134
- **Type**: `string`
135
-
136
- Path prefix or route. If an empty string used, will be used as a middleware.
92
+ ## `addDevServerHandler`
137
93
 
138
- ### Examples
94
+ Adds a Nitro server handler to be used only in development mode. This handler will be excluded from production build.
139
95
 
140
- ::code-group
96
+ ### Usage
141
97
 
142
- ```ts [module.ts]
98
+ ```ts twoslash
99
+ import { defineEventHandler } from 'h3'
143
100
  import { createResolver, defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
144
101
 
145
102
  export default defineNuxtModule({
146
103
  setup() {
147
- const resolver = createResolver(import.meta.url)
148
-
149
104
  addDevServerHandler({
150
- handler: () => {
105
+ handler: defineEventHandler(() => {
151
106
  return {
152
107
  body: `Response generated at ${new Date().toISOString()}`
153
108
  }
154
- },
109
+ }),
155
110
  route: '/_handler'
156
111
  })
157
112
  }
158
113
  })
159
114
  ```
160
115
 
161
- ::
116
+ ### Type
117
+
118
+ ```ts twoslash
119
+ // @errors: 2391
120
+ import type { NitroDevEventHandler } from 'nitropack'
121
+ // ---cut---
122
+ function addDevServerHandler (handler: NitroDevEventHandler): void
123
+ ```
124
+
125
+ ### Parameters
126
+
127
+ **handler**: A handler object with the following properties:
128
+
129
+ | Property | Type | Required | Description |
130
+ | ----------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
131
+ | `handler` | `EventHandler` | `true` | Event handler. |
132
+ | `route` | `string` | `false` | Path prefix or route. If an empty string used, will be used as a middleware. |
133
+
134
+ ### Examples
135
+
136
+ #### Basic Usage
137
+
138
+ In some cases, you may want to create a server handler specifically for development purposes, such as a Tailwind config viewer.
162
139
 
163
140
  ```ts
164
- // https://github.com/nuxt-modules/tailwindcss
165
141
  import { joinURL } from 'ufo'
166
142
  import { defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
167
143
 
168
144
  export default defineNuxtModule({
169
- async setup(options) {
145
+ async setup(options, nuxt) {
170
146
  const route = joinURL(nuxt.options.app?.baseURL, '/_tailwind')
171
147
 
172
148
  // @ts-ignore
@@ -190,29 +166,10 @@ You can call `useNitro()` only after `ready` hook.
190
166
  Changes to the Nitro instance configuration are not applied.
191
167
  ::
192
168
 
193
- ### Type
194
-
195
- ```ts
196
- function useNitro (): Nitro
197
-
198
- export interface Nitro {
199
- options: NitroOptions;
200
- scannedHandlers: NitroEventHandler[];
201
- vfs: Record<string, string>;
202
- hooks: Hookable<NitroHooks>;
203
- unimport?: Unimport;
204
- logger: ConsolaInstance;
205
- storage: Storage;
206
- close: () => Promise<void>;
207
- updateConfig: (config: NitroDynamicConfig) => void | Promise<void>;
208
- }
209
- ```
210
-
211
- ### Examples
169
+ ### Usage
212
170
 
213
171
  ```ts
214
- // https://github.com/nuxt/nuxt/blob/4e05650cde31ca73be4d14b1f0d23c7854008749/packages/nuxt/src/core/nuxt.ts#L404
215
- import { defineNuxtModule, useNitro, addPlugin, createResolver } from '@nuxt/kit'
172
+ import { defineNuxtModule, useNitro } from '@nuxt/kit'
216
173
 
217
174
  export default defineNuxtModule({
218
175
  setup(options, nuxt) {
@@ -220,29 +177,39 @@ export default defineNuxtModule({
220
177
 
221
178
  nuxt.hook('ready', () => {
222
179
  const nitro = useNitro()
223
- if (nitro.options.static && nuxt.options.experimental.payloadExtraction === undefined) {
224
- console.warn('Using experimental payload extraction for full-static output. You can opt-out by setting `experimental.payloadExtraction` to `false`.')
225
- nuxt.options.experimental.payloadExtraction = true
226
- }
227
- nitro.options.replace['process.env.NUXT_PAYLOAD_EXTRACTION'] = String(!!nuxt.options.experimental.payloadExtraction)
228
- nitro.options._config.replace!['process.env.NUXT_PAYLOAD_EXTRACTION'] = String(!!nuxt.options.experimental.payloadExtraction)
229
-
230
- if (!nuxt.options.dev && nuxt.options.experimental.payloadExtraction) {
231
- addPlugin(resolver.resolve(nuxt.options.appDir, 'plugins/payload.client'))
232
- }
180
+ // Do something with Nitro instance
233
181
  })
234
182
  }
235
183
  })
236
184
  ```
237
185
 
186
+ ### Type
187
+
188
+ ```ts
189
+ function useNitro (): Nitro
190
+ ```
191
+
238
192
  ## `addServerPlugin`
239
193
 
240
194
  Add plugin to extend Nitro's runtime behavior.
241
195
 
242
196
  ::tip
243
- You can read more about Nitro plugins in the [Nitro documentation](https://nitro.unjs.io/guide/plugins).
197
+ You can read more about Nitro plugins in the [Nitro documentation](https://nitro.build/guide/plugins).
244
198
  ::
245
199
 
200
+ ### Usage
201
+
202
+ ```ts twoslash
203
+ import { createResolver, defineNuxtModule, addServerPlugin } from '@nuxt/kit'
204
+
205
+ export default defineNuxtModule({
206
+ setup() {
207
+ const { resolve } = createResolver(import.meta.url)
208
+ addServerPlugin(resolve('./runtime/plugin.ts'))
209
+ }
210
+ })
211
+ ```
212
+
246
213
  ### Type
247
214
 
248
215
  ```ts
@@ -251,13 +218,9 @@ function addServerPlugin (plugin: string): void
251
218
 
252
219
  ### Parameters
253
220
 
254
- #### `plugin`
255
-
256
- **Type**: `string`
257
-
258
- **Required**: `true`
259
-
260
- Path to the plugin. The plugin must export a function that accepts Nitro instance as an argument.
221
+ | Property | Type | Required | Description |
222
+ | ----------- | -------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
223
+ | `plugin` | `string` | `true` | Path to the plugin. The plugin must export a default function that accepts the Nitro instance as an argument. |
261
224
 
262
225
  ### Examples
263
226
 
@@ -268,8 +231,8 @@ import { createResolver, defineNuxtModule, addServerPlugin } from '@nuxt/kit'
268
231
 
269
232
  export default defineNuxtModule({
270
233
  setup() {
271
- const resolver = createResolver(import.meta.url)
272
- addServerPlugin(resolver.resolve('./runtime/plugin.ts'))
234
+ const { resolve } = createResolver(import.meta.url)
235
+ addServerPlugin(resolve('./runtime/plugin.ts'))
273
236
  }
274
237
  })
275
238
  ```
@@ -296,23 +259,7 @@ export default defineNitroPlugin((nitroApp) => {
296
259
 
297
260
  Add routes to be prerendered to Nitro.
298
261
 
299
- ### Type
300
-
301
- ```ts
302
- function function addPrerenderRoutes (routes: string | string[]): void
303
- ```
304
-
305
- ### Parameters
306
-
307
- #### `routes`
308
-
309
- **Type**: `string | string[]`
310
-
311
- **Required**: `true`
312
-
313
- A route or an array of routes to prerender.
314
-
315
- ### Examples
262
+ ### Usage
316
263
 
317
264
  ```ts
318
265
  import { defineNuxtModule, addPrerenderRoutes } from '@nuxt/kit'
@@ -334,10 +281,39 @@ export default defineNuxtModule({
334
281
  })
335
282
  ```
336
283
 
284
+ ### Type
285
+
286
+ ```ts
287
+ function addPrerenderRoutes (routes: string | string[]): void
288
+ ```
289
+
290
+ ### Parameters
291
+
292
+ | Property | Type | Required | Description |
293
+ | ----------- | ------------------------------- | -------- | ---------------------------------------------- |
294
+ | `routes` | `string \| string[]`{lang="ts"} | `true` | A route or an array of routes to prerender. |
295
+
337
296
  ## `addServerImportsDir`
338
297
 
339
298
  Add a directory to be scanned for auto-imports by Nitro.
340
299
 
300
+ ### Usage
301
+
302
+ ```ts twoslash
303
+ import { defineNuxtModule, createResolver, addServerImportsDir } from '@nuxt/kit'
304
+
305
+ export default defineNuxtModule({
306
+ meta: {
307
+ name: 'my-module',
308
+ configKey: 'myModule',
309
+ },
310
+ setup(options) {
311
+ const { resolve } = createResolver(import.meta.url)
312
+ addServerImportsDir(resolve('./runtime/server/composables'))
313
+ }
314
+ })
315
+ ```
316
+
341
317
  ### Type
342
318
 
343
319
  ```ts
@@ -346,17 +322,18 @@ function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean
346
322
 
347
323
  ### Parameters
348
324
 
349
- #### `dirs`
350
-
351
- **Type**: `string | string[]`
325
+ | Property | Type | Required | Description |
326
+ | ----------- | ------------------------------- | -------- | ---------------------------------------------- |
327
+ | `dirs` | `string \| string[]`{lang="ts"} | `true` | A directory or an array of directories to register to be scanned by Nitro. |
328
+ | `opts` | `{ prepend?: boolean }` | `false` | Options for the import directory. If `prepend` is `true`, the directory is added to the beginning of the scan list. |
352
329
 
353
- **Required**: `true`
330
+ ### Examples
354
331
 
355
- A directory or an array of directories to register to be scanned by Nitro
332
+ You can use `addServerImportsDir` to add a directory to be scanned by Nitro. This is useful when you want Nitro to auto-import functions from a custom server directory.
356
333
 
357
- ### Examples
334
+ ::code-group
358
335
 
359
- ```ts
336
+ ```ts twoslash [module.ts]
360
337
  import { defineNuxtModule, createResolver, addServerImportsDir } from '@nuxt/kit'
361
338
 
362
339
  export default defineNuxtModule({
@@ -365,17 +342,58 @@ export default defineNuxtModule({
365
342
  configKey: 'myModule',
366
343
  },
367
344
  setup(options) {
368
- const resolver = createResolver(import.meta.url)
369
- addServerImportsDir(resolver.resolve('./runtime/server/utils'))
345
+ const { resolve } = createResolver(import.meta.url)
346
+ addServerImportsDir(resolve('./runtime/server/composables'))
370
347
  }
371
348
  })
372
349
  ```
373
350
 
351
+ ```ts twoslash [runtime/server/composables/index.ts]
352
+ export function useApiSecret() {
353
+ const { apiSecret } = useRuntimeConfig()
354
+ return apiSecret
355
+ }
356
+ ```
357
+
358
+ ::
359
+
360
+ You can then use the `useApiSecret` function in your server code:
361
+
362
+ ```ts twoslash [runtime/server/api/hello.ts]
363
+ const useApiSecret = (): string => ''
364
+ // ---cut---
365
+ export default defineEventHandler(() => {
366
+ const apiSecret = useApiSecret()
367
+ // Do something with the apiSecret
368
+ })
369
+ ```
370
+
374
371
  ## `addServerScanDir`
375
372
 
376
373
  Add directories to be scanned by Nitro. It will check for subdirectories, which will be registered
377
374
  just like the `~/server` folder is.
378
375
 
376
+ ::note
377
+ Only `~/server/api`, `~/server/routes`, `~/server/middleware`, and `~/server/utils` are scanned.
378
+ ::
379
+
380
+ ### Usage
381
+
382
+ ```ts twoslash
383
+ import { defineNuxtModule, createResolver, addServerScanDir } from '@nuxt/kit'
384
+
385
+ export default defineNuxtModule({
386
+ meta: {
387
+ name: 'my-module',
388
+ configKey: 'myModule',
389
+ },
390
+ setup(options) {
391
+ const { resolve } = createResolver(import.meta.url)
392
+ addServerScanDir(resolve('./runtime/server'))
393
+ }
394
+ })
395
+ ```
396
+
379
397
  ### Type
380
398
 
381
399
  ```ts
@@ -384,26 +402,47 @@ function addServerScanDir (dirs: string | string[], opts: { prepend?: boolean })
384
402
 
385
403
  ### Parameters
386
404
 
387
- #### `dirs`
388
-
389
- **Type**: `string | string[]`
405
+ | Property | Type | Required | Description |
406
+ | ----------- | ------------------------------- | -------- | ---------------------------------------------- |
407
+ | `dirs` | `string \| string[]`{lang="ts"} | `true` | A directory or an array of directories to register to be scanned for by Nitro as server dirs. |
408
+ | `opts` | `{ prepend?: boolean }` | `false` | Options for the import directory. If `prepend` is `true`, the directory is added to the beginning of the scan list. |
390
409
 
391
- **Required**: `true`
410
+ ### Examples
392
411
 
393
- A directory or an array of directories to register to be scanned for by Nitro as server dirs.
412
+ You can use `addServerScanDir` to add a directory to be scanned by Nitro. This is useful when you want to add a custom server directory.
394
413
 
395
- ### Examples
414
+ ::code-group
396
415
 
397
- ```ts
416
+ ```ts twoslash [module.ts]
398
417
  import { defineNuxtModule, createResolver, addServerScanDir } from '@nuxt/kit'
418
+
399
419
  export default defineNuxtModule({
400
420
  meta: {
401
421
  name: 'my-module',
402
422
  configKey: 'myModule',
403
423
  },
404
424
  setup(options) {
405
- const resolver = createResolver(import.meta.url)
406
- addServerScanDir(resolver.resolve('./runtime/server'))
425
+ const { resolve } = createResolver(import.meta.url)
426
+ addServerScanDir(resolve('./runtime/server'))
407
427
  }
408
428
  })
409
429
  ```
430
+
431
+ ```ts twoslash [runtime/server/utils/index.ts]
432
+ export function hello() {
433
+ return 'Hello from server utils!'
434
+ }
435
+ ```
436
+ ::
437
+
438
+ You can then use the `hello` function in your server code.
439
+
440
+ ```ts twoslash [runtime/server/api/hello.ts]
441
+ function hello() {
442
+ return 'Hello from server utils!'
443
+ }
444
+ // ---cut---
445
+ export default defineEventHandler(() => {
446
+ return hello() // Hello from server utils!
447
+ })
448
+ ```