@nuxt/docs-nightly 4.1.0-29279382.7234ae61 → 4.1.1-29281581.418bafeb
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.
|
@@ -27,6 +27,10 @@ modules[]=nuxt-security
|
|
|
27
27
|
|
|
28
28
|
If present, the properties in the `nuxt.config` file will overwrite the properties in `.nuxtrc` file.
|
|
29
29
|
|
|
30
|
+
::note
|
|
31
|
+
Nuxt automatically adds a `setups` section to track module installation and upgrade state. This is used internally for [module lifecycle hooks](/docs/api/kit/modules#using-lifecycle-hooks-for-module-installation-and-upgrade) and should not be modified manually.
|
|
32
|
+
::
|
|
33
|
+
|
|
30
34
|
::read-more{to="/docs/api/configuration/nuxt-config"}
|
|
31
35
|
Discover all the available options in the **Nuxt configuration** documentation.
|
|
32
36
|
::
|
|
@@ -780,6 +780,38 @@ Nuxt Modules should provide an explicit prefix for any exposed configuration, pl
|
|
|
780
780
|
|
|
781
781
|
Ideally, you should prefix them with your module's name (e.g. if your module is called `nuxt-foo`, expose `<FooButton>` and `useFooBar()` and **not** `<Button>` and `useBar()`).
|
|
782
782
|
|
|
783
|
+
#### Use Lifecycle Hooks for One-Time Setup
|
|
784
|
+
|
|
785
|
+
When your module needs to perform one-time setup tasks (like generating configuration files, setting up databases, or installing dependencies), use lifecycle hooks instead of running the logic in your main `setup` function.
|
|
786
|
+
|
|
787
|
+
```ts
|
|
788
|
+
import { addServerHandler, defineNuxtModule } from 'nuxt/kit'
|
|
789
|
+
import semver from 'semver'
|
|
790
|
+
|
|
791
|
+
export default defineNuxtModule({
|
|
792
|
+
meta: {
|
|
793
|
+
name: 'my-database-module',
|
|
794
|
+
version: '1.0.0',
|
|
795
|
+
},
|
|
796
|
+
async onInstall (nuxt) {
|
|
797
|
+
// One-time setup: create database schema, generate config files, etc.
|
|
798
|
+
await generateDatabaseConfig(nuxt.options.rootDir)
|
|
799
|
+
},
|
|
800
|
+
async onUpgrade (options, nuxt, previousVersion) {
|
|
801
|
+
// Handle version-specific migrations
|
|
802
|
+
if (semver.lt(previousVersion, '1.0.0')) {
|
|
803
|
+
await migrateLegacyData()
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
setup (options, nuxt) {
|
|
807
|
+
// Regular setup logic that runs on every build
|
|
808
|
+
addServerHandler({ /* ... */ })
|
|
809
|
+
},
|
|
810
|
+
})
|
|
811
|
+
```
|
|
812
|
+
|
|
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.
|
|
814
|
+
|
|
783
815
|
#### Be TypeScript Friendly
|
|
784
816
|
|
|
785
817
|
Nuxt has first-class TypeScript integration for the best developer experience.
|
package/3.api/5.kit/1.modules.md
CHANGED
|
@@ -62,6 +62,8 @@ export function defineNuxtModule<TOptions extends ModuleOptions> (): {
|
|
|
62
62
|
| `defaults` | `T \| ((nuxt: Nuxt) => T)`{lang="ts"} | `false` | Default options for the module. If a function is provided, it will be called with the Nuxt instance as the first argument. |
|
|
63
63
|
| `schema` | `T` | `false` | Schema for the module options. If provided, options will be applied to the schema. |
|
|
64
64
|
| `hooks` | `Partial<NuxtHooks>`{lang="ts"} | `false` | Hooks to be installed for the module. If provided, the module will install the hooks. |
|
|
65
|
+
| `onInstall` | `(nuxt: Nuxt) => Awaitable<void>`{lang="ts"} | `false` | Lifecycle hook called when the module is first installed. Requires `meta.name` and `meta.version` to be defined. |
|
|
66
|
+
| `onUpgrade` | `(options: T, nuxt: Nuxt, previousVersion: string) => Awaitable<void>`{lang="ts"} | `false` | Lifecycle hook called when the module is upgraded to a newer version. Requires `meta.name` and `meta.version` to be defined. |
|
|
65
67
|
| `setup` | `(this: void, resolvedOptions: T, nuxt: Nuxt) => Awaitable<void \| false \| ModuleSetupInstallResult>`{lang="ts"} | `false` | Setup function for the module. If provided, the module will call the setup function. |
|
|
66
68
|
|
|
67
69
|
### Examples
|
|
@@ -171,6 +173,72 @@ export default defineNuxtModule<ModuleOptions>().with({
|
|
|
171
173
|
|
|
172
174
|
Without using `.with()`, the `resolvedOptions` parameter would be typed as the raw `ModuleOptions` interface, where `timeout` and `retries` could be `undefined` even when defaults are provided. The `.with()` method enables TypeScript to understand that default values make those properties non-optional in the resolved options.
|
|
173
175
|
|
|
176
|
+
#### Using Lifecycle Hooks for Module Installation and Upgrade
|
|
177
|
+
|
|
178
|
+
You can define lifecycle hooks that run when your module is first installed or upgraded to a new version. These hooks are useful for performing one-time setup tasks, database migrations, or cleanup operations.
|
|
179
|
+
|
|
180
|
+
::important
|
|
181
|
+
For lifecycle hooks to work, you **must** provide both `meta.name` and `meta.version` in your module definition. The hooks use these values to track the module's installation state in the project's `.nuxtrc` file.
|
|
182
|
+
::
|
|
183
|
+
|
|
184
|
+
Lifecycle hooks run before the main `setup` function, and if a hook throws an error, it's logged but doesn't stop the build process.
|
|
185
|
+
|
|
186
|
+
**`onInstall`** runs only once when the module is first added to a project.
|
|
187
|
+
|
|
188
|
+
**`onUpgrade`** runs each time the module version increases (using semver comparison) — but only once for each version bump.
|
|
189
|
+
|
|
190
|
+
##### Example
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
194
|
+
import semver from 'semver'
|
|
195
|
+
|
|
196
|
+
export default defineNuxtModule({
|
|
197
|
+
meta: {
|
|
198
|
+
name: 'my-awesome-module',
|
|
199
|
+
version: '1.2.0', // Required for lifecycle hooks
|
|
200
|
+
configKey: 'myAwesomeModule'
|
|
201
|
+
},
|
|
202
|
+
defaults: {
|
|
203
|
+
apiKey: '',
|
|
204
|
+
enabled: true
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
onInstall(nuxt) {
|
|
208
|
+
// This runs only when the module is first installed
|
|
209
|
+
console.log('Setting up my-awesome-module for the first time!')
|
|
210
|
+
|
|
211
|
+
// You might want to:
|
|
212
|
+
// - Create initial configuration files
|
|
213
|
+
// - Set up database schemas
|
|
214
|
+
// - Display welcome messages
|
|
215
|
+
// - Perform initial data migration
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
onUpgrade(options, nuxt, previousVersion) {
|
|
219
|
+
// This runs when the module is upgraded to a newer version
|
|
220
|
+
console.log(`Upgrading my-awesome-module from ${previousVersion} to 1.2.0`)
|
|
221
|
+
|
|
222
|
+
// You might want to:
|
|
223
|
+
// - Migrate configuration files
|
|
224
|
+
// - Update database schemas
|
|
225
|
+
// - Clean up deprecated files
|
|
226
|
+
// - Display upgrade notes
|
|
227
|
+
|
|
228
|
+
if (semver.lt(previousVersion, '1.1.0')) {
|
|
229
|
+
console.log('⚠️ Breaking changes in 1.1.0 - please check the migration guide')
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
setup(options, nuxt) {
|
|
234
|
+
// Regular setup logic runs on every build
|
|
235
|
+
if (options.enabled) {
|
|
236
|
+
// Configure the module
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
174
242
|
## `installModule`
|
|
175
243
|
|
|
176
244
|
Install specified Nuxt module programmatically. This is helpful when your module depends on other modules. You can pass the module options as an object to `inlineOptions` and they will be passed to the module's `setup` function.
|