@nuxt/docs 3.17.1 → 3.17.2

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.
@@ -18,63 +18,82 @@ Register template as layout and add it to the layouts.
18
18
  In Nuxt 2 `error` layout can also be registered using this utility. In Nuxt 3+ `error` layout [replaced](/docs/getting-started/error-handling#rendering-an-error-page) with `error.vue` page in project root.
19
19
  ::
20
20
 
21
- ### Type
22
-
23
- ```ts
24
- function addLayout (layout: NuxtTemplate | string, name: string): void
25
-
26
- interface NuxtTemplate {
27
- src?: string
28
- filename?: string
29
- dst?: string
30
- options?: Record<string, any>
31
- getContents?: (data: Record<string, any>) => string | Promise<string>
32
- write?: boolean
33
- }
34
- ```
35
-
36
- ### Parameters
37
-
38
- #### `layout`
39
-
40
- **Type**: `NuxtTemplate | string`
41
-
42
- **Required**: `true`
43
-
44
- 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:
45
-
46
- - `src` (optional)
47
-
48
- **Type**: `string`
49
-
50
- Path to the template. If `src` is not provided, `getContents` must be provided instead.
21
+ ### Usage
51
22
 
52
- - `filename` (optional)
23
+ ```ts twoslash
24
+ import { addLayout, createResolver, defineNuxtModule } from '@nuxt/kit'
53
25
 
54
- **Type**: `string`
26
+ export default defineNuxtModule({
27
+ setup () {
28
+ const { resolve } = createResolver(import.meta.url)
55
29
 
56
- 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.
57
-
58
- - `dst` (optional)
59
-
60
- **Type**: `string`
61
-
62
- Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option.
63
-
64
- - `options` (optional)
30
+ addLayout({
31
+ src: resolve('templates/custom-layout.ts'),
32
+ filename: 'custom-layout.ts',
33
+ }, 'custom')
34
+ },
35
+ })
36
+ ```
65
37
 
66
- **Type**: `Options`
38
+ ### Type
67
39
 
68
- Options to pass to the template.
40
+ ```ts
41
+ function addLayout(layout: NuxtTemplate | string, name: string): void
42
+ ```
69
43
 
70
- - `getContents` (optional)
44
+ ### Parameters
71
45
 
72
- **Type**: `(data: Options) => string | Promise<string>`
46
+ **`layout`**: 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:
47
+
48
+ | Property | Type | Required | Description |
49
+ | ------------- | ------------------------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
50
+ | `src` | `string` | `false` | Path to the template. If `src` is not provided, `getContents` must be provided instead. |
51
+ | `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. |
52
+ | `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. |
53
+ | `options` | `Record<string, any>`{lang="ts"} | `false` | Options to pass to the template. |
54
+ | `getContents` | `(data) => 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. |
55
+ | `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. |
56
+
57
+ **`name`**: The name to register the layout under (e.g., `default`, `custom`, etc.).
58
+
59
+ ### Example
60
+
61
+ This will register a layout named `custom` that wraps pages with a header and footer.
62
+
63
+ ```ts twoslash
64
+ import { addLayout, defineNuxtModule } from '@nuxt/kit'
65
+
66
+ export default defineNuxtModule({
67
+ setup () {
68
+ addLayout({
69
+ write: true,
70
+ filename: 'my-layout.vue',
71
+ getContents: () => `<template>
72
+ <div>
73
+ <header>My Header</header>
74
+ <slot />
75
+ <footer>My Footer</footer>
76
+ </div>
77
+ </template>`,
78
+ }, 'custom')
79
+ },
80
+ })
81
+ ```
73
82
 
74
- 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.
83
+ You can then use this layout in your pages:
75
84
 
76
- - `write` (optional)
85
+ ```vue [pages/about.vue]
86
+ <script setup lang="ts">
87
+ definePageMeta({
88
+ layout: 'custom',
89
+ })
90
+ </script>
77
91
 
78
- **Type**: `boolean`
92
+ <template>
93
+ <div>About Page</div>
94
+ </template>
95
+ ```
79
96
 
80
- If set to `true`, the template will be written to the destination file. Otherwise, the template will be used only in virtual filesystem.
97
+ ::warning
98
+ Due to the lack of support for virtual `.vue` files by `@vitejs/plugin-vue`, you can work around this limitation by passing `write: true` to the first argument of `addLayout`.
99
+ ::
@@ -8,95 +8,81 @@ links:
8
8
  size: xs
9
9
  ---
10
10
 
11
- Plugins are self-contained code that usually add app-level functionality to Vue. In Nuxt, plugins are automatically imported from the `plugins` directory. However, if you need to ship a plugin with your module, Nuxt Kit provides the `addPlugin` and `addPluginTemplate` methods. These utils allow you to customize the plugin configuration to better suit your needs.
11
+ Plugins are self-contained code that usually add app-level functionality to Vue. In Nuxt, plugins are automatically imported from the `plugins/` directory. However, if you need to ship a plugin with your module, Nuxt Kit provides the `addPlugin` and `addPluginTemplate` methods. These utils allow you to customize the plugin configuration to better suit your needs.
12
12
 
13
13
  ## `addPlugin`
14
14
 
15
- Registers a Nuxt plugin and to the plugins array.
15
+ Registers a Nuxt plugin and adds it to the plugins array.
16
16
 
17
17
  ::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/injecting-plugins?friend=nuxt" target="_blank"}
18
- Watch Vue School video about addPlugin.
18
+ Watch Vue School video about `addPlugin`.
19
19
  ::
20
20
 
21
- ### Type
21
+ ### Usage
22
22
 
23
- ```ts
24
- function addPlugin (plugin: NuxtPlugin | string, options: AddPluginOptions): NuxtPlugin
23
+ ```ts twoslash
24
+ import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
25
25
 
26
- interface NuxtPlugin {
27
- src: string
28
- mode?: 'all' | 'server' | 'client'
29
- order?: number
30
- }
26
+ export default defineNuxtModule({
27
+ setup () {
28
+ const { resolve } = createResolver(import.meta.url)
31
29
 
32
- interface AddPluginOptions { append?: boolean }
30
+ addPlugin({
31
+ src: resolve('runtime/plugin.js'),
32
+ mode: 'client',
33
+ })
34
+ },
35
+ })
33
36
  ```
34
37
 
35
- ### Parameters
36
-
37
- #### `plugin`
38
-
39
- **Type**: `NuxtPlugin | string`
40
-
41
- **Required**: `true`
42
-
43
- A plugin object or a string with the path to the plugin. If a string is provided, it will be converted to a plugin object with `src` set to the string value. If a plugin object is provided, it must have the following properties:
44
-
45
- - `src` (required)
46
-
47
- **Type**: `string`
48
-
49
- Path to the plugin.
50
-
51
- - `mode` (optional)
52
-
53
- **Type**: `'all' | 'server' | 'client'`
54
-
55
- **Default**: `'all'`
38
+ ### Type
56
39
 
57
- If set to `'all'`, the plugin will be included in both client and server bundles. If set to `'server'`, the plugin will only be included in the server bundle. If set to `'client'`, the plugin will only be included in the client bundle. You can also use `.client` and `.server` modifiers when specifying `src` option to use plugin only in client or server side.
40
+ ```ts
41
+ function addPlugin(plugin: NuxtPlugin | string, options?: AddPluginOptions): NuxtPlugin
42
+ ```
58
43
 
59
- - `order` (optional)
44
+ ### Parameters
60
45
 
61
- **Type**: `number`
46
+ **`plugin`**: A plugin object or a string with the path to the plugin. If a string is provided, it will be converted to a plugin object with `src` set to the string value.
62
47
 
63
- **Default**: `0`
48
+ If a plugin object is provided, it must have the following properties:
64
49
 
65
- Order of the plugin. This allows more granular control over plugin order and should only be used by advanced users. Lower numbers run first, and user plugins default to `0`. It's recommended to set `order` to a number between `-20` for `pre`-plugins (plugins that run before Nuxt plugins) and `20` for `post`-plugins (plugins that run after Nuxt plugins).
50
+ | Property | Type | Required | Description |
51
+ | -------- | ------------------------------------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
52
+ | `src` | `string` | `true` | Path to the plugin file. |
53
+ | `mode` | `'all' \| 'server' \| 'client'`{lang="ts"} | `false` | If set to `'all'`, the plugin will be included in both client and server bundles. If set to `'server'`, the plugin will only be included in the server bundle. If set to `'client'`, the plugin will only be included in the client bundle. You can also use `.client` and `.server` modifiers when specifying `src` option to use plugin only in client or server side. |
54
+ | `order` | `number` | `false` | Order of the plugin. This allows more granular control over plugin order and should only be used by advanced users. Lower numbers run first, and user plugins default to `0`. It's recommended to set `order` to a number between `-20` for `pre`-plugins (plugins that run before Nuxt plugins) and `20` for `post`-plugins (plugins that run after Nuxt plugins). |
66
55
 
67
56
  ::warning
68
- Don't use `order` unless you know what you're doing. For most plugins, the default `order` of `0` is sufficient. To append a plugin to the end of the plugins array, use the `append` option instead.
57
+ Avoid using `order` unless necessary. Use `append` if you simply need to register plugins after Nuxt defaults.
69
58
  ::
70
59
 
71
- #### `options`
72
-
73
- **Type**: `AddPluginOptions`
60
+ **`options`**: Optional object with the following properties:
74
61
 
75
- **Default**: `{}`
76
-
77
- Options to pass to the plugin. If `append` is set to `true`, the plugin will be appended to the plugins array instead of prepended.
62
+ | Property | Type | Required | Description |
63
+ | -------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
64
+ | `append` | `boolean` | `false` | If `true`, the plugin will be appended to the plugins array. If `false`, it will be prepended. Defaults to `false`. |
78
65
 
79
66
  ### Examples
80
67
 
81
68
  ::code-group
82
69
 
83
70
  ```ts [module.ts]
84
- import { createResolver, defineNuxtModule, addPlugin } from '@nuxt/kit'
71
+ import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
85
72
 
86
73
  export default defineNuxtModule({
87
- setup() {
88
- const resolver = createResolver(import.meta.url)
74
+ setup () {
75
+ const { resolve } = createResolver(import.meta.url)
89
76
 
90
77
  addPlugin({
91
- src: resolver.resolve('runtime/plugin.js'),
92
- mode: 'client'
78
+ src: resolve('runtime/plugin.js'),
79
+ mode: 'client',
93
80
  })
94
- }
81
+ },
95
82
  })
96
83
  ```
97
84
 
98
- ```ts [runtime/plugin.js]
99
- // https://github.com/nuxt/nuxters
85
+ ```ts [runtime/plugin.ts]
100
86
  export default defineNuxtPlugin((nuxtApp) => {
101
87
  const colorMode = useColorMode()
102
88
 
@@ -115,139 +101,150 @@ export default defineNuxtPlugin((nuxtApp) => {
115
101
  Adds a template and registers as a nuxt plugin. This is useful for plugins that need to generate code at build time.
116
102
 
117
103
  ::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/injecting-plugin-templates?friend=nuxt" target="_blank"}
118
- Watch Vue School video about addPluginTemplate.
104
+ Watch Vue School video about `addPluginTemplate`.
119
105
  ::
120
106
 
107
+ ### Usage
108
+
109
+ ```ts twoslash
110
+ import { addPluginTemplate, defineNuxtModule } from '@nuxt/kit'
111
+
112
+ export default defineNuxtModule({
113
+ setup (options) {
114
+ addPluginTemplate({
115
+ filename: 'module-plugin.mjs',
116
+ getContents: () => `import { defineNuxtPlugin } from '#app/nuxt'
117
+ export default defineNuxtPlugin({
118
+ name: 'module-plugin',
119
+ setup (nuxtApp) {
120
+ ${options.log ? 'console.log("Plugin install")' : ''}
121
+ }
122
+ })`,
123
+ })
124
+ },
125
+ })
126
+ ```
127
+
121
128
  ### Type
122
129
 
123
130
  ```ts
124
- function addPluginTemplate (pluginOptions: NuxtPluginTemplate, options: AddPluginOptions): NuxtPlugin
125
-
126
- interface NuxtPluginTemplate<Options = Record<string, any>> {
127
- src?: string,
128
- filename?: string,
129
- dst?: string,
130
- mode?: 'all' | 'server' | 'client',
131
- options?: Options,
132
- getContents?: (data: Options) => string | Promise<string>,
133
- write?: boolean,
134
- order?: number
135
- }
136
-
137
- interface AddPluginOptions { append?: boolean }
138
-
139
- interface NuxtPlugin {
140
- src: string
141
- mode?: 'all' | 'server' | 'client'
142
- order?: number
143
- }
131
+ function addPluginTemplate(pluginOptions: NuxtPluginTemplate, options?: AddPluginOptions): NuxtPlugin
144
132
  ```
145
133
 
146
134
  ### Parameters
147
135
 
148
- #### `pluginOptions`
149
-
150
- **Type**: `NuxtPluginTemplate`
151
-
152
- **Required**: `true`
153
-
154
- A plugin template object with the following properties:
155
-
156
- - `src` (optional)
136
+ **`pluginOptions`**: A plugin template object with the following properties:
157
137
 
158
- **Type**: `string`
138
+ | Property | Type | Required | Description |
139
+ | ------------- | --------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
140
+ | `src` | `string` | `false` | Path to the template. If `src` is not provided, `getContents` must be provided instead. |
141
+ | `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. |
142
+ | `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. |
143
+ | `mode` | `'all' \| 'server' \| 'client'`{lang="ts"} | `false` | If set to `'all'`, the plugin will be included in both client and server bundles. If set to `'server'`, the plugin will only be included in the server bundle. If set to `'client'`, the plugin will only be included in the client bundle. You can also use `.client` and `.server` modifiers when specifying `src` option to use plugin only in client or server side. |
144
+ | `options` | `Record<string, any>`{lang="ts"} | `false` | Options to pass to the template. |
145
+ | `getContents` | `(data: Record<string, any>) => 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. |
146
+ | `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. |
147
+ | `order` | `number` | `false` | Order of the plugin. This allows more granular control over plugin order and should only be used by advanced users. Lower numbers run first, and user plugins default to `0`. It's recommended to set `order` to a number between `-20` for `pre`-plugins (plugins that run before Nuxt plugins) and `20` for `post`-plugins (plugins that run after Nuxt plugins). |
159
148
 
160
- Path to the template. If `src` is not provided, `getContents` must be provided instead.
161
-
162
- - `filename` (optional)
163
-
164
- **Type**: `string`
165
-
166
- 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.
167
-
168
- - `dst` (optional)
169
-
170
- **Type**: `string`
171
-
172
- Path to the destination file. If `dst` is not provided, it will be generated from the `filename` path and nuxt `buildDir` option.
173
-
174
- - `mode` (optional)
175
-
176
- **Type**: `'all' | 'server' | 'client'`
177
-
178
- **Default**: `'all'`
179
-
180
- If set to `'all'`, the plugin will be included in both client and server bundles. If set to `'server'`, the plugin will only be included in the server bundle. If set to `'client'`, the plugin will only be included in the client bundle. You can also use `.client` and `.server` modifiers when specifying `src` option to use plugin only in client or server side.
181
-
182
- - `options` (optional)
183
-
184
- **Type**: `Options`
149
+ ::warning
150
+ Prefer using `getContents` for dynamic plugin generation. Avoid setting `order` unless necessary.
151
+ ::
185
152
 
186
- Options to pass to the template.
153
+ **`options`**: Optional object with the following properties:
187
154
 
188
- - `getContents` (optional)
155
+ | Property | Type | Required | Description |
156
+ | -------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
157
+ | `append` | `boolean` | `false` | If `true`, the plugin will be appended to the plugins array. If `false`, it will be prepended. Defaults to `false`. |
189
158
 
190
- **Type**: `(data: Options) => string | Promise<string>`
159
+ ### Examples
191
160
 
192
- 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.
161
+ #### Generate a plugin template with different options
193
162
 
194
- - `write` (optional)
163
+ Use `addPluginTemplate` when you need to generate plugin code dynamically at build time. This allows you to generate different plugin contents based on the options passed to it. For example, Nuxt internally uses this function to generate Vue app configurations.
195
164
 
196
- **Type**: `boolean`
165
+ ```ts twoslash [module.ts]
166
+ import { addPluginTemplate, defineNuxtModule } from '@nuxt/kit'
197
167
 
198
- If set to `true`, the template will be written to the destination file. Otherwise, the template will be used only in virtual filesystem.
168
+ export default defineNuxtModule({
169
+ setup (_, nuxt) {
170
+ if (nuxt.options.vue.config && Object.values(nuxt.options.vue.config).some(v => v !== null && v !== undefined)) {
171
+ addPluginTemplate({
172
+ filename: 'vue-app-config.mjs',
173
+ write: true,
174
+ getContents: () => `import { defineNuxtPlugin } from '#app/nuxt'
175
+ export default defineNuxtPlugin({
176
+ name: 'nuxt:vue-app-config',
177
+ enforce: 'pre',
178
+ setup (nuxtApp) {
179
+ ${Object.keys(nuxt.options.vue.config!)
180
+ .map(k => `nuxtApp.vueApp.config[${JSON.stringify(k)}] = ${JSON.stringify(nuxt.options.vue.config![k as 'idPrefix'])}`)
181
+ .join('\n')
182
+ }
183
+ }
184
+ })`,
185
+ })
186
+ }
187
+ },
188
+ })
189
+ ```
199
190
 
200
- - `order` (optional)
191
+ This generates different plugin code depending on the provided configuration.
201
192
 
202
- **Type**: `number`
193
+ ::code-group
203
194
 
204
- **Default**: `0`
195
+ ```ts [nuxt.config.ts]
196
+ export default defineNuxtConfig({
197
+ vue: {
198
+ config: {
199
+ idPrefix: 'nuxt',
200
+ },
201
+ },
202
+ })
203
+ ```
205
204
 
206
- Order of the plugin. This allows more granular control over plugin order and should only be used by advanced users. Lower numbers run first, and user plugins default to `0`. It's recommended to set `order` to a number between `-20` for `pre`-plugins (plugins that run before Nuxt plugins) and `20` for `post`-plugins (plugins that run after Nuxt plugins).
205
+ ```ts [#build/vue-app-config.mjs]
206
+ import { defineNuxtPlugin } from '#app/nuxt'
207
+ export default defineNuxtPlugin({
208
+ name: 'nuxt:vue-app-config',
209
+ enforce: 'pre',
210
+ setup (nuxtApp) {
211
+ nuxtApp.vueApp.config["idPrefix"] = "nuxt"
212
+ }
213
+ })
214
+ ```
207
215
 
208
- ::warning
209
- Don't use `order` unless you know what you're doing. For most plugins, the default `order` of `0` is sufficient. To append a plugin to the end of the plugins array, use the `append` option instead.
210
216
  ::
211
217
 
212
- #### `options`
218
+ #### Using an EJS template to generate a plugin
213
219
 
214
- **Type**: `AddPluginOptions`
215
-
216
- **Default**: `{}`
217
-
218
- Options to pass to the plugin. If `append` is set to `true`, the plugin will be appended to the plugins array instead of prepended.
219
-
220
- ### Examples
220
+ You can also use an EJS template to generate your plugin. Options can be passed through the `options` property and then used within the EJS template to generate the plugin content.
221
221
 
222
222
  ::code-group
223
223
 
224
224
  ```ts [module.ts]
225
- // https://github.com/vuejs/vuefire
226
- import { createResolver, defineNuxtModule, addPluginTemplate } from '@nuxt/kit'
225
+ import { addPluginTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
227
226
 
228
227
  export default defineNuxtModule({
229
- setup() {
230
- const resolver = createResolver(import.meta.url)
228
+ setup (options, nuxt) {
229
+ const { resolve } = createResolver(import.meta.url)
231
230
 
232
231
  addPluginTemplate({
233
- src: resolve(templatesDir, 'plugin.ejs'),
232
+ src: resolve('templates/plugin.ejs'),
234
233
  filename: 'plugin.mjs',
235
234
  options: {
236
- ...options,
237
235
  ssr: nuxt.options.ssr,
238
236
  },
239
237
  })
240
- }
238
+ },
241
239
  })
242
240
  ```
243
241
 
244
- ```ts [runtime/plugin.ejs]
242
+ ```ts [templates/plugin.ejs]
245
243
  import { VueFire, useSSRInitialState } from 'vuefire'
246
244
  import { defineNuxtPlugin } from '#imports'
247
245
 
248
246
  export default defineNuxtPlugin((nuxtApp) => {
249
247
  const firebaseApp = nuxtApp.$firebaseApp
250
-
251
248
  nuxtApp.vueApp.use(VueFire, { firebaseApp })
252
249
 
253
250
  <% if(options.ssr) { %>
@@ -261,3 +258,7 @@ export default defineNuxtPlugin((nuxtApp) => {
261
258
  ```
262
259
 
263
260
  ::
261
+
262
+ ::warning
263
+ If you set `compatibilityVersion` to `4`, Nuxt no longer uses `lodash.template` to compile templates by default. You can still enable it via the `experimental.compileTemplate` option, but support for EJS templates will be removed entirely in the next major version.
264
+ ::
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/docs",
3
- "version": "3.17.1",
3
+ "version": "3.17.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/nuxt.git",