@nuxt/docs-nightly 4.1.3-29314777.50febbbb → 4.1.3-29316225.304409da
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/03.configuration.md +19 -19
- package/1.getting-started/04.views.md +1 -1
- package/1.getting-started/05.assets.md +8 -2
- package/1.getting-started/06.styling.md +50 -40
- package/1.getting-started/07.routing.md +4 -4
- package/1.getting-started/08.seo-meta.md +46 -33
- package/1.getting-started/09.transitions.md +38 -33
- package/1.getting-started/10.data-fetching.md +47 -39
- package/1.getting-started/11.state-management.md +15 -8
- package/1.getting-started/12.error-handling.md +6 -4
- package/1.getting-started/13.server.md +3 -3
- package/1.getting-started/14.layers.md +18 -12
- package/1.getting-started/15.prerendering.md +24 -24
- package/1.getting-started/16.deployment.md +7 -7
- package/1.getting-started/17.testing.md +41 -41
- package/1.getting-started/18.upgrade.md +48 -66
- package/2.guide/1.directory-structure/1.app/1.components.md +36 -19
- package/2.guide/1.directory-structure/1.app/1.composables.md +7 -7
- package/2.guide/1.directory-structure/1.app/1.layouts.md +9 -5
- package/2.guide/1.directory-structure/1.app/1.middleware.md +20 -14
- package/2.guide/1.directory-structure/1.app/1.pages.md +19 -17
- package/2.guide/1.directory-structure/1.app/1.plugins.md +17 -17
- package/2.guide/1.directory-structure/1.app/1.utils.md +1 -1
- package/2.guide/1.directory-structure/1.app/3.app-config.md +6 -6
- package/2.guide/1.directory-structure/1.app/3.error.md +4 -4
- package/2.guide/1.directory-structure/1.content.md +4 -1
- package/2.guide/1.directory-structure/1.modules.md +4 -4
- package/2.guide/1.directory-structure/1.public.md +1 -1
- package/2.guide/1.directory-structure/1.server.md +28 -28
- package/2.guide/1.directory-structure/1.shared.md +1 -1
- package/2.guide/2.concepts/1.auto-imports.md +11 -11
- package/2.guide/2.concepts/2.vuejs-development.md +5 -5
- package/2.guide/2.concepts/3.rendering.md +14 -12
- package/2.guide/2.concepts/5.modules.md +2 -2
- package/2.guide/2.concepts/7.esm.md +14 -12
- package/2.guide/2.concepts/8.typescript.md +4 -4
- package/2.guide/3.going-further/1.events.md +2 -2
- package/2.guide/3.going-further/1.experimental-features.md +234 -73
- package/2.guide/3.going-further/1.features.md +44 -9
- package/2.guide/3.going-further/1.internals.md +15 -15
- package/2.guide/3.going-further/10.runtime-config.md +7 -7
- package/2.guide/3.going-further/2.hooks.md +8 -8
- package/2.guide/3.going-further/3.modules.md +76 -74
- package/2.guide/3.going-further/4.kit.md +3 -3
- package/2.guide/3.going-further/6.nuxt-app.md +1 -1
- package/2.guide/3.going-further/7.layers.md +50 -41
- package/2.guide/3.going-further/9.debugging.md +2 -2
- package/2.guide/4.recipes/1.custom-routing.md +19 -19
- package/2.guide/4.recipes/2.vite-plugin.md +3 -3
- package/2.guide/4.recipes/3.custom-usefetch.md +9 -9
- package/2.guide/4.recipes/4.sessions-and-authentication.md +33 -19
- package/2.guide/5.best-practices/performance.md +8 -8
- package/3.api/1.components/1.client-only.md +6 -3
- package/3.api/1.components/1.nuxt-client-fallback.md +9 -6
- package/3.api/1.components/11.teleports.md +4 -1
- package/3.api/1.components/12.nuxt-route-announcer.md +7 -7
- package/3.api/1.components/13.nuxt-time.md +44 -17
- package/3.api/1.components/2.nuxt-page.md +2 -2
- package/3.api/1.components/3.nuxt-layout.md +9 -4
- package/3.api/1.components/4.nuxt-link.md +38 -18
- package/3.api/1.components/6.nuxt-error-boundary.md +11 -9
- package/3.api/2.composables/on-prehydrate.md +2 -2
- package/3.api/2.composables/use-async-data.md +11 -11
- package/3.api/2.composables/use-cookie.md +26 -18
- package/3.api/2.composables/use-error.md +1 -1
- package/3.api/2.composables/use-fetch.md +13 -13
- package/3.api/2.composables/use-head-safe.md +5 -5
- package/3.api/2.composables/use-head.md +2 -2
- package/3.api/2.composables/use-hydration.md +5 -5
- package/3.api/2.composables/use-loading-indicator.md +10 -10
- package/3.api/2.composables/use-nuxt-app.md +9 -9
- package/3.api/2.composables/use-nuxt-data.md +7 -7
- package/3.api/2.composables/use-preview-mode.md +12 -15
- package/3.api/2.composables/use-request-fetch.md +1 -1
- package/3.api/2.composables/use-request-headers.md +3 -3
- package/3.api/2.composables/use-response-header.md +7 -8
- package/3.api/2.composables/use-route-announcer.md +3 -3
- package/3.api/2.composables/use-router.md +5 -3
- package/3.api/2.composables/use-runtime-config.md +6 -6
- package/3.api/2.composables/use-runtime-hook.md +1 -1
- package/3.api/2.composables/use-seo-meta.md +1 -1
- package/3.api/2.composables/use-server-seo-meta.md +1 -1
- package/3.api/2.composables/use-state.md +3 -3
- package/3.api/3.utils/$fetch.md +5 -3
- package/3.api/3.utils/abort-navigation.md +2 -2
- package/3.api/3.utils/add-route-middleware.md +3 -3
- package/3.api/3.utils/call-once.md +3 -3
- package/3.api/3.utils/clear-error.md +1 -1
- package/3.api/3.utils/clear-nuxt-data.md +2 -2
- package/3.api/3.utils/clear-nuxt-state.md +2 -2
- package/3.api/3.utils/create-error.md +1 -1
- 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 +11 -11
- package/3.api/3.utils/define-nuxt-route-middleware.md +2 -2
- package/3.api/3.utils/define-page-meta.md +14 -14
- package/3.api/3.utils/define-route-rules.md +3 -3
- package/3.api/3.utils/navigate-to.md +9 -9
- package/3.api/3.utils/preload-components.md +1 -1
- package/3.api/3.utils/prerender-routes.md +2 -2
- package/3.api/3.utils/refresh-cookie.md +3 -3
- package/3.api/3.utils/refresh-nuxt-data.md +9 -4
- package/3.api/3.utils/reload-nuxt-app.md +2 -2
- package/3.api/3.utils/set-response-status.md +1 -1
- package/3.api/3.utils/show-error.md +2 -2
- package/3.api/3.utils/update-app-config.md +2 -1
- package/3.api/4.commands/prepare.md +4 -0
- package/3.api/5.kit/1.modules.md +36 -36
- 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 +12 -12
- package/3.api/5.kit/4.autoimports.md +12 -12
- package/3.api/5.kit/5.components.md +5 -5
- package/3.api/5.kit/6.context.md +3 -3
- package/3.api/5.kit/7.pages.md +4 -4
- package/3.api/5.kit/8.layout.md +1 -1
- package/3.api/5.kit/9.plugins.md +5 -4
- package/3.api/6.advanced/2.import-meta.md +3 -3
- package/3.api/6.nuxt-config.md +293 -858
- package/6.bridge/1.overview.md +1 -1
- package/6.bridge/10.configuration.md +2 -1
- package/6.bridge/2.typescript.md +2 -2
- package/6.bridge/3.bridge-composition-api.md +2 -2
- package/6.bridge/4.plugins-and-middleware.md +5 -5
- package/6.bridge/5.nuxt3-compatible-api.md +11 -8
- package/6.bridge/6.meta.md +15 -14
- package/6.bridge/8.nitro.md +2 -2
- package/6.bridge/9.vite.md +3 -3
- package/7.migration/2.configuration.md +17 -15
- package/7.migration/4.meta.md +17 -14
- package/7.migration/5.plugins-and-middleware.md +5 -5
- package/7.migration/6.pages-and-layouts.md +15 -11
- package/7.migration/7.component-options.md +3 -3
- package/7.migration/8.runtime-config.md +3 -3
- package/package.json +1 -1
|
@@ -36,31 +36,31 @@ Check [Nuxt App](/docs/4.x/api/composables/use-nuxt-app) for more information ab
|
|
|
36
36
|
|
|
37
37
|
`nuxtApp` has the following properties:
|
|
38
38
|
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
vueApp
|
|
39
|
+
```ts
|
|
40
|
+
interface NuxtApp {
|
|
41
|
+
vueApp // the global Vue application: https://vuejs.org/api/application.html#application-api
|
|
42
42
|
|
|
43
|
-
versions
|
|
43
|
+
versions // an object containing Nuxt and Vue versions
|
|
44
44
|
|
|
45
45
|
// These let you call and add runtime NuxtApp hooks
|
|
46
46
|
// https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/nuxt.ts#L18
|
|
47
|
-
hooks
|
|
48
|
-
hook
|
|
49
|
-
callHook
|
|
47
|
+
hooks
|
|
48
|
+
hook
|
|
49
|
+
callHook
|
|
50
50
|
|
|
51
51
|
// Only accessible on server-side
|
|
52
52
|
ssrContext: {
|
|
53
|
-
url
|
|
54
|
-
req
|
|
55
|
-
res
|
|
56
|
-
runtimeConfig
|
|
57
|
-
noSSR
|
|
58
|
-
}
|
|
53
|
+
url
|
|
54
|
+
req
|
|
55
|
+
res
|
|
56
|
+
runtimeConfig
|
|
57
|
+
noSSR
|
|
58
|
+
}
|
|
59
59
|
|
|
60
60
|
// This will be stringified and passed from server to client
|
|
61
61
|
payload: {
|
|
62
|
-
serverRendered: true
|
|
63
|
-
data: {}
|
|
62
|
+
serverRendered: true
|
|
63
|
+
data: {}
|
|
64
64
|
state: {}
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -14,9 +14,9 @@ export default defineNuxtConfig({
|
|
|
14
14
|
apiSecret: '123',
|
|
15
15
|
// Keys within public, will be also exposed to the client-side
|
|
16
16
|
public: {
|
|
17
|
-
apiBase: '/api'
|
|
18
|
-
}
|
|
19
|
-
}
|
|
17
|
+
apiBase: '/api',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
20
|
})
|
|
21
21
|
```
|
|
22
22
|
|
|
@@ -78,7 +78,7 @@ export default defineNuxtConfig({
|
|
|
78
78
|
apiSecret: '', // can be overridden by NUXT_API_SECRET environment variable
|
|
79
79
|
public: {
|
|
80
80
|
apiBase: '', // can be overridden by NUXT_PUBLIC_API_BASE environment variable
|
|
81
|
-
}
|
|
81
|
+
},
|
|
82
82
|
},
|
|
83
83
|
})
|
|
84
84
|
```
|
|
@@ -127,7 +127,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
127
127
|
const config = useRuntimeConfig()
|
|
128
128
|
|
|
129
129
|
console.log('API base URL:', config.public.apiBase)
|
|
130
|
-
})
|
|
130
|
+
})
|
|
131
131
|
```
|
|
132
132
|
|
|
133
133
|
### Server Routes
|
|
@@ -139,8 +139,8 @@ export default defineEventHandler(async (event) => {
|
|
|
139
139
|
const { apiSecret } = useRuntimeConfig(event)
|
|
140
140
|
const result = await $fetch('https://my.api.com/test', {
|
|
141
141
|
headers: {
|
|
142
|
-
Authorization: `Bearer ${apiSecret}
|
|
143
|
-
}
|
|
142
|
+
Authorization: `Bearer ${apiSecret}`,
|
|
143
|
+
},
|
|
144
144
|
})
|
|
145
145
|
return result
|
|
146
146
|
})
|
|
@@ -13,11 +13,11 @@ These hooks are available for [Nuxt Modules](/docs/4.x/guide/going-further/modul
|
|
|
13
13
|
|
|
14
14
|
### Within `nuxt.config.ts`
|
|
15
15
|
|
|
16
|
-
```
|
|
16
|
+
```ts [nuxt.config.ts]
|
|
17
17
|
export default defineNuxtConfig({
|
|
18
18
|
hooks: {
|
|
19
|
-
close: () => { }
|
|
20
|
-
}
|
|
19
|
+
close: () => { },
|
|
20
|
+
},
|
|
21
21
|
})
|
|
22
22
|
```
|
|
23
23
|
|
|
@@ -29,7 +29,7 @@ import { defineNuxtModule } from '@nuxt/kit'
|
|
|
29
29
|
export default defineNuxtModule({
|
|
30
30
|
setup (options, nuxt) {
|
|
31
31
|
nuxt.hook('close', async () => { })
|
|
32
|
-
}
|
|
32
|
+
},
|
|
33
33
|
})
|
|
34
34
|
```
|
|
35
35
|
|
|
@@ -41,7 +41,7 @@ Explore all available Nuxt hooks.
|
|
|
41
41
|
|
|
42
42
|
App hooks can be mainly used by [Nuxt Plugins](/docs/4.x/guide/directory-structure/plugins) to hook into rendering lifecycle but could also be used in Vue composables.
|
|
43
43
|
|
|
44
|
-
```
|
|
44
|
+
```ts [app/plugins/test.ts]
|
|
45
45
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
46
46
|
nuxtApp.hook('page:start', () => {
|
|
47
47
|
/* your code goes here */
|
|
@@ -57,7 +57,7 @@ Explore all available App hooks.
|
|
|
57
57
|
|
|
58
58
|
These hooks are available for [server plugins](/docs/4.x/guide/directory-structure/server#server-plugins) to hook into Nitro's runtime behavior.
|
|
59
59
|
|
|
60
|
-
```
|
|
60
|
+
```ts [~/server/plugins/test.ts]
|
|
61
61
|
export default defineNitroPlugin((nitroApp) => {
|
|
62
62
|
nitroApp.hooks.hook('render:html', (html, { event }) => {
|
|
63
63
|
console.log('render:html', html)
|
|
@@ -79,7 +79,7 @@ Learn more about available Nitro lifecycle hooks.
|
|
|
79
79
|
You can define your own custom hooks support by extending Nuxt's hook interfaces.
|
|
80
80
|
|
|
81
81
|
```ts
|
|
82
|
-
import { HookResult } from
|
|
82
|
+
import type { HookResult } from '@nuxt/schema'
|
|
83
83
|
|
|
84
84
|
declare module '#app' {
|
|
85
85
|
interface RuntimeNuxtHooks {
|
|
@@ -92,7 +92,7 @@ declare module '#app' {
|
|
|
92
92
|
|
|
93
93
|
declare module 'nitropack/types' {
|
|
94
94
|
interface NitroRuntimeHooks {
|
|
95
|
-
'your-nitro-hook': () => void
|
|
95
|
+
'your-nitro-hook': () => void
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
```
|
|
@@ -136,7 +136,7 @@ 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
|
}
|
|
@@ -148,7 +148,7 @@ You can get type-hint support for this function using the higher-level `defineNu
|
|
|
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
|
|
|
@@ -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
|
|
|
@@ -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
|
|
|
@@ -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,7 +345,7 @@ 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
|
|
|
@@ -353,43 +355,43 @@ export default defineNuxtModule({
|
|
|
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
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,12 +521,12 @@ 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
|
|
|
@@ -533,21 +535,21 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
533
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
|
|
|
@@ -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' })
|
|
@@ -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
|
|
@@ -29,7 +29,7 @@ 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
|
|
|
@@ -41,10 +41,10 @@ Nuxt Kit utilities are only available for modules and not meant to be imported i
|
|
|
41
41
|
|
|
42
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()
|
|
@@ -50,7 +50,7 @@ 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
|
```
|