@nuxt/docs 4.1.2 → 4.1.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.
- package/1.getting-started/01.introduction.md +6 -6
- package/1.getting-started/02.installation.md +2 -1
- package/1.getting-started/03.configuration.md +36 -36
- package/1.getting-started/04.views.md +9 -9
- package/1.getting-started/05.assets.md +15 -9
- package/1.getting-started/06.styling.md +58 -48
- package/1.getting-started/07.routing.md +17 -17
- package/1.getting-started/08.seo-meta.md +59 -46
- package/1.getting-started/09.transitions.md +49 -44
- package/1.getting-started/10.data-fetching.md +104 -81
- package/1.getting-started/11.state-management.md +26 -19
- package/1.getting-started/12.error-handling.md +22 -20
- package/1.getting-started/13.server.md +8 -8
- package/1.getting-started/14.layers.md +22 -16
- package/1.getting-started/15.prerendering.md +32 -32
- package/1.getting-started/16.deployment.md +10 -10
- package/1.getting-started/17.testing.md +44 -44
- package/1.getting-started/18.upgrade.md +53 -71
- package/2.guide/0.index.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/.navigation.yml +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/0.nuxt.md +3 -3
- package/2.guide/1.directory-structure/0.output.md +18 -0
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/.navigation.yml +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.assets.md +4 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.components.md +45 -28
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.composables.md +13 -13
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.layouts.md +19 -15
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.middleware.md +31 -25
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.pages.md +39 -37
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.plugins.md +25 -25
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/1.utils.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app-config.md +18 -18
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.app.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.app/3.error.md +6 -6
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.content.md +7 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.modules.md +8 -8
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.node_modules.md +2 -2
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.public.md +2 -2
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.server.md +35 -35
- package/2.guide/{2.directory-structure → 1.directory-structure}/1.shared.md +7 -7
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.env.md +8 -8
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.gitignore.md +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtignore.md +5 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/2.nuxtrc.md +4 -4
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.nuxt-config.md +3 -3
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.package.md +1 -1
- package/2.guide/{2.directory-structure → 1.directory-structure}/3.tsconfig.md +3 -3
- package/2.guide/{1.concepts → 2.concepts}/1.auto-imports.md +22 -22
- package/2.guide/{1.concepts → 2.concepts}/10.nuxt-lifecycle.md +17 -17
- package/2.guide/{1.concepts → 2.concepts}/2.vuejs-development.md +13 -13
- package/2.guide/{1.concepts → 2.concepts}/3.rendering.md +24 -22
- package/2.guide/{1.concepts → 2.concepts}/4.server-engine.md +6 -6
- package/2.guide/{1.concepts → 2.concepts}/5.modules.md +5 -5
- package/2.guide/{1.concepts → 2.concepts}/7.esm.md +17 -15
- package/2.guide/{1.concepts → 2.concepts}/8.typescript.md +12 -12
- package/2.guide/3.going-further/1.events.md +3 -3
- package/2.guide/3.going-further/1.experimental-features.md +246 -85
- package/2.guide/3.going-further/1.features.md +44 -9
- package/2.guide/3.going-further/1.internals.md +25 -25
- package/2.guide/3.going-further/10.runtime-config.md +12 -12
- package/2.guide/3.going-further/11.nightly-release-channel.md +1 -1
- package/2.guide/3.going-further/2.hooks.md +14 -14
- package/2.guide/3.going-further/3.modules.md +96 -94
- package/2.guide/3.going-further/4.kit.md +7 -7
- package/2.guide/3.going-further/6.nuxt-app.md +8 -8
- package/2.guide/3.going-further/7.layers.md +62 -53
- package/2.guide/3.going-further/9.debugging.md +2 -2
- package/2.guide/4.recipes/1.custom-routing.md +30 -30
- package/2.guide/4.recipes/2.vite-plugin.md +4 -4
- package/2.guide/4.recipes/3.custom-usefetch.md +13 -13
- package/2.guide/4.recipes/4.sessions-and-authentication.md +35 -21
- package/2.guide/5.best-practices/hydration.md +4 -4
- package/2.guide/5.best-practices/performance.md +17 -17
- package/3.api/1.components/1.client-only.md +6 -3
- package/3.api/1.components/1.nuxt-client-fallback.md +10 -7
- package/3.api/1.components/10.nuxt-picture.md +1 -1
- package/3.api/1.components/11.teleports.md +5 -2
- package/3.api/1.components/12.nuxt-route-announcer.md +9 -9
- package/3.api/1.components/13.nuxt-time.md +44 -17
- package/3.api/1.components/2.nuxt-page.md +6 -6
- package/3.api/1.components/3.nuxt-layout.md +15 -10
- package/3.api/1.components/4.nuxt-link.md +42 -22
- package/3.api/1.components/5.nuxt-loading-indicator.md +3 -3
- package/3.api/1.components/6.nuxt-error-boundary.md +12 -10
- package/3.api/2.composables/on-prehydrate.md +2 -2
- package/3.api/2.composables/use-app-config.md +1 -1
- package/3.api/2.composables/use-async-data.md +20 -20
- package/3.api/2.composables/use-cookie.md +29 -21
- package/3.api/2.composables/use-error.md +2 -2
- package/3.api/2.composables/use-fetch.md +58 -32
- package/3.api/2.composables/use-head-safe.md +7 -7
- package/3.api/2.composables/use-head.md +5 -5
- package/3.api/2.composables/use-hydration.md +6 -6
- package/3.api/2.composables/use-lazy-async-data.md +4 -4
- package/3.api/2.composables/use-lazy-fetch.md +4 -4
- package/3.api/2.composables/use-loading-indicator.md +12 -12
- package/3.api/2.composables/use-nuxt-app.md +22 -22
- package/3.api/2.composables/use-nuxt-data.md +8 -8
- package/3.api/2.composables/use-preview-mode.md +15 -18
- package/3.api/2.composables/use-request-event.md +1 -1
- package/3.api/2.composables/use-request-fetch.md +3 -3
- package/3.api/2.composables/use-request-header.md +1 -1
- package/3.api/2.composables/use-request-headers.md +4 -4
- package/3.api/2.composables/use-request-url.md +1 -1
- package/3.api/2.composables/use-response-header.md +9 -10
- package/3.api/2.composables/use-route-announcer.md +4 -4
- package/3.api/2.composables/use-route.md +1 -1
- package/3.api/2.composables/use-router.md +10 -8
- package/3.api/2.composables/use-runtime-config.md +11 -11
- package/3.api/2.composables/use-runtime-hook.md +2 -2
- package/3.api/2.composables/use-seo-meta.md +4 -4
- package/3.api/2.composables/use-server-seo-meta.md +6 -6
- package/3.api/2.composables/use-state.md +5 -5
- package/3.api/3.utils/$fetch.md +10 -8
- package/3.api/3.utils/abort-navigation.md +3 -3
- package/3.api/3.utils/add-route-middleware.md +5 -5
- package/3.api/3.utils/call-once.md +6 -6
- package/3.api/3.utils/clear-error.md +3 -3
- package/3.api/3.utils/clear-nuxt-data.md +3 -3
- package/3.api/3.utils/clear-nuxt-state.md +3 -3
- package/3.api/3.utils/create-error.md +2 -2
- package/3.api/3.utils/define-lazy-hydration-component.md +13 -13
- package/3.api/3.utils/define-nuxt-component.md +5 -5
- package/3.api/3.utils/define-nuxt-plugin.md +13 -13
- package/3.api/3.utils/define-nuxt-route-middleware.md +5 -5
- package/3.api/3.utils/define-page-meta.md +23 -23
- package/3.api/3.utils/define-route-rules.md +7 -7
- package/3.api/3.utils/navigate-to.md +11 -11
- package/3.api/3.utils/prefetch-components.md +1 -1
- package/3.api/3.utils/preload-components.md +1 -1
- package/3.api/3.utils/preload-route-components.md +2 -2
- package/3.api/3.utils/prerender-routes.md +3 -3
- package/3.api/3.utils/refresh-cookie.md +4 -4
- package/3.api/3.utils/refresh-nuxt-data.md +12 -7
- package/3.api/3.utils/reload-nuxt-app.md +3 -3
- package/3.api/3.utils/set-page-layout.md +1 -1
- package/3.api/3.utils/set-response-status.md +3 -3
- package/3.api/3.utils/show-error.md +5 -5
- package/3.api/3.utils/update-app-config.md +4 -3
- package/3.api/4.commands/add.md +1 -1
- package/3.api/4.commands/analyze.md +2 -1
- package/3.api/4.commands/build.md +2 -1
- package/3.api/4.commands/dev.md +5 -4
- package/3.api/4.commands/generate.md +3 -2
- package/3.api/4.commands/init.md +3 -2
- package/3.api/4.commands/module.md +4 -4
- package/3.api/4.commands/prepare.md +7 -2
- package/3.api/4.commands/preview.md +5 -4
- package/3.api/4.commands/test.md +40 -0
- package/3.api/4.commands/typecheck.md +5 -3
- package/3.api/4.commands/upgrade.md +3 -3
- package/3.api/5.kit/1.modules.md +36 -36
- package/3.api/5.kit/10.runtime-config.md +1 -1
- package/3.api/5.kit/10.templates.md +8 -6
- package/3.api/5.kit/11.nitro.md +62 -62
- package/3.api/5.kit/12.resolving.md +2 -2
- package/3.api/5.kit/14.builder.md +1 -0
- package/3.api/5.kit/15.examples.md +2 -2
- package/3.api/5.kit/16.layers.md +26 -26
- package/3.api/5.kit/3.compatibility.md +14 -14
- package/3.api/5.kit/4.autoimports.md +13 -13
- package/3.api/5.kit/5.components.md +7 -7
- package/3.api/5.kit/6.context.md +3 -3
- package/3.api/5.kit/7.pages.md +7 -7
- package/3.api/5.kit/8.layout.md +2 -2
- package/3.api/5.kit/9.plugins.md +5 -4
- package/3.api/6.advanced/1.hooks.md +3 -3
- package/3.api/6.advanced/2.import-meta.md +3 -3
- package/3.api/6.nuxt-config.md +300 -865
- package/3.api/index.md +7 -7
- package/5.community/2.getting-help.md +1 -1
- package/5.community/3.reporting-bugs.md +1 -1
- package/5.community/4.contribution.md +11 -11
- package/5.community/5.framework-contribution.md +4 -4
- package/5.community/6.roadmap.md +1 -1
- package/6.bridge/1.overview.md +13 -13
- package/6.bridge/10.configuration.md +2 -1
- package/6.bridge/2.typescript.md +3 -3
- package/6.bridge/3.bridge-composition-api.md +8 -8
- package/6.bridge/4.plugins-and-middleware.md +9 -9
- package/6.bridge/5.nuxt3-compatible-api.md +20 -17
- package/6.bridge/6.meta.md +20 -19
- package/6.bridge/7.runtime-config.md +1 -1
- package/6.bridge/8.nitro.md +3 -3
- package/6.bridge/9.vite.md +4 -4
- package/7.migration/1.overview.md +2 -2
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +23 -21
- package/7.migration/20.module-authors.md +7 -7
- package/7.migration/3.auto-imports.md +3 -3
- package/7.migration/4.meta.md +21 -18
- package/7.migration/5.plugins-and-middleware.md +8 -8
- package/7.migration/6.pages-and-layouts.md +28 -24
- package/7.migration/7.component-options.md +18 -18
- package/7.migration/8.runtime-config.md +7 -7
- package/package.json +1 -1
- package/2.guide/2.directory-structure/0.output.md +0 -18
- /package/2.guide/{1.concepts → 2.concepts}/.navigation.yml +0 -0
- /package/2.guide/{1.concepts → 2.concepts}/9.code-style.md +0 -0
|
@@ -4,7 +4,7 @@ description: "Learn how to create a Nuxt Module to integrate, enhance or extend
|
|
|
4
4
|
image: '/socials/module-author-guide.jpg'
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
Nuxt's [configuration](/docs/api/nuxt-config) and [hooks](/docs/guide/going-further/hooks) systems make it possible to customize every aspect of Nuxt and add any integration you might need (Vue plugins, CMS, server routes, components, logging, etc.).
|
|
7
|
+
Nuxt's [configuration](/docs/4.x/api/nuxt-config) and [hooks](/docs/4.x/guide/going-further/hooks) systems make it possible to customize every aspect of Nuxt and add any integration you might need (Vue plugins, CMS, server routes, components, logging, etc.).
|
|
8
8
|
|
|
9
9
|
**Nuxt Modules** are functions that sequentially run when starting Nuxt in development mode using `nuxt dev` or building a project for production with `nuxt build`.
|
|
10
10
|
With modules, you can encapsulate, properly test, and share custom solutions as npm packages without adding unnecessary boilerplate to your project, or requiring changes to Nuxt itself.
|
|
@@ -117,7 +117,7 @@ Nuxt Modules come with a variety of powerful APIs and patterns allowing them to
|
|
|
117
117
|
We can consider two kinds of Nuxt Modules:
|
|
118
118
|
|
|
119
119
|
- published modules are distributed on npm - you can see a list of some community modules on [the Nuxt website](/modules).
|
|
120
|
-
- "local" modules, they exist within a Nuxt project itself, either [inlined in Nuxt config](/docs/api/nuxt-config#modules) or as part of [the `modules` directory](/docs/guide/directory-structure/modules).
|
|
120
|
+
- "local" modules, they exist within a Nuxt project itself, either [inlined in Nuxt config](/docs/4.x/api/nuxt-config#modules) or as part of [the `modules` directory](/docs/4.x/guide/directory-structure/modules).
|
|
121
121
|
|
|
122
122
|
In either case, their anatomy is similar.
|
|
123
123
|
|
|
@@ -136,19 +136,19 @@ export default function (inlineOptions, nuxt) {
|
|
|
136
136
|
// You can do whatever you like here..
|
|
137
137
|
console.log(inlineOptions.token) // `123`
|
|
138
138
|
console.log(nuxt.options.dev) // `true` or `false`
|
|
139
|
-
nuxt.hook('ready',
|
|
139
|
+
nuxt.hook('ready', (nuxt) => {
|
|
140
140
|
console.log('Nuxt is ready')
|
|
141
141
|
})
|
|
142
142
|
}
|
|
143
143
|
```
|
|
144
144
|
|
|
145
|
-
You can get type-hint support for this function using the higher-level `defineNuxtModule` helper provided by [Nuxt Kit](/docs/guide/going-further/kit).
|
|
145
|
+
You can get type-hint support for this function using the higher-level `defineNuxtModule` helper provided by [Nuxt Kit](/docs/4.x/guide/going-further/kit).
|
|
146
146
|
|
|
147
147
|
```ts
|
|
148
148
|
import { defineNuxtModule } from '@nuxt/kit'
|
|
149
149
|
|
|
150
150
|
export default defineNuxtModule((options, nuxt) => {
|
|
151
|
-
nuxt.hook('pages:extend', pages => {
|
|
151
|
+
nuxt.hook('pages:extend', (pages) => {
|
|
152
152
|
console.log(`Discovered ${pages.length} pages`)
|
|
153
153
|
})
|
|
154
154
|
})
|
|
@@ -170,8 +170,8 @@ export default defineNuxtModule({
|
|
|
170
170
|
// Compatibility constraints
|
|
171
171
|
compatibility: {
|
|
172
172
|
// Semver version of supported nuxt versions
|
|
173
|
-
nuxt: '>=3.0.0'
|
|
174
|
-
}
|
|
173
|
+
nuxt: '>=3.0.0',
|
|
174
|
+
},
|
|
175
175
|
},
|
|
176
176
|
// Default configuration options for your module, can also be a function returning those
|
|
177
177
|
defaults: {},
|
|
@@ -191,13 +191,13 @@ export default defineNuxtModule({
|
|
|
191
191
|
overrides: {},
|
|
192
192
|
// Any configuration that should be set. It will override module defaults but
|
|
193
193
|
// will not override any configuration set in `nuxt.options`.
|
|
194
|
-
defaults: {}
|
|
195
|
-
}
|
|
194
|
+
defaults: {},
|
|
195
|
+
},
|
|
196
196
|
},
|
|
197
197
|
// The function holding your module logic, it can be asynchronous
|
|
198
|
-
setup(moduleOptions, nuxt) {
|
|
198
|
+
setup (moduleOptions, nuxt) {
|
|
199
199
|
// ...
|
|
200
|
-
}
|
|
200
|
+
},
|
|
201
201
|
})
|
|
202
202
|
```
|
|
203
203
|
|
|
@@ -224,9 +224,9 @@ Modules, like everything in a Nuxt configuration, aren't included in your applic
|
|
|
224
224
|
Inside the runtime directory, you can provide any kind of assets related to the Nuxt App:
|
|
225
225
|
- Vue components
|
|
226
226
|
- Composables
|
|
227
|
-
- [Nuxt plugins](/docs/guide/directory-structure/plugins)
|
|
227
|
+
- [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins)
|
|
228
228
|
|
|
229
|
-
To the [server engine](/docs/guide/concepts/server-engine), Nitro:
|
|
229
|
+
To the [server engine](/docs/4.x/guide/concepts/server-engine), Nitro:
|
|
230
230
|
- API routes
|
|
231
231
|
- Middlewares
|
|
232
232
|
- Nitro plugins
|
|
@@ -237,10 +237,10 @@ Or any other kind of asset you want to inject in users' Nuxt applications:
|
|
|
237
237
|
- Images
|
|
238
238
|
- etc.
|
|
239
239
|
|
|
240
|
-
You'll then be able to inject all those assets inside the application from your [module definition](#module-definition).
|
|
240
|
+
You'll then be able to inject all those assets inside the application from your [module definition](/docs/4.x/guide/going-further/modules#module-definition).
|
|
241
241
|
|
|
242
242
|
::tip
|
|
243
|
-
Learn more about asset injection in [the recipes section](#recipes).
|
|
243
|
+
Learn more about asset injection in [the recipes section](/docs/4.x/guide/going-further/modules#recipes).
|
|
244
244
|
::
|
|
245
245
|
|
|
246
246
|
::warning
|
|
@@ -259,13 +259,13 @@ Modules come with a set of first-party tools to help you with their development.
|
|
|
259
259
|
|
|
260
260
|
#### `@nuxt/kit`
|
|
261
261
|
|
|
262
|
-
[Nuxt Kit](/docs/guide/going-further/kit) provides composable utilities to help your module interact with Nuxt applications. It's recommended to use Nuxt Kit utilities over manual alternatives whenever possible to ensure better compatibility and code readability of your module.
|
|
262
|
+
[Nuxt Kit](/docs/4.x/guide/going-further/kit) provides composable utilities to help your module interact with Nuxt applications. It's recommended to use Nuxt Kit utilities over manual alternatives whenever possible to ensure better compatibility and code readability of your module.
|
|
263
263
|
|
|
264
|
-
:read-more{to="/docs/guide/going-further/kit"}
|
|
264
|
+
:read-more{to="/docs/4.x/guide/going-further/kit"}
|
|
265
265
|
|
|
266
266
|
#### `@nuxt/test-utils`
|
|
267
267
|
|
|
268
|
-
[Nuxt Test Utils](/docs/getting-started/testing) is a collection of utilities to help set up and run Nuxt applications within your module tests.
|
|
268
|
+
[Nuxt Test Utils](/docs/4.x/getting-started/testing) is a collection of utilities to help set up and run Nuxt applications within your module tests.
|
|
269
269
|
|
|
270
270
|
### Recipes
|
|
271
271
|
|
|
@@ -283,7 +283,7 @@ export default defineNuxtModule({
|
|
|
283
283
|
// We create the `experimental` object if it doesn't exist yet
|
|
284
284
|
nuxt.options.experimental ||= {}
|
|
285
285
|
nuxt.options.experimental.componentIslands = true
|
|
286
|
-
}
|
|
286
|
+
},
|
|
287
287
|
})
|
|
288
288
|
```
|
|
289
289
|
|
|
@@ -295,7 +295,7 @@ Watch Vue School video about altering Nuxt configuration.
|
|
|
295
295
|
|
|
296
296
|
#### Exposing Options to Runtime
|
|
297
297
|
|
|
298
|
-
Because modules aren't part of the application runtime, their options aren't either. However, in many cases, you might need access to some of these module options within your runtime code. We recommend exposing the needed config using Nuxt's [`runtimeConfig`](/docs/api/nuxt-config#runtimeconfig).
|
|
298
|
+
Because modules aren't part of the application runtime, their options aren't either. However, in many cases, you might need access to some of these module options within your runtime code. We recommend exposing the needed config using Nuxt's [`runtimeConfig`](/docs/4.x/api/nuxt-config#runtimeconfig).
|
|
299
299
|
|
|
300
300
|
<!-- TODO: Update after #18466 (or equivalent) -->
|
|
301
301
|
|
|
@@ -306,9 +306,9 @@ import { defu } from 'defu'
|
|
|
306
306
|
export default defineNuxtModule({
|
|
307
307
|
setup (options, nuxt) {
|
|
308
308
|
nuxt.options.runtimeConfig.public.myModule = defu(nuxt.options.runtimeConfig.public.myModule, {
|
|
309
|
-
foo: options.foo
|
|
309
|
+
foo: options.foo,
|
|
310
310
|
})
|
|
311
|
-
}
|
|
311
|
+
},
|
|
312
312
|
})
|
|
313
313
|
```
|
|
314
314
|
|
|
@@ -317,6 +317,8 @@ Note that we use [`defu`](https://github.com/unjs/defu) to extend the public run
|
|
|
317
317
|
You can then access your module options in a plugin, component, the application like any other runtime configuration:
|
|
318
318
|
|
|
319
319
|
```js
|
|
320
|
+
import { useRuntimeConfig } from '@nuxt/kit'
|
|
321
|
+
|
|
320
322
|
const options = useRuntimeConfig().public.myModule
|
|
321
323
|
```
|
|
322
324
|
|
|
@@ -324,7 +326,7 @@ const options = useRuntimeConfig().public.myModule
|
|
|
324
326
|
Be careful not to expose any sensitive module configuration on the public runtime config, such as private API keys, as they will end up in the public bundle.
|
|
325
327
|
::
|
|
326
328
|
|
|
327
|
-
:read-more{to="/docs/guide/going-further/runtime-config"}
|
|
329
|
+
:read-more{to="/docs/4.x/guide/going-further/runtime-config"}
|
|
328
330
|
|
|
329
331
|
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/passing-and-exposing-module-options?friend=nuxt" target="_blank"}
|
|
330
332
|
Watch Vue School video about passing and exposing Nuxt module options.
|
|
@@ -335,7 +337,7 @@ Watch Vue School video about passing and exposing Nuxt module options.
|
|
|
335
337
|
Plugins are a common way for a module to add runtime logic. You can use the `addPlugin` utility to register them from your module.
|
|
336
338
|
|
|
337
339
|
```js
|
|
338
|
-
import {
|
|
340
|
+
import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
339
341
|
|
|
340
342
|
export default defineNuxtModule({
|
|
341
343
|
setup (options, nuxt) {
|
|
@@ -343,53 +345,53 @@ export default defineNuxtModule({
|
|
|
343
345
|
const resolver = createResolver(import.meta.url)
|
|
344
346
|
|
|
345
347
|
addPlugin(resolver.resolve('./runtime/plugin'))
|
|
346
|
-
}
|
|
348
|
+
},
|
|
347
349
|
})
|
|
348
350
|
```
|
|
349
351
|
|
|
350
|
-
:read-more{to="/docs/guide/going-further/kit"}
|
|
352
|
+
:read-more{to="/docs/4.x/guide/going-further/kit"}
|
|
351
353
|
|
|
352
354
|
#### Injecting Vue Components With `addComponent`
|
|
353
355
|
|
|
354
356
|
If your module should provide Vue components, you can use the `addComponent` utility to add them as auto-imports for Nuxt to resolve.
|
|
355
357
|
|
|
356
|
-
```
|
|
357
|
-
import { defineNuxtModule,
|
|
358
|
+
```ts twoslash
|
|
359
|
+
import { addComponent, createResolver, defineNuxtModule, useRuntimeConfig } from '@nuxt/kit'
|
|
358
360
|
|
|
359
361
|
export default defineNuxtModule({
|
|
360
|
-
setup(options, nuxt) {
|
|
362
|
+
setup (options, nuxt) {
|
|
361
363
|
const resolver = createResolver(import.meta.url)
|
|
362
364
|
|
|
363
365
|
// From the runtime directory
|
|
364
366
|
addComponent({
|
|
365
367
|
name: 'MySuperComponent', // name of the component to be used in vue templates
|
|
366
368
|
export: 'MySuperComponent', // (optional) if the component is a named (rather than default) export
|
|
367
|
-
filePath: resolver.resolve('runtime/components/MySuperComponent.vue')
|
|
369
|
+
filePath: resolver.resolve('runtime/components/MySuperComponent.vue'),
|
|
368
370
|
})
|
|
369
371
|
|
|
370
372
|
// From a library
|
|
371
373
|
addComponent({
|
|
372
374
|
name: 'MyAwesomeComponent', // name of the component to be used in vue templates
|
|
373
375
|
export: 'MyAwesomeComponent', // (optional) if the component is a named (rather than default) export
|
|
374
|
-
filePath: '@vue/awesome-components'
|
|
376
|
+
filePath: '@vue/awesome-components',
|
|
375
377
|
})
|
|
376
|
-
}
|
|
378
|
+
},
|
|
377
379
|
})
|
|
378
380
|
```
|
|
379
381
|
|
|
380
382
|
Alternatively, you can add an entire directory by using `addComponentsDir`.
|
|
381
383
|
|
|
382
384
|
```ts
|
|
383
|
-
import {
|
|
385
|
+
import { addComponentsDir, defineNuxtModule } from '@nuxt/kit'
|
|
384
386
|
|
|
385
387
|
export default defineNuxtModule({
|
|
386
|
-
setup(options, nuxt) {
|
|
388
|
+
setup (options, nuxt) {
|
|
387
389
|
const resolver = createResolver(import.meta.url)
|
|
388
390
|
|
|
389
391
|
addComponentsDir({
|
|
390
|
-
path: resolver.resolve('runtime/components')
|
|
392
|
+
path: resolver.resolve('runtime/components'),
|
|
391
393
|
})
|
|
392
|
-
}
|
|
394
|
+
},
|
|
393
395
|
})
|
|
394
396
|
```
|
|
395
397
|
|
|
@@ -398,66 +400,66 @@ export default defineNuxtModule({
|
|
|
398
400
|
If your module should provide composables, you can use the `addImports` utility to add them as auto-imports for Nuxt to resolve.
|
|
399
401
|
|
|
400
402
|
```ts
|
|
401
|
-
import {
|
|
403
|
+
import { addImports, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
402
404
|
|
|
403
405
|
export default defineNuxtModule({
|
|
404
|
-
setup(options, nuxt) {
|
|
406
|
+
setup (options, nuxt) {
|
|
405
407
|
const resolver = createResolver(import.meta.url)
|
|
406
408
|
|
|
407
409
|
addImports({
|
|
408
410
|
name: 'useComposable', // name of the composable to be used
|
|
409
411
|
as: 'useComposable',
|
|
410
|
-
from: resolver.resolve('runtime/composables/useComposable') // path of composable
|
|
412
|
+
from: resolver.resolve('runtime/composables/useComposable'), // path of composable
|
|
411
413
|
})
|
|
412
|
-
}
|
|
414
|
+
},
|
|
413
415
|
})
|
|
414
416
|
```
|
|
415
417
|
|
|
416
418
|
Alternatively, you can add an entire directory by using `addImportsDir`.
|
|
417
419
|
|
|
418
420
|
```ts
|
|
419
|
-
import {
|
|
421
|
+
import { addImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
420
422
|
|
|
421
423
|
export default defineNuxtModule({
|
|
422
|
-
setup(options, nuxt) {
|
|
424
|
+
setup (options, nuxt) {
|
|
423
425
|
const resolver = createResolver(import.meta.url)
|
|
424
426
|
|
|
425
427
|
addImportsDir(resolver.resolve('runtime/composables'))
|
|
426
|
-
}
|
|
428
|
+
},
|
|
427
429
|
})
|
|
428
430
|
```
|
|
429
431
|
|
|
430
432
|
#### Injecting Server Routes With `addServerHandler`
|
|
431
433
|
|
|
432
434
|
```ts
|
|
433
|
-
import {
|
|
435
|
+
import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
434
436
|
|
|
435
437
|
export default defineNuxtModule({
|
|
436
|
-
setup(options, nuxt) {
|
|
438
|
+
setup (options, nuxt) {
|
|
437
439
|
const resolver = createResolver(import.meta.url)
|
|
438
440
|
|
|
439
441
|
addServerHandler({
|
|
440
442
|
route: '/api/hello',
|
|
441
|
-
handler: resolver.resolve('./runtime/server/api/hello/index.get')
|
|
443
|
+
handler: resolver.resolve('./runtime/server/api/hello/index.get'),
|
|
442
444
|
})
|
|
443
|
-
}
|
|
445
|
+
},
|
|
444
446
|
})
|
|
445
447
|
```
|
|
446
448
|
|
|
447
449
|
You can also add a dynamic server route:
|
|
448
450
|
|
|
449
451
|
```ts
|
|
450
|
-
import {
|
|
452
|
+
import { addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
451
453
|
|
|
452
454
|
export default defineNuxtModule({
|
|
453
|
-
setup(options, nuxt) {
|
|
455
|
+
setup (options, nuxt) {
|
|
454
456
|
const resolver = createResolver(import.meta.url)
|
|
455
457
|
|
|
456
458
|
addServerHandler({
|
|
457
459
|
route: '/api/hello/:name',
|
|
458
|
-
handler: resolver.resolve('./runtime/server/api/hello/[name].get')
|
|
460
|
+
handler: resolver.resolve('./runtime/server/api/hello/[name].get'),
|
|
459
461
|
})
|
|
460
|
-
}
|
|
462
|
+
},
|
|
461
463
|
})
|
|
462
464
|
```
|
|
463
465
|
|
|
@@ -466,34 +468,34 @@ export default defineNuxtModule({
|
|
|
466
468
|
If your module should provide other kinds of assets, they can also be injected. Here's a simple example module injecting a stylesheet through Nuxt's `css` array.
|
|
467
469
|
|
|
468
470
|
```js
|
|
469
|
-
import {
|
|
471
|
+
import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
470
472
|
|
|
471
473
|
export default defineNuxtModule({
|
|
472
474
|
setup (options, nuxt) {
|
|
473
475
|
const resolver = createResolver(import.meta.url)
|
|
474
476
|
|
|
475
477
|
nuxt.options.css.push(resolver.resolve('./runtime/style.css'))
|
|
476
|
-
}
|
|
478
|
+
},
|
|
477
479
|
})
|
|
478
480
|
```
|
|
479
481
|
|
|
480
|
-
And a more advanced one, exposing a folder of assets through [Nitro](/docs/guide/concepts/server-engine)'s `publicAssets` option:
|
|
482
|
+
And a more advanced one, exposing a folder of assets through [Nitro](/docs/4.x/guide/concepts/server-engine)'s `publicAssets` option:
|
|
481
483
|
|
|
482
484
|
```js
|
|
483
|
-
import {
|
|
485
|
+
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
484
486
|
|
|
485
487
|
export default defineNuxtModule({
|
|
486
488
|
setup (options, nuxt) {
|
|
487
489
|
const resolver = createResolver(import.meta.url)
|
|
488
490
|
|
|
489
|
-
nuxt.hook('nitro:config',
|
|
491
|
+
nuxt.hook('nitro:config', (nitroConfig) => {
|
|
490
492
|
nitroConfig.publicAssets ||= []
|
|
491
493
|
nitroConfig.publicAssets.push({
|
|
492
494
|
dir: resolver.resolve('./runtime/public'),
|
|
493
|
-
maxAge: 60 * 60 * 24 * 365 // 1 year
|
|
495
|
+
maxAge: 60 * 60 * 24 * 365, // 1 year
|
|
494
496
|
})
|
|
495
497
|
})
|
|
496
|
-
}
|
|
498
|
+
},
|
|
497
499
|
})
|
|
498
500
|
```
|
|
499
501
|
|
|
@@ -502,7 +504,7 @@ export default defineNuxtModule({
|
|
|
502
504
|
If your module depends on other modules, you can add them by using Nuxt Kit's `installModule` utility. For example, if you wanted to use Nuxt Tailwind in your module, you could add it as below:
|
|
503
505
|
|
|
504
506
|
```ts
|
|
505
|
-
import {
|
|
507
|
+
import { createResolver, defineNuxtModule, installModule } from '@nuxt/kit'
|
|
506
508
|
|
|
507
509
|
export default defineNuxtModule<ModuleOptions>({
|
|
508
510
|
async setup (options, nuxt) {
|
|
@@ -519,39 +521,39 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
519
521
|
content: {
|
|
520
522
|
files: [
|
|
521
523
|
resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'),
|
|
522
|
-
resolver.resolve('./runtime/*.{mjs,js,ts}')
|
|
523
|
-
]
|
|
524
|
-
}
|
|
525
|
-
}
|
|
524
|
+
resolver.resolve('./runtime/*.{mjs,js,ts}'),
|
|
525
|
+
],
|
|
526
|
+
},
|
|
527
|
+
},
|
|
526
528
|
})
|
|
527
|
-
}
|
|
529
|
+
},
|
|
528
530
|
})
|
|
529
531
|
```
|
|
530
532
|
|
|
531
533
|
#### Using Hooks
|
|
532
534
|
|
|
533
|
-
[Lifecycle hooks](/docs/guide/going-further/hooks) allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through the `hooks` map in their definition.
|
|
535
|
+
[Lifecycle hooks](/docs/4.x/guide/going-further/hooks) allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through the `hooks` map in their definition.
|
|
534
536
|
|
|
535
537
|
```js
|
|
536
|
-
import {
|
|
538
|
+
import { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
537
539
|
|
|
538
540
|
export default defineNuxtModule({
|
|
539
541
|
// Hook to the `app:error` hook through the `hooks` map
|
|
540
542
|
hooks: {
|
|
541
543
|
'app:error': (err) => {
|
|
542
|
-
console.info(`This error happened: ${err}`)
|
|
543
|
-
}
|
|
544
|
+
console.info(`This error happened: ${err}`)
|
|
545
|
+
},
|
|
544
546
|
},
|
|
545
547
|
setup (options, nuxt) {
|
|
546
548
|
// Programmatically hook to the `pages:extend` hook
|
|
547
549
|
nuxt.hook('pages:extend', (pages) => {
|
|
548
|
-
console.info(`Discovered ${pages.length} pages`)
|
|
550
|
+
console.info(`Discovered ${pages.length} pages`)
|
|
549
551
|
})
|
|
550
|
-
}
|
|
552
|
+
},
|
|
551
553
|
})
|
|
552
554
|
```
|
|
553
555
|
|
|
554
|
-
:read-more{to="/docs/api/advanced/hooks"}
|
|
556
|
+
:read-more{to="/docs/4.x/api/advanced/hooks"}
|
|
555
557
|
|
|
556
558
|
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/nuxt-lifecycle-hooks?friend=nuxt" target="_blank"}
|
|
557
559
|
Watch Vue School video about using Nuxt lifecycle hooks in modules.
|
|
@@ -568,10 +570,10 @@ import { defineNuxtModule } from '@nuxt/kit'
|
|
|
568
570
|
|
|
569
571
|
export default defineNuxtModule({
|
|
570
572
|
setup (options, nuxt) {
|
|
571
|
-
nuxt.hook('close', async nuxt => {
|
|
573
|
+
nuxt.hook('close', async (nuxt) => {
|
|
572
574
|
// Your custom code here
|
|
573
575
|
})
|
|
574
|
-
}
|
|
576
|
+
},
|
|
575
577
|
})
|
|
576
578
|
```
|
|
577
579
|
::
|
|
@@ -597,7 +599,7 @@ export default defineNuxtModule({
|
|
|
597
599
|
const payload = { foo: 'bar' }
|
|
598
600
|
await nuxt.callHook('my-module:custom-hook', payload)
|
|
599
601
|
})
|
|
600
|
-
}
|
|
602
|
+
},
|
|
601
603
|
})
|
|
602
604
|
```
|
|
603
605
|
|
|
@@ -606,32 +608,32 @@ export default defineNuxtModule({
|
|
|
606
608
|
If you need to add a virtual file that can be imported into the user's app, you can use the `addTemplate` utility.
|
|
607
609
|
|
|
608
610
|
```ts
|
|
609
|
-
import {
|
|
611
|
+
import { addTemplate, defineNuxtModule } from '@nuxt/kit'
|
|
610
612
|
|
|
611
613
|
export default defineNuxtModule({
|
|
612
614
|
setup (options, nuxt) {
|
|
613
615
|
// The file is added to Nuxt's internal virtual file system and can be imported from '#build/my-module-feature.mjs'
|
|
614
616
|
addTemplate({
|
|
615
617
|
filename: 'my-module-feature.mjs',
|
|
616
|
-
getContents: () => 'export const myModuleFeature = () => "hello world !"'
|
|
618
|
+
getContents: () => 'export const myModuleFeature = () => "hello world !"',
|
|
617
619
|
})
|
|
618
|
-
}
|
|
620
|
+
},
|
|
619
621
|
})
|
|
620
622
|
```
|
|
621
623
|
|
|
622
624
|
For the server, you should use the `addServerTemplate` utility instead.
|
|
623
625
|
|
|
624
626
|
```ts
|
|
625
|
-
import {
|
|
627
|
+
import { addServerTemplate, defineNuxtModule } from '@nuxt/kit'
|
|
626
628
|
|
|
627
629
|
export default defineNuxtModule({
|
|
628
630
|
setup (options, nuxt) {
|
|
629
631
|
// The file is added to Nitro's virtual file system and can be imported in the server code from 'my-server-module.mjs'
|
|
630
632
|
addServerTemplate({
|
|
631
633
|
filename: 'my-server-module.mjs',
|
|
632
|
-
getContents: () => 'export const myServerModule = () => "hello world !"'
|
|
634
|
+
getContents: () => 'export const myServerModule = () => "hello world !"',
|
|
633
635
|
})
|
|
634
|
-
}
|
|
636
|
+
},
|
|
635
637
|
})
|
|
636
638
|
```
|
|
637
639
|
|
|
@@ -644,7 +646,7 @@ writes a template to the disk and adds a reference to it in the generated `nuxt.
|
|
|
644
646
|
If your module should augment types handled by Nuxt, you can use `addTypeTemplate` to perform this operation:
|
|
645
647
|
|
|
646
648
|
```js
|
|
647
|
-
import {
|
|
649
|
+
import { addTemplate, addTypeTemplate, defineNuxtModule } from '@nuxt/kit'
|
|
648
650
|
|
|
649
651
|
export default defineNuxtModule({
|
|
650
652
|
setup (options, nuxt) {
|
|
@@ -658,9 +660,9 @@ export default defineNuxtModule({
|
|
|
658
660
|
interface NitroRouteRules extends MyModuleNitroRules {}
|
|
659
661
|
interface NitroRouteConfig extends MyModuleNitroRules {}
|
|
660
662
|
}
|
|
661
|
-
export {}
|
|
663
|
+
export {}`,
|
|
662
664
|
})
|
|
663
|
-
}
|
|
665
|
+
},
|
|
664
666
|
})
|
|
665
667
|
```
|
|
666
668
|
|
|
@@ -678,7 +680,7 @@ nuxt.hook('prepare:types', ({ references }) => {
|
|
|
678
680
|
If you need to update your templates/virtual files, you can leverage the `updateTemplates` utility like this :
|
|
679
681
|
|
|
680
682
|
```ts
|
|
681
|
-
nuxt.hook('builder:watch',
|
|
683
|
+
nuxt.hook('builder:watch', (event, path) => {
|
|
682
684
|
if (path.includes('my-module-feature.config')) {
|
|
683
685
|
// This will reload the template that you registered
|
|
684
686
|
updateTemplates({ filter: t => t.filename === 'my-module-feature.mjs' })
|
|
@@ -700,7 +702,7 @@ We're still discussing and exploring how to ease unit and integration testing on
|
|
|
700
702
|
|
|
701
703
|
#### End to End
|
|
702
704
|
|
|
703
|
-
[Nuxt Test Utils](/docs/getting-started/testing) is the go-to library to help you test your module in an end-to-end way. Here's the workflow to adopt with it:
|
|
705
|
+
[Nuxt Test Utils](/docs/4.x/getting-started/testing) is the go-to library to help you test your module in an end-to-end way. Here's the workflow to adopt with it:
|
|
704
706
|
|
|
705
707
|
1. Create a Nuxt application to be used as a "fixture" inside `test/fixtures/*`
|
|
706
708
|
2. Setup Nuxt with this fixture inside your test file
|
|
@@ -710,24 +712,24 @@ We're still discussing and exploring how to ease unit and integration testing on
|
|
|
710
712
|
|
|
711
713
|
In practice, the fixture:
|
|
712
714
|
|
|
713
|
-
```
|
|
715
|
+
```ts [test/fixtures/ssr/nuxt.config.ts]
|
|
714
716
|
// 1. Create a Nuxt application to be used as a "fixture"
|
|
715
717
|
import MyModule from '../../../src/module'
|
|
716
718
|
|
|
717
719
|
export default defineNuxtConfig({
|
|
718
720
|
ssr: true,
|
|
719
721
|
modules: [
|
|
720
|
-
MyModule
|
|
721
|
-
]
|
|
722
|
+
MyModule,
|
|
723
|
+
],
|
|
722
724
|
})
|
|
723
725
|
```
|
|
724
726
|
|
|
725
727
|
And its test:
|
|
726
728
|
|
|
727
|
-
```
|
|
728
|
-
import { describe,
|
|
729
|
+
```ts [test/rendering.ts]
|
|
730
|
+
import { describe, expect, it } from 'vitest'
|
|
729
731
|
import { fileURLToPath } from 'node:url'
|
|
730
|
-
import {
|
|
732
|
+
import { $fetch, setup } from '@nuxt/test-utils/e2e'
|
|
731
733
|
|
|
732
734
|
describe('ssr', async () => {
|
|
733
735
|
// 2. Setup Nuxt with this fixture inside your test file
|
|
@@ -754,7 +756,7 @@ An example of such a workflow is available on [the module starter](https://githu
|
|
|
754
756
|
|
|
755
757
|
#### Manual QA With Playground and Externally
|
|
756
758
|
|
|
757
|
-
Having a playground Nuxt application to test your module when developing it is really useful. [The module starter integrates one for that purpose](#how-to-develop).
|
|
759
|
+
Having a playground Nuxt application to test your module when developing it is really useful. [The module starter integrates one for that purpose](/docs/4.x/guide/going-further/modules#how-to-develop).
|
|
758
760
|
|
|
759
761
|
You can test your module with other Nuxt applications (applications that are not part of your module repository) locally. To do so, you can use [`npm pack`](https://docs.npmjs.com/cli/commands/npm-pack) command, or your package manager equivalent, to create a tarball from your module. Then in your test project, you can add your module to `package.json` packages as: `"my-module": "file:/path/to/tarball.tgz"`.
|
|
760
762
|
|
|
@@ -810,7 +812,7 @@ export default defineNuxtModule({
|
|
|
810
812
|
})
|
|
811
813
|
```
|
|
812
814
|
|
|
813
|
-
This pattern prevents unnecessary work on every build and provides a better developer experience. See the [lifecycle hooks documentation](/docs/api/kit/modules#using-lifecycle-hooks-for-module-installation-and-upgrade) for more details.
|
|
815
|
+
This pattern prevents unnecessary work on every build and provides a better developer experience. See the [lifecycle hooks documentation](/docs/4.x/api/kit/modules#using-lifecycle-hooks-for-module-installation-and-upgrade) for more details.
|
|
814
816
|
|
|
815
817
|
#### Be TypeScript Friendly
|
|
816
818
|
|
|
@@ -820,7 +822,7 @@ Exposing types and using TypeScript to develop modules benefits users even when
|
|
|
820
822
|
|
|
821
823
|
#### Avoid CommonJS Syntax
|
|
822
824
|
|
|
823
|
-
Nuxt relies on native ESM. Please read [Native ES Modules](/docs/guide/concepts/esm) for more information.
|
|
825
|
+
Nuxt relies on native ESM. Please read [Native ES Modules](/docs/4.x/guide/concepts/esm) for more information.
|
|
824
826
|
|
|
825
827
|
#### Document Module Usage
|
|
826
828
|
|
|
@@ -3,9 +3,9 @@ title: "Nuxt Kit"
|
|
|
3
3
|
description: "@nuxt/kit provides features for module authors."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Nuxt Kit provides composable utilities to make interacting with [Nuxt Hooks](/docs/api/advanced/hooks), the [Nuxt Interface](/docs/guide/going-further/internals#the-nuxt-interface) and developing [Nuxt Modules](/docs/guide/going-further/modules) super easy.
|
|
6
|
+
Nuxt Kit provides composable utilities to make interacting with [Nuxt Hooks](/docs/4.x/api/advanced/hooks), the [Nuxt Interface](/docs/4.x/guide/going-further/internals#the-nuxt-interface) and developing [Nuxt Modules](/docs/4.x/guide/going-further/modules) super easy.
|
|
7
7
|
|
|
8
|
-
::read-more{to="/docs/api/kit"}
|
|
8
|
+
::read-more{to="/docs/4.x/api/kit"}
|
|
9
9
|
Discover all Nuxt Kit utilities.
|
|
10
10
|
::
|
|
11
11
|
|
|
@@ -29,22 +29,22 @@ You can install the latest Nuxt Kit by adding it to the `dependencies` section o
|
|
|
29
29
|
|
|
30
30
|
### Import Kit Utilities
|
|
31
31
|
|
|
32
|
-
```
|
|
32
|
+
```ts [test.mjs]
|
|
33
33
|
import { useNuxt } from '@nuxt/kit'
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
:read-more{to="/docs/api/kit"}
|
|
36
|
+
:read-more{to="/docs/4.x/api/kit"}
|
|
37
37
|
|
|
38
38
|
::note
|
|
39
39
|
Nuxt Kit utilities are only available for modules and not meant to be imported in runtime (components, Vue composables, pages, plugins, or server routes).
|
|
40
40
|
::
|
|
41
41
|
|
|
42
|
-
Nuxt Kit is an [esm-only package](/docs/guide/concepts/esm) meaning that you **cannot** `require('@nuxt/kit')`. As a workaround, use dynamic import in the CommonJS context:
|
|
42
|
+
Nuxt Kit is an [esm-only package](/docs/4.x/guide/concepts/esm) meaning that you **cannot** `require('@nuxt/kit')`. As a workaround, use dynamic import in the CommonJS context:
|
|
43
43
|
|
|
44
|
-
```
|
|
44
|
+
```ts [test.cjs]
|
|
45
45
|
// This does NOT work!
|
|
46
46
|
// const kit = require('@nuxt/kit')
|
|
47
|
-
async function main() {
|
|
47
|
+
async function main () {
|
|
48
48
|
const kit = await import('@nuxt/kit')
|
|
49
49
|
}
|
|
50
50
|
main()
|
|
@@ -15,7 +15,7 @@ In Nuxt 2, this was referred to as **Nuxt context**.
|
|
|
15
15
|
|
|
16
16
|
## Nuxt App Interface
|
|
17
17
|
|
|
18
|
-
::read-more{to="/docs/guide/going-further/internals#the-nuxtapp-interface"}
|
|
18
|
+
::read-more{to="/docs/4.x/guide/going-further/internals#the-nuxtapp-interface"}
|
|
19
19
|
Jump over the `NuxtApp` interface documentation.
|
|
20
20
|
::
|
|
21
21
|
|
|
@@ -23,13 +23,13 @@ Jump over the `NuxtApp` interface documentation.
|
|
|
23
23
|
|
|
24
24
|
Many composables and utilities, both built-in and user-made, may require access to the Nuxt instance. This doesn't exist everywhere on your application, because a fresh instance is created on every request.
|
|
25
25
|
|
|
26
|
-
Currently, the Nuxt context is only accessible in [plugins](/docs/guide/directory-structure/plugins), [Nuxt hooks](/docs/guide/going-further/hooks), [Nuxt middleware](/docs/guide/directory-structure/app/middleware) (if wrapped in `defineNuxtRouteMiddleware`), and [setup functions](https://vuejs.org/api/composition-api-setup.html) (in pages and components).
|
|
26
|
+
Currently, the Nuxt context is only accessible in [plugins](/docs/4.x/guide/directory-structure/plugins), [Nuxt hooks](/docs/4.x/guide/going-further/hooks), [Nuxt middleware](/docs/4.x/guide/directory-structure/app/middleware) (if wrapped in `defineNuxtRouteMiddleware`), and [setup functions](https://vuejs.org/api/composition-api-setup.html) (in pages and components).
|
|
27
27
|
|
|
28
|
-
If a composable is called without access to the context, you may get an error stating that 'A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function.' In that case, you can also explicitly call functions within this context by using [`nuxtApp.runWithContext`](/docs/api/composables/use-nuxt-app#runwithcontext).
|
|
28
|
+
If a composable is called without access to the context, you may get an error stating that 'A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function.' In that case, you can also explicitly call functions within this context by using [`nuxtApp.runWithContext`](/docs/4.x/api/composables/use-nuxt-app#runwithcontext).
|
|
29
29
|
|
|
30
30
|
## Accessing NuxtApp
|
|
31
31
|
|
|
32
|
-
Within composables, plugins and components you can access `nuxtApp` with [`useNuxtApp()`](/docs/api/composables/use-nuxt-app):
|
|
32
|
+
Within composables, plugins and components you can access `nuxtApp` with [`useNuxtApp()`](/docs/4.x/api/composables/use-nuxt-app):
|
|
33
33
|
|
|
34
34
|
```ts [app/composables/useMyComposable.ts]
|
|
35
35
|
export function useMyComposable () {
|
|
@@ -38,11 +38,11 @@ export function useMyComposable () {
|
|
|
38
38
|
}
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
If your composable does not always need `nuxtApp` or you simply want to check if it is present or not, since [`useNuxtApp`](/docs/api/composables/use-nuxt-app) throws an exception, you can use [`tryUseNuxtApp`](/docs/api/composables/use-nuxt-app#tryusenuxtapp) instead.
|
|
41
|
+
If your composable does not always need `nuxtApp` or you simply want to check if it is present or not, since [`useNuxtApp`](/docs/4.x/api/composables/use-nuxt-app) throws an exception, you can use [`tryUseNuxtApp`](/docs/4.x/api/composables/use-nuxt-app#tryusenuxtapp) instead.
|
|
42
42
|
|
|
43
43
|
Plugins also receive `nuxtApp` as the first argument for convenience.
|
|
44
44
|
|
|
45
|
-
:read-more{to="/docs/guide/directory-structure/plugins"}
|
|
45
|
+
:read-more{to="/docs/4.x/guide/directory-structure/plugins"}
|
|
46
46
|
|
|
47
47
|
## Providing Helpers
|
|
48
48
|
|
|
@@ -50,12 +50,12 @@ You can provide helpers to be usable across all composables and application. Thi
|
|
|
50
50
|
|
|
51
51
|
```ts
|
|
52
52
|
const nuxtApp = useNuxtApp()
|
|
53
|
-
nuxtApp.provide('hello',
|
|
53
|
+
nuxtApp.provide('hello', name => `Hello ${name}!`)
|
|
54
54
|
|
|
55
55
|
console.log(nuxtApp.$hello('name')) // Prints "Hello name!"
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
::read-more{to="/docs/guide/directory-structure/plugins#providing-helpers"}
|
|
58
|
+
::read-more{to="/docs/4.x/guide/directory-structure/plugins#providing-helpers"}
|
|
59
59
|
It is possible to inject helpers by returning an object with a `provide` key in plugins.
|
|
60
60
|
::
|
|
61
61
|
|