@nuxt/docs 0.0.0 → 3.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.navigation.yml +2 -0
- package/1.getting-started/.navigation.yml +3 -0
- package/1.getting-started/01.introduction.md +81 -0
- package/1.getting-started/02.installation.md +109 -0
- package/1.getting-started/03.configuration.md +226 -0
- package/1.getting-started/04.views.md +163 -0
- package/1.getting-started/05.assets.md +48 -0
- package/1.getting-started/06.styling.md +565 -0
- package/1.getting-started/07.routing.md +149 -0
- package/1.getting-started/08.seo-meta.md +360 -0
- package/1.getting-started/09.transitions.md +473 -0
- package/1.getting-started/10.data-fetching.md +795 -0
- package/1.getting-started/11.state-management.md +223 -0
- package/1.getting-started/12.error-handling.md +233 -0
- package/1.getting-started/13.server.md +94 -0
- package/1.getting-started/14.layers.md +92 -0
- package/1.getting-started/15.prerendering.md +194 -0
- package/1.getting-started/16.deployment.md +130 -0
- package/1.getting-started/17.testing.md +728 -0
- package/1.getting-started/18.upgrade.md +997 -0
- package/2.guide/.navigation.yml +2 -0
- package/2.guide/0.index.md +22 -0
- package/2.guide/1.concepts/.navigation.yml +3 -0
- package/2.guide/1.concepts/1.auto-imports.md +205 -0
- package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
- package/2.guide/1.concepts/2.vuejs-development.md +103 -0
- package/2.guide/1.concepts/3.rendering.md +255 -0
- package/2.guide/1.concepts/4.server-engine.md +62 -0
- package/2.guide/1.concepts/5.modules.md +48 -0
- package/2.guide/1.concepts/7.esm.md +299 -0
- package/2.guide/1.concepts/8.typescript.md +97 -0
- package/2.guide/1.concepts/9.code-style.md +22 -0
- package/2.guide/2.directory-structure/.navigation.yml +3 -0
- package/2.guide/2.directory-structure/0.nuxt.md +20 -0
- package/2.guide/2.directory-structure/0.output.md +18 -0
- package/2.guide/2.directory-structure/1.assets.md +16 -0
- package/2.guide/2.directory-structure/1.components.md +608 -0
- package/2.guide/2.directory-structure/1.composables.md +121 -0
- package/2.guide/2.directory-structure/1.content.md +64 -0
- package/2.guide/2.directory-structure/1.layouts.md +180 -0
- package/2.guide/2.directory-structure/1.middleware.md +209 -0
- package/2.guide/2.directory-structure/1.modules.md +66 -0
- package/2.guide/2.directory-structure/1.node_modules.md +12 -0
- package/2.guide/2.directory-structure/1.pages.md +440 -0
- package/2.guide/2.directory-structure/1.plugins.md +299 -0
- package/2.guide/2.directory-structure/1.public.md +27 -0
- package/2.guide/2.directory-structure/1.server.md +546 -0
- package/2.guide/2.directory-structure/1.shared.md +104 -0
- package/2.guide/2.directory-structure/1.utils.md +49 -0
- package/2.guide/2.directory-structure/2.env.md +75 -0
- package/2.guide/2.directory-structure/2.gitignore.md +37 -0
- package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
- package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
- package/2.guide/2.directory-structure/3.app-config.md +177 -0
- package/2.guide/2.directory-structure/3.app.md +72 -0
- package/2.guide/2.directory-structure/3.error.md +55 -0
- package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
- package/2.guide/2.directory-structure/3.package.md +32 -0
- package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
- package/2.guide/3.going-further/.navigation.yml +3 -0
- package/2.guide/3.going-further/1.experimental-features.md +689 -0
- package/2.guide/3.going-further/1.features.md +103 -0
- package/2.guide/3.going-further/1.internals.md +81 -0
- package/2.guide/3.going-further/10.runtime-config.md +174 -0
- package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
- package/2.guide/3.going-further/2.hooks.md +98 -0
- package/2.guide/3.going-further/3.modules.md +811 -0
- package/2.guide/3.going-further/4.kit.md +51 -0
- package/2.guide/3.going-further/6.nuxt-app.md +64 -0
- package/2.guide/3.going-further/7.layers.md +227 -0
- package/2.guide/3.going-further/9.debugging.md +115 -0
- package/2.guide/3.going-further/index.md +4 -0
- package/2.guide/4.recipes/.navigation.yml +3 -0
- package/2.guide/4.recipes/1.custom-routing.md +181 -0
- package/2.guide/4.recipes/2.vite-plugin.md +65 -0
- package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
- package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
- package/3.api/.navigation.yml +3 -0
- package/3.api/1.components/.navigation.yml +3 -0
- package/3.api/1.components/1.client-only.md +76 -0
- package/3.api/1.components/1.dev-only.md +51 -0
- package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
- package/3.api/1.components/10.nuxt-picture.md +27 -0
- package/3.api/1.components/11.teleports.md +40 -0
- package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
- package/3.api/1.components/13.nuxt-time.md +173 -0
- package/3.api/1.components/2.nuxt-page.md +154 -0
- package/3.api/1.components/3.nuxt-layout.md +156 -0
- package/3.api/1.components/4.nuxt-link.md +322 -0
- package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
- package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
- package/3.api/1.components/7.nuxt-welcome.md +25 -0
- package/3.api/1.components/8.nuxt-island.md +70 -0
- package/3.api/1.components/9.nuxt-img.md +43 -0
- package/3.api/2.composables/.navigation.yml +3 -0
- package/3.api/2.composables/on-prehydrate.md +60 -0
- package/3.api/2.composables/use-app-config.md +19 -0
- package/3.api/2.composables/use-async-data.md +212 -0
- package/3.api/2.composables/use-cookie.md +233 -0
- package/3.api/2.composables/use-error.md +32 -0
- package/3.api/2.composables/use-fetch.md +217 -0
- package/3.api/2.composables/use-head-safe.md +55 -0
- package/3.api/2.composables/use-head.md +69 -0
- package/3.api/2.composables/use-hydration.md +68 -0
- package/3.api/2.composables/use-lazy-async-data.md +47 -0
- package/3.api/2.composables/use-lazy-fetch.md +55 -0
- package/3.api/2.composables/use-loading-indicator.md +77 -0
- package/3.api/2.composables/use-nuxt-app.md +294 -0
- package/3.api/2.composables/use-nuxt-data.md +112 -0
- package/3.api/2.composables/use-preview-mode.md +118 -0
- package/3.api/2.composables/use-request-event.md +23 -0
- package/3.api/2.composables/use-request-fetch.md +52 -0
- package/3.api/2.composables/use-request-header.md +34 -0
- package/3.api/2.composables/use-request-headers.md +37 -0
- package/3.api/2.composables/use-request-url.md +41 -0
- package/3.api/2.composables/use-response-header.md +48 -0
- package/3.api/2.composables/use-route-announcer.md +60 -0
- package/3.api/2.composables/use-route.md +52 -0
- package/3.api/2.composables/use-router.md +92 -0
- package/3.api/2.composables/use-runtime-config.md +142 -0
- package/3.api/2.composables/use-runtime-hook.md +43 -0
- package/3.api/2.composables/use-seo-meta.md +80 -0
- package/3.api/2.composables/use-server-seo-meta.md +27 -0
- package/3.api/2.composables/use-state.md +48 -0
- package/3.api/3.utils/$fetch.md +98 -0
- package/3.api/3.utils/.navigation.yml +3 -0
- package/3.api/3.utils/abort-navigation.md +73 -0
- package/3.api/3.utils/add-route-middleware.md +88 -0
- package/3.api/3.utils/call-once.md +92 -0
- package/3.api/3.utils/clear-error.md +29 -0
- package/3.api/3.utils/clear-nuxt-data.md +23 -0
- package/3.api/3.utils/clear-nuxt-state.md +23 -0
- package/3.api/3.utils/create-error.md +55 -0
- package/3.api/3.utils/define-nuxt-component.md +53 -0
- package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
- package/3.api/3.utils/define-page-meta.md +234 -0
- package/3.api/3.utils/define-route-rules.md +52 -0
- package/3.api/3.utils/navigate-to.md +230 -0
- package/3.api/3.utils/on-before-route-leave.md +11 -0
- package/3.api/3.utils/on-before-route-update.md +11 -0
- package/3.api/3.utils/on-nuxt-ready.md +25 -0
- package/3.api/3.utils/prefetch-components.md +28 -0
- package/3.api/3.utils/preload-components.md +23 -0
- package/3.api/3.utils/preload-route-components.md +41 -0
- package/3.api/3.utils/prerender-routes.md +46 -0
- package/3.api/3.utils/refresh-cookie.md +46 -0
- package/3.api/3.utils/refresh-nuxt-data.md +91 -0
- package/3.api/3.utils/reload-nuxt-app.md +74 -0
- package/3.api/3.utils/set-page-layout.md +24 -0
- package/3.api/3.utils/set-response-status.md +36 -0
- package/3.api/3.utils/show-error.md +31 -0
- package/3.api/3.utils/update-app-config.md +27 -0
- package/3.api/4.commands/.navigation.yml +3 -0
- package/3.api/4.commands/add.md +112 -0
- package/3.api/4.commands/analyze.md +41 -0
- package/3.api/4.commands/build-module.md +42 -0
- package/3.api/4.commands/build.md +46 -0
- package/3.api/4.commands/cleanup.md +38 -0
- package/3.api/4.commands/dev.md +59 -0
- package/3.api/4.commands/devtools.md +38 -0
- package/3.api/4.commands/generate.md +41 -0
- package/3.api/4.commands/info.md +33 -0
- package/3.api/4.commands/init.md +46 -0
- package/3.api/4.commands/module.md +84 -0
- package/3.api/4.commands/prepare.md +36 -0
- package/3.api/4.commands/preview.md +43 -0
- package/3.api/4.commands/typecheck.md +42 -0
- package/3.api/4.commands/upgrade.md +37 -0
- package/3.api/5.kit/.navigation.yml +3 -0
- package/3.api/5.kit/1.modules.md +172 -0
- package/3.api/5.kit/10.runtime-config.md +27 -0
- package/3.api/5.kit/10.templates.md +283 -0
- package/3.api/5.kit/11.nitro.md +409 -0
- package/3.api/5.kit/12.resolving.md +268 -0
- package/3.api/5.kit/13.logging.md +65 -0
- package/3.api/5.kit/14.builder.md +491 -0
- package/3.api/5.kit/15.examples.md +41 -0
- package/3.api/5.kit/2.programmatic.md +125 -0
- package/3.api/5.kit/3.compatibility.md +230 -0
- package/3.api/5.kit/4.autoimports.md +144 -0
- package/3.api/5.kit/5.components.md +127 -0
- package/3.api/5.kit/6.context.md +130 -0
- package/3.api/5.kit/7.pages.md +295 -0
- package/3.api/5.kit/8.layout.md +80 -0
- package/3.api/5.kit/9.plugins.md +263 -0
- package/3.api/6.advanced/.navigation.yml +1 -0
- package/3.api/6.advanced/1.hooks.md +105 -0
- package/3.api/6.advanced/2.import-meta.md +60 -0
- package/3.api/6.nuxt-config.md +12 -0
- package/3.api/index.md +31 -0
- package/5.community/.navigation.yml +3 -0
- package/5.community/2.getting-help.md +48 -0
- package/5.community/3.reporting-bugs.md +50 -0
- package/5.community/4.contribution.md +205 -0
- package/5.community/5.framework-contribution.md +142 -0
- package/5.community/6.roadmap.md +79 -0
- package/5.community/7.changelog.md +92 -0
- package/6.bridge/.navigation.yml +3 -0
- package/6.bridge/1.overview.md +137 -0
- package/6.bridge/10.configuration.md +96 -0
- package/6.bridge/2.typescript.md +46 -0
- package/6.bridge/3.bridge-composition-api.md +132 -0
- package/6.bridge/4.plugins-and-middleware.md +65 -0
- package/6.bridge/5.nuxt3-compatible-api.md +204 -0
- package/6.bridge/6.meta.md +117 -0
- package/6.bridge/7.runtime-config.md +38 -0
- package/6.bridge/8.nitro.md +102 -0
- package/6.bridge/9.vite.md +37 -0
- package/7.migration/.navigation.yml +3 -0
- package/7.migration/1.overview.md +24 -0
- package/7.migration/10.bundling.md +28 -0
- package/7.migration/11.server.md +17 -0
- package/7.migration/2.configuration.md +240 -0
- package/7.migration/20.module-authors.md +94 -0
- package/7.migration/3.auto-imports.md +18 -0
- package/7.migration/4.meta.md +127 -0
- package/7.migration/5.plugins-and-middleware.md +80 -0
- package/7.migration/6.pages-and-layouts.md +233 -0
- package/7.migration/7.component-options.md +156 -0
- package/7.migration/8.runtime-config.md +58 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/package.json +16 -4
- package/dist/.gitkeep +0 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Templates"
|
|
3
|
+
description: Nuxt Kit provides a set of utilities to help you work with templates. These functions allow you to generate extra files during development and build time.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/kit/src/template.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
Templates allows to generate extra files during development and build time. These files will be available in virtual filesystem and can be used in plugins, layouts, components, etc. `addTemplate` and `addTypeTemplate` allow you to add templates to the Nuxt application. `updateTemplates` allows you to regenerate templates that match the filter.
|
|
12
|
+
|
|
13
|
+
## `addTemplate`
|
|
14
|
+
|
|
15
|
+
Renders given template during build into the project buildDir.
|
|
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)
|
|
54
|
+
|
|
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`
|
|
68
|
+
|
|
69
|
+
Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option.
|
|
70
|
+
|
|
71
|
+
- `options` (optional)
|
|
72
|
+
|
|
73
|
+
**Type**: `Options`
|
|
74
|
+
|
|
75
|
+
Options to pass to the template.
|
|
76
|
+
|
|
77
|
+
- `getContents` (optional)
|
|
78
|
+
|
|
79
|
+
**Type**: `(data: Options) => string | Promise<string>`
|
|
80
|
+
|
|
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.
|
|
82
|
+
|
|
83
|
+
- `write` (optional)
|
|
84
|
+
|
|
85
|
+
**Type**: `boolean`
|
|
86
|
+
|
|
87
|
+
If set to `true`, the template will be written to the destination file. Otherwise, the template will be used only in virtual filesystem.
|
|
88
|
+
|
|
89
|
+
### Examples
|
|
90
|
+
|
|
91
|
+
::code-group
|
|
92
|
+
|
|
93
|
+
```ts [module.ts]
|
|
94
|
+
// https://github.com/nuxt/bridge
|
|
95
|
+
import { addTemplate, defineNuxtModule } from '@nuxt/kit'
|
|
96
|
+
import { defu } from 'defu'
|
|
97
|
+
|
|
98
|
+
export default defineNuxtModule({
|
|
99
|
+
setup(options, nuxt) {
|
|
100
|
+
const globalMeta = defu(nuxt.options.app.head, {
|
|
101
|
+
charset: options.charset,
|
|
102
|
+
viewport: options.viewport
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
addTemplate({
|
|
106
|
+
filename: 'meta.config.mjs',
|
|
107
|
+
getContents: () => 'export default ' + JSON.stringify({ globalMeta, mixinKey: 'setup' })
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```ts [plugin.ts]
|
|
114
|
+
import { createHead as createServerHead } from '@unhead/vue/server'
|
|
115
|
+
import { createHead as createClientHead } from '@unhead/vue/client'
|
|
116
|
+
import { defineNuxtPlugin } from '#imports'
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
import metaConfig from '#build/meta.config.mjs'
|
|
119
|
+
|
|
120
|
+
export default defineNuxtPlugin((nuxtApp) => {
|
|
121
|
+
const createHead = import.meta.server ? createServerHead : createClientHead
|
|
122
|
+
const head = createHead()
|
|
123
|
+
head.push(metaConfig.globalMeta)
|
|
124
|
+
|
|
125
|
+
nuxtApp.vueApp.use(head)
|
|
126
|
+
})
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
::
|
|
130
|
+
|
|
131
|
+
## `addTypeTemplate`
|
|
132
|
+
|
|
133
|
+
Renders given template during build into the project buildDir, then registers it as types.
|
|
134
|
+
|
|
135
|
+
### Type
|
|
136
|
+
|
|
137
|
+
```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
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Parameters
|
|
161
|
+
|
|
162
|
+
#### `template`
|
|
163
|
+
|
|
164
|
+
**Type**: `NuxtTypeTemplate | string`
|
|
165
|
+
|
|
166
|
+
**Required**: `true`
|
|
167
|
+
|
|
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:
|
|
169
|
+
|
|
170
|
+
- `src` (optional)
|
|
171
|
+
|
|
172
|
+
**Type**: `string`
|
|
173
|
+
|
|
174
|
+
Path to the template. If `src` is not provided, `getContents` must be provided instead.
|
|
175
|
+
|
|
176
|
+
- `filename` (optional)
|
|
177
|
+
|
|
178
|
+
**Type**: `string`
|
|
179
|
+
|
|
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.
|
|
181
|
+
|
|
182
|
+
- `dst` (optional)
|
|
183
|
+
|
|
184
|
+
**Type**: `string`
|
|
185
|
+
|
|
186
|
+
Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option.
|
|
187
|
+
|
|
188
|
+
- `options` (optional)
|
|
189
|
+
|
|
190
|
+
**Type**: `Options`
|
|
191
|
+
|
|
192
|
+
Options to pass to the template.
|
|
193
|
+
|
|
194
|
+
- `getContents` (optional)
|
|
195
|
+
|
|
196
|
+
**Type**: `(data: Options) => string | Promise<string>`
|
|
197
|
+
|
|
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.
|
|
199
|
+
|
|
200
|
+
### Examples
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
// https://github.com/Hebilicious/nuxtpress
|
|
204
|
+
import { addTypeTemplate, defineNuxtModule } from "@nuxt/kit"
|
|
205
|
+
|
|
206
|
+
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
|
+
}`
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## `updateTemplates`
|
|
222
|
+
|
|
223
|
+
Regenerate templates that match the filter. If no filter is provided, all templates will be regenerated.
|
|
224
|
+
|
|
225
|
+
### Type
|
|
226
|
+
|
|
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
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Parameters
|
|
247
|
+
|
|
248
|
+
#### `options`
|
|
249
|
+
|
|
250
|
+
**Type**: `UpdateTemplatesOptions`
|
|
251
|
+
|
|
252
|
+
**Default**: `{}`
|
|
253
|
+
|
|
254
|
+
Options to pass to the template. This object can have the following property:
|
|
255
|
+
|
|
256
|
+
- `filter` (optional)
|
|
257
|
+
|
|
258
|
+
**Type**: `(template: ResolvedNuxtTemplate) => boolean`
|
|
259
|
+
|
|
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.
|
|
261
|
+
|
|
262
|
+
### Example
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
// https://github.com/nuxt/nuxt
|
|
266
|
+
import { defineNuxtModule, updateTemplates } from '@nuxt/kit'
|
|
267
|
+
|
|
268
|
+
export default defineNuxtModule({
|
|
269
|
+
setup(options, nuxt) {
|
|
270
|
+
// watch and rebuild routes template list when one of the pages changes
|
|
271
|
+
nuxt.hook('builder:watch', async (event, relativePath) => {
|
|
272
|
+
if (event === 'change') { return }
|
|
273
|
+
|
|
274
|
+
const path = resolve(nuxt.options.srcDir, relativePath)
|
|
275
|
+
if (updateTemplatePaths.some(dir => path.startsWith(dir))) {
|
|
276
|
+
await updateTemplates({
|
|
277
|
+
filter: template => template.filename === 'routes.mjs'
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
})
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
```
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Nitro"
|
|
3
|
+
description: Nuxt Kit provides a set of utilities to help you work with Nitro. These functions allow you to add server handlers, plugins, and prerender routes.
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/kit/src/nitro.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
Nitro is an open source TypeScript framework to build ultra-fast web servers. Nuxt uses Nitro as its server engine. You can use `useNitro` to access the Nitro instance, `addServerHandler` to add a server handler, `addDevServerHandler` to add a server handler to be used only in development mode, `addServerPlugin` to add a plugin to extend Nitro's runtime behavior, and `addPrerenderRoutes` to add routes to be prerendered by Nitro.
|
|
12
|
+
|
|
13
|
+
## `addServerHandler`
|
|
14
|
+
|
|
15
|
+
Adds a nitro server handler. Use it if you want to create server middleware or custom route.
|
|
16
|
+
|
|
17
|
+
### Type
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
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
|
+
```
|
|
30
|
+
|
|
31
|
+
### Parameters
|
|
32
|
+
|
|
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`
|
|
50
|
+
|
|
51
|
+
Path prefix or route. If an empty string used, will be used as a middleware.
|
|
52
|
+
|
|
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`
|
|
68
|
+
|
|
69
|
+
Router method matcher. If handler name contains method name, it will be used as a default value.
|
|
70
|
+
|
|
71
|
+
### Examples
|
|
72
|
+
|
|
73
|
+
::code-group
|
|
74
|
+
|
|
75
|
+
```ts [module.ts]
|
|
76
|
+
// https://github.com/nuxt-modules/robots
|
|
77
|
+
import { createResolver, defineNuxtModule, addServerHandler } from '@nuxt/kit'
|
|
78
|
+
|
|
79
|
+
export default defineNuxtModule({
|
|
80
|
+
setup(options) {
|
|
81
|
+
const resolver = createResolver(import.meta.url)
|
|
82
|
+
|
|
83
|
+
addServerHandler({
|
|
84
|
+
route: '/robots.txt',
|
|
85
|
+
handler: resolver.resolve('./runtime/robots.get')
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```ts [runtime/robots.get.ts]
|
|
92
|
+
export default defineEventHandler(() => {
|
|
93
|
+
return {
|
|
94
|
+
body: `User-agent: *\nDisallow: /`
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
::
|
|
100
|
+
|
|
101
|
+
## `addDevServerHandler`
|
|
102
|
+
|
|
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
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
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.
|
|
137
|
+
|
|
138
|
+
### Examples
|
|
139
|
+
|
|
140
|
+
::code-group
|
|
141
|
+
|
|
142
|
+
```ts [module.ts]
|
|
143
|
+
import { createResolver, defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
|
|
144
|
+
|
|
145
|
+
export default defineNuxtModule({
|
|
146
|
+
setup() {
|
|
147
|
+
const resolver = createResolver(import.meta.url)
|
|
148
|
+
|
|
149
|
+
addDevServerHandler({
|
|
150
|
+
handler: () => {
|
|
151
|
+
return {
|
|
152
|
+
body: `Response generated at ${new Date().toISOString()}`
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
route: '/_handler'
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
::
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
// https://github.com/nuxt-modules/tailwindcss
|
|
165
|
+
import { joinURL } from 'ufo'
|
|
166
|
+
import { defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
|
|
167
|
+
|
|
168
|
+
export default defineNuxtModule({
|
|
169
|
+
async setup(options) {
|
|
170
|
+
const route = joinURL(nuxt.options.app?.baseURL, '/_tailwind')
|
|
171
|
+
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
const createServer = await import('tailwind-config-viewer/server/index.js').then(r => r.default || r) as any
|
|
174
|
+
const viewerDevMiddleware = createServer({ tailwindConfigProvider: () => options, routerPrefix: route }).asMiddleware()
|
|
175
|
+
|
|
176
|
+
addDevServerHandler({ route, handler: viewerDevMiddleware })
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## `useNitro`
|
|
182
|
+
|
|
183
|
+
Returns the Nitro instance.
|
|
184
|
+
|
|
185
|
+
::warning
|
|
186
|
+
You can call `useNitro()` only after `ready` hook.
|
|
187
|
+
::
|
|
188
|
+
|
|
189
|
+
::note
|
|
190
|
+
Changes to the Nitro instance configuration are not applied.
|
|
191
|
+
::
|
|
192
|
+
|
|
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
|
|
212
|
+
|
|
213
|
+
```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'
|
|
216
|
+
|
|
217
|
+
export default defineNuxtModule({
|
|
218
|
+
setup(options, nuxt) {
|
|
219
|
+
const resolver = createResolver(import.meta.url)
|
|
220
|
+
|
|
221
|
+
nuxt.hook('ready', () => {
|
|
222
|
+
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
|
+
}
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## `addServerPlugin`
|
|
239
|
+
|
|
240
|
+
Add plugin to extend Nitro's runtime behavior.
|
|
241
|
+
|
|
242
|
+
::tip
|
|
243
|
+
You can read more about Nitro plugins in the [Nitro documentation](https://nitro.unjs.io/guide/plugins).
|
|
244
|
+
::
|
|
245
|
+
|
|
246
|
+
### Type
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
function addServerPlugin (plugin: string): void
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Parameters
|
|
253
|
+
|
|
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.
|
|
261
|
+
|
|
262
|
+
### Examples
|
|
263
|
+
|
|
264
|
+
::code-group
|
|
265
|
+
|
|
266
|
+
```ts [module.ts]
|
|
267
|
+
import { createResolver, defineNuxtModule, addServerPlugin } from '@nuxt/kit'
|
|
268
|
+
|
|
269
|
+
export default defineNuxtModule({
|
|
270
|
+
setup() {
|
|
271
|
+
const resolver = createResolver(import.meta.url)
|
|
272
|
+
addServerPlugin(resolver.resolve('./runtime/plugin.ts'))
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
```ts [runtime/plugin.ts]
|
|
278
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
279
|
+
nitroApp.hooks.hook("request", (event) => {
|
|
280
|
+
console.log("on request", event.path);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
|
|
284
|
+
console.log("on response", event.path, { body });
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
nitroApp.hooks.hook("afterResponse", (event, { body }) => {
|
|
288
|
+
console.log("on after response", event.path, { body });
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
::
|
|
294
|
+
|
|
295
|
+
## `addPrerenderRoutes`
|
|
296
|
+
|
|
297
|
+
Add routes to be prerendered to Nitro.
|
|
298
|
+
|
|
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
|
|
316
|
+
|
|
317
|
+
```ts
|
|
318
|
+
import { defineNuxtModule, addPrerenderRoutes } from '@nuxt/kit'
|
|
319
|
+
|
|
320
|
+
export default defineNuxtModule({
|
|
321
|
+
meta: {
|
|
322
|
+
name: 'nuxt-sitemap',
|
|
323
|
+
configKey: 'sitemap',
|
|
324
|
+
},
|
|
325
|
+
defaults: {
|
|
326
|
+
sitemapUrl: '/sitemap.xml',
|
|
327
|
+
prerender: true,
|
|
328
|
+
},
|
|
329
|
+
setup(options) {
|
|
330
|
+
if (options.prerender) {
|
|
331
|
+
addPrerenderRoutes(options.sitemapUrl)
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
})
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## `addServerImportsDir`
|
|
338
|
+
|
|
339
|
+
Add a directory to be scanned for auto-imports by Nitro.
|
|
340
|
+
|
|
341
|
+
### Type
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
function addServerImportsDir (dirs: string | string[], opts: { prepend?: boolean }): void
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Parameters
|
|
348
|
+
|
|
349
|
+
#### `dirs`
|
|
350
|
+
|
|
351
|
+
**Type**: `string | string[]`
|
|
352
|
+
|
|
353
|
+
**Required**: `true`
|
|
354
|
+
|
|
355
|
+
A directory or an array of directories to register to be scanned by Nitro
|
|
356
|
+
|
|
357
|
+
### Examples
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
import { defineNuxtModule, createResolver, addServerImportsDir } from '@nuxt/kit'
|
|
361
|
+
|
|
362
|
+
export default defineNuxtModule({
|
|
363
|
+
meta: {
|
|
364
|
+
name: 'my-module',
|
|
365
|
+
configKey: 'myModule',
|
|
366
|
+
},
|
|
367
|
+
setup(options) {
|
|
368
|
+
const resolver = createResolver(import.meta.url)
|
|
369
|
+
addServerImportsDir(resolver.resolve('./runtime/server/utils'))
|
|
370
|
+
}
|
|
371
|
+
})
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
## `addServerScanDir`
|
|
375
|
+
|
|
376
|
+
Add directories to be scanned by Nitro. It will check for subdirectories, which will be registered
|
|
377
|
+
just like the `~/server` folder is.
|
|
378
|
+
|
|
379
|
+
### Type
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
function addServerScanDir (dirs: string | string[], opts: { prepend?: boolean }): void
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### Parameters
|
|
386
|
+
|
|
387
|
+
#### `dirs`
|
|
388
|
+
|
|
389
|
+
**Type**: `string | string[]`
|
|
390
|
+
|
|
391
|
+
**Required**: `true`
|
|
392
|
+
|
|
393
|
+
A directory or an array of directories to register to be scanned for by Nitro as server dirs.
|
|
394
|
+
|
|
395
|
+
### Examples
|
|
396
|
+
|
|
397
|
+
```ts
|
|
398
|
+
import { defineNuxtModule, createResolver, addServerScanDir } from '@nuxt/kit'
|
|
399
|
+
export default defineNuxtModule({
|
|
400
|
+
meta: {
|
|
401
|
+
name: 'my-module',
|
|
402
|
+
configKey: 'myModule',
|
|
403
|
+
},
|
|
404
|
+
setup(options) {
|
|
405
|
+
const resolver = createResolver(import.meta.url)
|
|
406
|
+
addServerScanDir(resolver.resolve('./runtime/server'))
|
|
407
|
+
}
|
|
408
|
+
})
|
|
409
|
+
```
|