@nuxt/docs-nightly 4.0.0-29102016.0f2ddb79 → 4.0.0-29102890.1505fb54

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.
@@ -131,7 +131,7 @@ definePageMeta({
131
131
 
132
132
  Nuxt offers route validation via the `validate` property in [`definePageMeta()`](/docs/api/utils/define-page-meta) in each page you wish to validate.
133
133
 
134
- The `validate` property accepts the `route` as an argument. You can return a boolean value to determine whether or not this is a valid route to be rendered with this page. If you return `false`, and another match can't be found, this will cause a 404 error. You can also directly return an object with `statusCode`/`statusMessage` to respond immediately with an error (other matches will not be checked).
134
+ The `validate` property accepts the `route` as an argument. You can return a boolean value to determine whether or not this is a valid route to be rendered with this page. If you return `false`, this will cause a 404 error. You can also directly return an object with `statusCode`/`statusMessage` to customize the error returned.
135
135
 
136
136
  If you have a more complex use case, then you can use anonymous route middleware instead.
137
137
 
@@ -14,87 +14,52 @@ Modules are the building blocks of Nuxt. Kit provides a set of utilities to help
14
14
 
15
15
  Define a Nuxt module, automatically merging defaults with user provided options, installing any hooks that are provided, and calling an optional setup function for full control.
16
16
 
17
- ### Type
17
+ ### Usage
18
18
 
19
- ```ts
20
- function defineNuxtModule<OptionsT extends ModuleOptions> (definition: ModuleDefinition<OptionsT> | NuxtModule<OptionsT>): NuxtModule<OptionsT>
21
-
22
- type ModuleOptions = Record<string, any>
23
-
24
- interface ModuleDefinition<T extends ModuleOptions = ModuleOptions> {
25
- meta?: ModuleMeta
26
- defaults?: T | ((nuxt: Nuxt) => T)
27
- schema?: T
28
- hooks?: Partial<NuxtHooks>
29
- setup?: (this: void, resolvedOptions: T, nuxt: Nuxt) => Awaitable<void | false | ModuleSetupReturn>
30
- }
31
-
32
- interface NuxtModule<T extends ModuleOptions = ModuleOptions> {
33
- (this: void, inlineOptions: T, nuxt: Nuxt): Awaitable<void | false | ModuleSetupReturn>
34
- getOptions?: (inlineOptions?: T, nuxt?: Nuxt) => Promise<T>
35
- getMeta?: () => Promise<ModuleMeta>
36
- }
37
-
38
- interface ModuleSetupReturn {
39
- timings?: {
40
- setup?: number
41
- [key: string]: number | undefined
19
+ ```ts twoslash
20
+ import { defineNuxtModule } from '@nuxt/kit'
21
+
22
+ export default defineNuxtModule({
23
+ meta: {
24
+ name: 'my-module',
25
+ configKey: 'myModule'
26
+ },
27
+ defaults: {
28
+ enabled: true
29
+ },
30
+ setup (options) {
31
+ if (options.enabled) {
32
+ console.log('My Nuxt module is enabled!')
33
+ }
42
34
  }
43
- }
44
-
45
- interface ModuleMeta {
46
- name?: string
47
- version?: string
48
- configKey?: string
49
- compatibility?: NuxtCompatibility
50
- [key: string]: unknown
51
- }
35
+ })
52
36
  ```
53
37
 
54
- ### Parameters
55
-
56
- #### `definition`
57
-
58
- **Type**: `ModuleDefinition<T> | NuxtModule<T>`
59
-
60
- **Required**: `true`
61
-
62
- A module definition object or a module function.
63
-
64
- - `meta` (optional)
65
-
66
- **Type**: `ModuleMeta`
67
-
68
- Metadata of the module. It defines the module name, version, config key and compatibility.
69
-
70
- - `defaults` (optional)
71
-
72
- **Type**: `T | ((nuxt: Nuxt) => T)`
73
-
74
- Default options for the module. If a function is provided, it will be called with the Nuxt instance as the first argument.
75
-
76
- - `schema` (optional)
77
-
78
- **Type**: `T`
79
-
80
- Schema for the module options. If provided, options will be applied to the schema.
38
+ ### Type
81
39
 
82
- - `hooks` (optional)
40
+ ```ts twoslash
41
+ function defineNuxtModule (definition: ModuleDefinition | NuxtModule): NuxtModule
42
+ ```
83
43
 
84
- **Type**: `Partial<NuxtHooks>`
44
+ ### Parameters
85
45
 
86
- Hooks to be installed for the module. If provided, the module will install the hooks.
46
+ **definition**: A module definition object or a module function. The module definition object should contain the following properties:
87
47
 
88
- - `setup` (optional)
48
+ | Property | Type | Required | Description |
49
+ | ------------------ | -------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
50
+ | `meta` | `ModuleMeta` | `false` | Metadata of the module. It defines the module name, version, config key and compatibility. |
51
+ | `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. |
52
+ | `schema` | `T` | `false` | Schema for the module options. If provided, options will be applied to the schema. |
53
+ | `hooks` | `Partial<NuxtHooks>`{lang="ts"} | `false` | Hooks to be installed for the module. If provided, the module will install the hooks. |
54
+ | `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. |
89
55
 
90
- **Type**: `(this: void, resolvedOptions: T, nuxt: Nuxt) => Awaitable<void | false | ModuleSetupReturn>`
56
+ ### Examples
91
57
 
92
- Setup function for the module. If provided, the module will call the setup function.
58
+ #### Using `configKey` to Make Your Module Configurable
93
59
 
94
- ### Examples
60
+ When defining a Nuxt module, you can set a `configKey` to specify how users should configure the module in their `nuxt.config`.
95
61
 
96
62
  ```ts
97
- // https://github.com/nuxt/starter/tree/module
98
63
  import { defineNuxtModule } from '@nuxt/kit'
99
64
 
100
65
  export default defineNuxtModule({
@@ -103,51 +68,94 @@ export default defineNuxtModule({
103
68
  configKey: 'myModule'
104
69
  },
105
70
  defaults: {
106
- test: 123
71
+ // Module options
72
+ enabled: true
107
73
  },
108
- setup (options, nuxt) {
109
- nuxt.hook('modules:done', () => {
110
- console.log('My module is ready with current test option: ', options.test)
111
- })
74
+ setup (options) {
75
+ if (options.enabled) {
76
+ console.log('My Nuxt module is enabled!')
77
+ }
112
78
  }
113
79
  })
114
80
  ```
115
81
 
116
- ## `installModule`
117
-
118
- 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.
119
-
120
- ### Type
82
+ Users can provide options for this module under the corresponding key in `nuxt.config`.
121
83
 
122
84
  ```ts
123
- async function installModule (moduleToInstall: string | NuxtModule, inlineOptions?: any, nuxt?: Nuxt)
85
+ export default defineNuxtConfig({
86
+ myModule: {
87
+ enabled: false
88
+ }
89
+ })
124
90
  ```
125
91
 
126
- ### Parameters
92
+ #### Defining Module Compatibility Requirements
93
+
94
+ If you're developing a Nuxt module and using APIs that are only supported in specific Nuxt versions, it's highly recommended to include `compatibility.nuxt`.
127
95
 
128
- #### `moduleToInstall`
96
+ ```ts
97
+ export default defineNuxtModule({
98
+ meta: {
99
+ name: '@nuxt/icon',
100
+ configKey: 'icon',
101
+ compatibility: {
102
+ // Required nuxt version in semver format.
103
+ nuxt: '>=3.0.0', // or use '^3.0.0'
104
+ },
105
+ },
106
+ async setup() {
107
+ const resolver = createResolver(import.meta.url)
108
+ // Implement
109
+ },
110
+ })
111
+ ```
129
112
 
130
- **Type**: `string` | `NuxtModule`
113
+ If the user tries to use your module with an incompatible Nuxt version, they will receive a warning in the console.
131
114
 
132
- **Required**: `true`
115
+ ```terminal
116
+ WARN Module @nuxt/icon is disabled due to incompatibility issues:
117
+ - [nuxt] Nuxt version ^3.1.0 is required but currently using 3.0.0
118
+ ```
133
119
 
134
- The module to install. Can be either a string with the module name or a module object itself.
120
+ ## `installModule`
135
121
 
136
- #### `inlineOptions`
122
+ 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.
137
123
 
138
- **Type**: `any`
124
+ ### Usage
139
125
 
140
- **Default**: `{}`
126
+ ```ts twoslash
127
+ import { defineNuxtModule, installModule } from '@nuxt/kit'
141
128
 
142
- An object with the module options to be passed to the module's `setup` function.
129
+ export default defineNuxtModule({
130
+ async setup () {
131
+ // will install @nuxtjs/fontaine with Roboto font and Impact fallback
132
+ await installModule('@nuxtjs/fontaine', {
133
+ // module configuration
134
+ fonts: [
135
+ {
136
+ family: 'Roboto',
137
+ fallbacks: ['Impact'],
138
+ fallbackName: 'fallback-a',
139
+ }
140
+ ]
141
+ })
142
+ }
143
+ })
144
+ ```
143
145
 
144
- #### `nuxt`
146
+ ### Type
145
147
 
146
- **Type**: `Nuxt`
148
+ ```ts
149
+ async function installModule (moduleToInstall: string | NuxtModule, inlineOptions?: any, nuxt?: Nuxt)
150
+ ```
147
151
 
148
- **Default**: `useNuxt()`
152
+ ### Parameters
149
153
 
150
- Nuxt instance. If not provided, it will be retrieved from the context via `useNuxt()` call.
154
+ | Property | Type | Required | Description |
155
+ | ------------------ | -------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
156
+ | `moduleToInstall` | `string \| NuxtModule`{lang="ts"} | `true` | The module to install. Can be either a string with the module name or a module object itself. |
157
+ | `inlineOptions` | `any` | `false` | An object with the module options to be passed to the module's `setup` function. |
158
+ | `nuxt` | `Nuxt` | `false` | Nuxt instance. If not provided, it will be retrieved from the context via `useNuxt()` call. |
151
159
 
152
160
  ### Examples
153
161
 
@@ -22,6 +22,8 @@ function useRuntimeConfig (): Record<string, unknown>
22
22
 
23
23
  It is also possible to update runtime configuration. This will be merged with the existing runtime configuration, and if Nitro has already been initialized it will trigger an HMR event to reload the Nitro runtime config.
24
24
 
25
+ ### Type
26
+
25
27
  ```ts
26
28
  function updateRuntimeConfig (config: Record<string, unknown>): void | Promise<void>
27
29
  ```