@nuxt/docs 3.20.2 → 3.21.1
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 +4 -4
- package/1.getting-started/02.installation.md +4 -7
- package/1.getting-started/03.configuration.md +29 -29
- package/1.getting-started/04.views.md +6 -4
- package/1.getting-started/05.assets.md +2 -2
- package/1.getting-started/06.styling.md +22 -16
- package/1.getting-started/07.routing.md +6 -6
- package/1.getting-started/08.seo-meta.md +8 -4
- package/1.getting-started/09.transitions.md +6 -6
- package/1.getting-started/10.data-fetching.md +18 -18
- package/1.getting-started/11.state-management.md +5 -5
- package/1.getting-started/12.error-handling.md +25 -19
- package/1.getting-started/13.server.md +9 -9
- package/1.getting-started/14.layers.md +49 -15
- package/1.getting-started/15.prerendering.md +18 -4
- package/1.getting-started/16.deployment.md +2 -1
- package/1.getting-started/17.testing.md +17 -7
- package/1.getting-started/18.upgrade.md +111 -60
- package/2.directory-structure/0.output.md +1 -1
- package/2.directory-structure/1.assets.md +1 -1
- package/2.directory-structure/1.components.md +12 -8
- package/2.directory-structure/1.composables.md +2 -2
- package/2.directory-structure/1.content.md +1 -1
- package/2.directory-structure/1.layers.md +87 -0
- package/2.directory-structure/1.layouts.md +35 -3
- package/2.directory-structure/1.middleware.md +7 -7
- package/2.directory-structure/1.modules.md +8 -2
- package/2.directory-structure/1.node_modules.md +1 -1
- package/2.directory-structure/1.pages.md +39 -18
- package/2.directory-structure/1.plugins.md +4 -1
- package/2.directory-structure/1.public.md +1 -1
- package/2.directory-structure/1.server.md +28 -8
- package/2.directory-structure/1.shared.md +3 -3
- package/2.directory-structure/1.utils.md +2 -2
- package/2.directory-structure/2.env.md +3 -3
- package/2.directory-structure/2.nuxtignore.md +1 -0
- package/2.directory-structure/2.nuxtrc.md +5 -2
- package/2.directory-structure/3.app-config.md +2 -2
- package/2.directory-structure/3.app.md +3 -3
- package/2.directory-structure/3.error.md +9 -5
- package/2.directory-structure/3.nuxt-config.md +1 -1
- package/2.directory-structure/3.package.md +1 -1
- package/2.directory-structure/3.tsconfig.md +3 -2
- package/2.directory-structure/index.md +12 -8
- package/3.guide/0.index.md +5 -2
- package/3.guide/1.concepts/{3.rendering.md → 1.rendering.md} +11 -32
- package/3.guide/1.concepts/{2.vuejs-development.md → 10.vuejs-development.md} +9 -8
- package/3.guide/1.concepts/{10.nuxt-lifecycle.md → 2.nuxt-lifecycle.md} +31 -24
- package/3.guide/1.concepts/{1.auto-imports.md → 3.auto-imports.md} +6 -6
- package/3.guide/1.concepts/4.server-engine.md +2 -2
- package/3.guide/1.concepts/5.modules.md +14 -1
- package/3.guide/1.concepts/7.esm.md +5 -4
- package/3.guide/1.concepts/8.typescript.md +9 -15
- package/3.guide/1.concepts/9.code-style.md +1 -1
- package/3.guide/2.best-practices/hydration.md +1 -1
- package/3.guide/2.best-practices/performance.md +5 -5
- package/3.guide/3.ai/.navigation.yml +3 -0
- package/3.guide/3.ai/1.mcp.md +277 -0
- package/3.guide/3.ai/2.llms-txt.md +65 -0
- package/3.guide/4.modules/.navigation.yml +3 -0
- package/3.guide/4.modules/1.getting-started.md +103 -0
- package/3.guide/4.modules/2.module-anatomy.md +138 -0
- package/3.guide/4.modules/3.recipes-basics.md +420 -0
- package/3.guide/4.modules/4.recipes-advanced.md +243 -0
- package/3.guide/4.modules/5.testing.md +76 -0
- package/3.guide/4.modules/6.best-practices.md +104 -0
- package/3.guide/4.modules/7.ecosystem.md +32 -0
- package/3.guide/4.modules/index.md +36 -0
- package/3.guide/{3.recipes → 5.recipes}/1.custom-routing.md +3 -3
- package/3.guide/{3.recipes → 5.recipes}/2.vite-plugin.md +4 -0
- package/3.guide/{3.recipes → 5.recipes}/3.custom-usefetch.md +2 -2
- package/3.guide/{3.recipes → 5.recipes}/4.sessions-and-authentication.md +3 -3
- package/3.guide/{4.going-further → 6.going-further}/1.events.md +3 -4
- package/3.guide/{4.going-further → 6.going-further}/1.experimental-features.md +211 -86
- package/3.guide/6.going-further/1.features.md +108 -0
- package/3.guide/{4.going-further → 6.going-further}/1.internals.md +4 -3
- package/3.guide/{4.going-further → 6.going-further}/10.runtime-config.md +2 -2
- package/3.guide/{4.going-further → 6.going-further}/11.nightly-release-channel.md +1 -1
- package/3.guide/{4.going-further → 6.going-further}/2.hooks.md +5 -5
- package/3.guide/{4.going-further → 6.going-further}/4.kit.md +3 -3
- package/3.guide/{4.going-further → 6.going-further}/6.nuxt-app.md +6 -6
- package/3.guide/{4.going-further → 6.going-further}/7.layers.md +31 -5
- package/3.guide/{4.going-further → 6.going-further}/9.debugging.md +3 -2
- package/4.api/1.components/1.nuxt-client-fallback.md +5 -1
- package/4.api/1.components/10.nuxt-picture.md +1 -1
- package/4.api/1.components/11.teleports.md +2 -2
- package/4.api/1.components/12.nuxt-route-announcer.md +0 -2
- package/4.api/1.components/13.nuxt-time.md +0 -2
- package/4.api/1.components/2.nuxt-page.md +3 -3
- package/4.api/1.components/3.nuxt-layout.md +6 -6
- package/4.api/1.components/4.nuxt-link.md +13 -13
- package/4.api/1.components/5.nuxt-loading-indicator.md +1 -1
- package/4.api/1.components/6.nuxt-error-boundary.md +2 -2
- package/4.api/1.components/7.nuxt-welcome.md +2 -2
- package/4.api/1.components/8.nuxt-island.md +9 -2
- package/4.api/2.composables/use-app-config.md +1 -1
- package/4.api/2.composables/use-async-data.md +5 -5
- package/4.api/2.composables/use-cookie.md +16 -16
- package/4.api/2.composables/use-error.md +3 -3
- package/4.api/2.composables/use-fetch.md +37 -37
- package/4.api/2.composables/use-head-safe.md +1 -1
- package/4.api/2.composables/use-head.md +22 -7
- package/4.api/2.composables/use-lazy-async-data.md +1 -1
- package/4.api/2.composables/use-lazy-fetch.md +9 -9
- package/4.api/2.composables/use-nuxt-app.md +9 -7
- package/4.api/2.composables/use-route-announcer.md +1 -3
- package/4.api/2.composables/use-route.md +2 -2
- package/4.api/2.composables/use-router.md +15 -15
- package/4.api/2.composables/use-runtime-config.md +5 -5
- package/4.api/2.composables/use-seo-meta.md +2 -2
- package/4.api/2.composables/use-server-seo-meta.md +2 -2
- package/4.api/2.composables/use-state.md +12 -2
- package/4.api/3.utils/$fetch.md +1 -1
- package/4.api/3.utils/abort-navigation.md +2 -2
- package/4.api/3.utils/call-once.md +2 -4
- package/4.api/3.utils/clear-error.md +1 -1
- package/4.api/3.utils/create-error.md +7 -7
- package/4.api/3.utils/define-lazy-hydration-component.md +5 -5
- package/4.api/3.utils/define-nuxt-component.md +1 -1
- package/4.api/3.utils/define-nuxt-plugin.md +12 -12
- package/4.api/3.utils/define-nuxt-route-middleware.md +2 -2
- package/4.api/3.utils/define-page-meta.md +18 -11
- package/4.api/3.utils/define-route-rules.md +2 -2
- package/4.api/3.utils/navigate-to.md +14 -14
- package/4.api/3.utils/on-before-route-leave.md +1 -1
- package/4.api/3.utils/on-before-route-update.md +1 -1
- package/4.api/3.utils/preload-route-components.md +2 -2
- package/4.api/3.utils/refresh-cookie.md +0 -2
- package/4.api/3.utils/refresh-nuxt-data.md +1 -1
- package/4.api/3.utils/reload-nuxt-app.md +1 -1
- package/4.api/3.utils/set-page-layout.md +36 -0
- package/4.api/3.utils/set-response-status.md +3 -3
- package/4.api/3.utils/show-error.md +4 -4
- package/4.api/3.utils/update-app-config.md +1 -1
- package/4.api/4.commands/add.md +12 -12
- package/4.api/4.commands/analyze.md +11 -11
- package/4.api/4.commands/build-module.md +11 -11
- package/4.api/4.commands/build.md +12 -12
- package/4.api/4.commands/cleanup.md +6 -6
- package/4.api/4.commands/dev.md +23 -23
- package/4.api/4.commands/devtools.md +7 -7
- package/4.api/4.commands/generate.md +12 -12
- package/4.api/4.commands/info.md +6 -6
- package/4.api/4.commands/init.md +18 -18
- package/4.api/4.commands/module.md +18 -18
- package/4.api/4.commands/prepare.md +10 -10
- package/4.api/4.commands/preview.md +11 -11
- package/4.api/4.commands/test.md +9 -9
- package/4.api/4.commands/typecheck.md +10 -10
- package/4.api/4.commands/upgrade.md +10 -10
- package/4.api/5.kit/1.modules.md +31 -18
- package/4.api/5.kit/10.templates.md +23 -23
- package/4.api/5.kit/11.nitro.md +40 -36
- package/4.api/5.kit/12.resolving.md +2 -2
- package/4.api/5.kit/14.builder.md +35 -23
- package/4.api/5.kit/16.layers.md +16 -16
- package/4.api/5.kit/2.programmatic.md +2 -2
- package/4.api/5.kit/3.compatibility.md +2 -2
- package/4.api/5.kit/4.autoimports.md +23 -19
- package/4.api/5.kit/5.components.md +35 -35
- package/4.api/5.kit/6.context.md +1 -1
- package/4.api/5.kit/8.layout.md +1 -1
- package/4.api/6.advanced/1.hooks.md +85 -85
- package/4.api/index.md +7 -7
- package/5.community/4.contribution.md +10 -10
- package/5.community/5.framework-contribution.md +9 -9
- package/5.community/6.roadmap.md +26 -26
- package/5.community/7.changelog.md +10 -0
- package/6.bridge/1.overview.md +8 -0
- package/6.bridge/3.bridge-composition-api.md +2 -2
- package/6.bridge/4.plugins-and-middleware.md +2 -2
- package/6.bridge/5.nuxt3-compatible-api.md +1 -1
- package/6.bridge/8.nitro.md +4 -0
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +5 -5
- package/7.migration/20.module-authors.md +3 -3
- package/7.migration/4.meta.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +3 -3
- package/7.migration/6.pages-and-layouts.md +5 -5
- package/7.migration/7.component-options.md +6 -6
- package/7.migration/8.runtime-config.md +1 -1
- package/package.json +1 -1
- package/3.guide/4.going-further/1.features.md +0 -103
- package/3.guide/4.going-further/3.modules.md +0 -901
- /package/3.guide/{3.recipes → 5.recipes}/.navigation.yml +0 -0
- /package/3.guide/{4.going-further → 6.going-further}/.navigation.yml +0 -0
- /package/3.guide/{4.going-further → 6.going-further}/index.md +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Test Your Module"
|
|
3
|
+
description: "Learn how to test your Nuxt module with unit, integration and E2E tests."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Testing helps ensure your module works as expected given various setups. Find in this section how to perform various kinds of tests against your module.
|
|
7
|
+
|
|
8
|
+
## Write Unit Tests
|
|
9
|
+
|
|
10
|
+
::tip
|
|
11
|
+
We're still discussing and exploring how to ease unit and integration testing on Nuxt modules.
|
|
12
|
+
:br :br
|
|
13
|
+
[Check out this RFC to join the conversation](https://github.com/nuxt/nuxt/discussions/18399).
|
|
14
|
+
::
|
|
15
|
+
|
|
16
|
+
## Write E2E Tests
|
|
17
|
+
|
|
18
|
+
[Nuxt Test Utils](/docs/3.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:
|
|
19
|
+
|
|
20
|
+
1. Create a Nuxt application to be used as a "fixture" inside `test/fixtures/*`
|
|
21
|
+
2. Setup Nuxt with this fixture inside your test file
|
|
22
|
+
3. Interact with the fixture using utilities from `@nuxt/test-utils` (e.g. fetching a page)
|
|
23
|
+
4. Perform checks related to this fixture (e.g. "HTML contains ...")
|
|
24
|
+
5. Repeat
|
|
25
|
+
|
|
26
|
+
In practice, the fixture:
|
|
27
|
+
|
|
28
|
+
```ts [test/fixtures/ssr/nuxt.config.ts]
|
|
29
|
+
// 1. Create a Nuxt application to be used as a "fixture"
|
|
30
|
+
import MyModule from '../../../src/module'
|
|
31
|
+
|
|
32
|
+
export default defineNuxtConfig({
|
|
33
|
+
ssr: true,
|
|
34
|
+
modules: [
|
|
35
|
+
MyModule,
|
|
36
|
+
],
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
And its test:
|
|
41
|
+
|
|
42
|
+
```ts [test/rendering.ts]
|
|
43
|
+
import { describe, expect, it } from 'vitest'
|
|
44
|
+
import { fileURLToPath } from 'node:url'
|
|
45
|
+
import { $fetch, setup } from '@nuxt/test-utils/e2e'
|
|
46
|
+
|
|
47
|
+
describe('ssr', async () => {
|
|
48
|
+
// 2. Setup Nuxt with this fixture inside your test file
|
|
49
|
+
await setup({
|
|
50
|
+
rootDir: fileURLToPath(new URL('./fixtures/ssr', import.meta.url)),
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('renders the index page', async () => {
|
|
54
|
+
// 3. Interact with the fixture using utilities from `@nuxt/test-utils`
|
|
55
|
+
const html = await $fetch('/')
|
|
56
|
+
|
|
57
|
+
// 4. Perform checks related to this fixture
|
|
58
|
+
expect(html).toContain('<div>ssr</div>')
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// 5. Repeat
|
|
63
|
+
describe('csr', async () => { /* ... */ })
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
::tip
|
|
67
|
+
An example of such a workflow is available on [the module starter](https://github.com/nuxt/starter/blob/module/test/basic.test.ts).
|
|
68
|
+
::
|
|
69
|
+
|
|
70
|
+
## Test Manually
|
|
71
|
+
|
|
72
|
+
Having a playground Nuxt application to test your module when developing it is really useful. [The module starter integrates one for that purpose](/docs/3.x/guide/modules/getting-started#develop-your-module).
|
|
73
|
+
|
|
74
|
+
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"`.
|
|
75
|
+
|
|
76
|
+
After that, you should be able to reference `my-module` like in any regular project.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Follow Best Practices"
|
|
3
|
+
description: "Build performant and maintainable Nuxt modules with these guidelines."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
With great power comes great responsibility. While modules are powerful, here are some best practices to keep in mind while authoring modules to keep applications performant and developer experience great.
|
|
7
|
+
|
|
8
|
+
## Handle Async Setup
|
|
9
|
+
|
|
10
|
+
As we've seen, Nuxt modules can be asynchronous. For example, you may want to develop a module that needs fetching some API or calling an async function.
|
|
11
|
+
|
|
12
|
+
However, be careful with asynchronous behaviors as Nuxt will wait for your module to setup before going to the next module and starting the development server, build process, etc. Prefer deferring time-consuming logic to Nuxt hooks.
|
|
13
|
+
|
|
14
|
+
::warning
|
|
15
|
+
If your module takes more than **1 second** to setup, Nuxt will emit a warning about it.
|
|
16
|
+
::
|
|
17
|
+
|
|
18
|
+
## Prefix Your Exports
|
|
19
|
+
|
|
20
|
+
Nuxt modules should provide an explicit prefix for any exposed configuration, plugin, API, composable, component, or server route to avoid conflicts with other modules, Nuxt internals, or user-defined code.
|
|
21
|
+
|
|
22
|
+
Ideally, prefix them with your module's name. For example, if your module is called `nuxt-foo`:
|
|
23
|
+
|
|
24
|
+
| Type | ❌ Avoid | ✅ Prefer |
|
|
25
|
+
| --- | --- | --- |
|
|
26
|
+
| Components | `<Button>`, `<Modal>` | `<FooButton>`, `<FooModal>` |
|
|
27
|
+
| Composables | `useData()`, `useModal()` | `useFooData()`, `useFooModal()` |
|
|
28
|
+
| Server routes | `/api/track`, `/api/data` | `/api/_foo/track`, `/api/_foo/data` |
|
|
29
|
+
|
|
30
|
+
### Server Routes
|
|
31
|
+
|
|
32
|
+
This is particularly important for server routes, where common paths like `/api/auth`, `/api/login`, or `/api/user` are very likely already used by the application.
|
|
33
|
+
|
|
34
|
+
Use a unique prefix based on your module name:
|
|
35
|
+
- `/api/_foo/...` (using underscore prefix)
|
|
36
|
+
- `/_foo/...` (for non-API routes)
|
|
37
|
+
|
|
38
|
+
## Use Lifecycle Hooks
|
|
39
|
+
|
|
40
|
+
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.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { addServerHandler, defineNuxtModule } from 'nuxt/kit'
|
|
44
|
+
import semver from 'semver'
|
|
45
|
+
|
|
46
|
+
export default defineNuxtModule({
|
|
47
|
+
meta: {
|
|
48
|
+
name: 'my-database-module',
|
|
49
|
+
version: '1.0.0',
|
|
50
|
+
},
|
|
51
|
+
async onInstall (nuxt) {
|
|
52
|
+
// One-time setup: create database schema, generate config files, etc.
|
|
53
|
+
await generateDatabaseConfig(nuxt.options.rootDir)
|
|
54
|
+
},
|
|
55
|
+
async onUpgrade (nuxt, options, previousVersion) {
|
|
56
|
+
// Handle version-specific migrations
|
|
57
|
+
if (semver.lt(previousVersion, '1.0.0')) {
|
|
58
|
+
await migrateLegacyData()
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
setup (options, nuxt) {
|
|
62
|
+
// Regular setup logic that runs on every build
|
|
63
|
+
addServerHandler({ /* ... */ })
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
This pattern prevents unnecessary work on every build and provides a better developer experience. See the [lifecycle hooks documentation](/docs/3.x/api/kit/modules#using-lifecycle-hooks-for-module-installation-and-upgrade) for more details.
|
|
69
|
+
|
|
70
|
+
## Be TypeScript Friendly
|
|
71
|
+
|
|
72
|
+
Nuxt has first-class TypeScript integration for the best developer experience.
|
|
73
|
+
|
|
74
|
+
Exposing types and using TypeScript to develop modules benefits users even when not using TypeScript directly.
|
|
75
|
+
|
|
76
|
+
## Use ESM Syntax
|
|
77
|
+
|
|
78
|
+
Nuxt relies on native ESM. Please read [Native ES Modules](/docs/3.x/guide/concepts/esm) for more information.
|
|
79
|
+
|
|
80
|
+
## Document Your Module
|
|
81
|
+
|
|
82
|
+
Consider documenting module usage in the readme file:
|
|
83
|
+
|
|
84
|
+
- Why use this module?
|
|
85
|
+
- How to use this module?
|
|
86
|
+
- What does this module do?
|
|
87
|
+
|
|
88
|
+
Linking to the integration website and documentation is always a good idea.
|
|
89
|
+
|
|
90
|
+
## Provide a Demo
|
|
91
|
+
|
|
92
|
+
It's a good practice to make a minimal reproduction with your module and [StackBlitz](https://nuxt.new/s/v4) that you add to your module readme.
|
|
93
|
+
|
|
94
|
+
This not only provides potential users of your module a quick and easy way to experiment with the module but also an easy way for them to build minimal reproductions they can send you when they encounter issues.
|
|
95
|
+
|
|
96
|
+
## Stay Version Agnostic
|
|
97
|
+
|
|
98
|
+
Nuxt, Nuxt Kit, and other new toolings are made to have both forward and backward compatibility in mind.
|
|
99
|
+
|
|
100
|
+
Please use "X for Nuxt" instead of "X for Nuxt 3" to avoid fragmentation in the ecosystem and prefer using `meta.compatibility` to set Nuxt version constraints.
|
|
101
|
+
|
|
102
|
+
## Follow Starter Conventions
|
|
103
|
+
|
|
104
|
+
The module starter comes with a default set of tools and configurations (e.g. ESLint configuration). If you plan on open-sourcing your module, sticking with those defaults ensures your module shares a consistent coding style with other [community modules](/modules) out there, making it easier for others to contribute.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Publish & Share Your Module"
|
|
3
|
+
description: "Join the Nuxt module ecosystem and publish your module to npm."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
The [Nuxt module ecosystem](/modules) represents more than 35 million monthly NPM downloads and provides extended functionalities and integrations with all sort of tools. You can be part of this ecosystem!
|
|
7
|
+
|
|
8
|
+
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/exploring-nuxt-modules-ecosystem-and-module-types?friend=nuxt" target="_blank"}
|
|
9
|
+
Watch Vue School video about Nuxt module types.
|
|
10
|
+
::
|
|
11
|
+
|
|
12
|
+
## Understand Module Types
|
|
13
|
+
|
|
14
|
+
**Official modules** are modules prefixed (scoped) with `@nuxt/` (e.g. [`@nuxt/content`](https://content.nuxt.com)). They are made and maintained actively by the Nuxt team. Like with the framework, contributions from the community are more than welcome to help make them better!
|
|
15
|
+
|
|
16
|
+
**Community modules** are modules prefixed (scoped) with `@nuxtjs/` (e.g. [`@nuxtjs/tailwindcss`](https://tailwindcss.nuxtjs.org)). They are proven modules made and maintained by community members. Again, contributions are welcome from anyone.
|
|
17
|
+
|
|
18
|
+
**Third-party and other community modules** are modules (often) prefixed with `nuxt-`. Anyone can make them, using this prefix allows these modules to be discoverable on npm. This is the best starting point to draft and try an idea!
|
|
19
|
+
|
|
20
|
+
**Private or personal modules** are modules made for your own use case or company. They don't need to follow any naming rules to work with Nuxt and are often seen scoped under an npm organization (e.g. `@my-company/nuxt-auth`)
|
|
21
|
+
|
|
22
|
+
## List Your Module
|
|
23
|
+
|
|
24
|
+
Any community modules are welcome to be listed on [the module list](/modules). To be listed, [open an issue in the nuxt/modules](https://github.com/nuxt/modules/issues/new?template=module_request.yml) repository. The Nuxt team can help you to apply best practices before listing.
|
|
25
|
+
|
|
26
|
+
## Join nuxt-modules
|
|
27
|
+
|
|
28
|
+
By moving your modules to [nuxt-modules](https://github.com/nuxt-modules), there is always someone else to help, and this way, we can join forces to make one perfect solution.
|
|
29
|
+
|
|
30
|
+
If you have an already published and working module, and want to transfer it to `nuxt-modules`, [open an issue in nuxt/modules](https://github.com/nuxt/modules/issues/new).
|
|
31
|
+
|
|
32
|
+
By joining `nuxt-modules` we can rename your community module under the `@nuxtjs/` scope and provide a subdomain (e.g. `my-module.nuxtjs.org`) for its documentation.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Module Author Guide'
|
|
3
|
+
titleTemplate: '%s'
|
|
4
|
+
description: 'Learn how to create a Nuxt module to integrate, enhance or extend any Nuxt applications.'
|
|
5
|
+
navigation: false
|
|
6
|
+
surround: false
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
Nuxt's [configuration](/docs/3.x/api/nuxt-config) and [hooks](/docs/3.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.).
|
|
10
|
+
|
|
11
|
+
**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`.
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
::card-group{class="sm:grid-cols-1"}
|
|
15
|
+
::card{icon="i-lucide-rocket" title="Create Your First Module" to="/docs/3.x/guide/modules/getting-started"}
|
|
16
|
+
Learn how to create your first Nuxt module using the official starter template.
|
|
17
|
+
::
|
|
18
|
+
::card{icon="i-lucide-box" title="Understand Module Structure" to="/docs/3.x/guide/modules/module-anatomy"}
|
|
19
|
+
Learn how Nuxt modules are structured and how to define them.
|
|
20
|
+
::
|
|
21
|
+
::card{icon="i-lucide-code" title="Add Plugins, Components & More" to="/docs/3.x/guide/modules/recipes-basics"}
|
|
22
|
+
Learn how to inject plugins, components, composables and server routes from your module.
|
|
23
|
+
::
|
|
24
|
+
::card{icon="i-lucide-layers" title="Use Hooks & Extend Types" to="/docs/3.x/guide/modules/recipes-advanced"}
|
|
25
|
+
Master lifecycle hooks, virtual files and TypeScript declarations in your modules.
|
|
26
|
+
::
|
|
27
|
+
::card{icon="i-lucide-test-tube" title="Test Your Module" to="/docs/3.x/guide/modules/testing"}
|
|
28
|
+
Learn how to test your Nuxt module with unit, integration and E2E tests.
|
|
29
|
+
::
|
|
30
|
+
::card{icon="i-lucide-medal" title="Follow Best Practices" to="/docs/3.x/guide/modules/best-practices"}
|
|
31
|
+
Build performant and maintainable Nuxt modules with these guidelines.
|
|
32
|
+
::
|
|
33
|
+
::card{icon="i-lucide-globe" title="Publish & Share Your Module" to="/docs/3.x/guide/modules/ecosystem"}
|
|
34
|
+
Join the Nuxt module ecosystem and publish your module to npm.
|
|
35
|
+
::
|
|
36
|
+
::
|
|
@@ -17,7 +17,7 @@ If it returns `null` or `undefined`, Nuxt will fall back to the default routes (
|
|
|
17
17
|
import type { RouterConfig } from '@nuxt/schema'
|
|
18
18
|
|
|
19
19
|
export default {
|
|
20
|
-
// https://router.vuejs.org/api/interfaces/routeroptions
|
|
20
|
+
// https://router.vuejs.org/api/interfaces/routeroptions#routes
|
|
21
21
|
routes: _routes => [
|
|
22
22
|
{
|
|
23
23
|
name: 'home',
|
|
@@ -81,7 +81,7 @@ The [Nuxt kit](/docs/3.x/guide/going-further/kit) provides a few ways [to add ro
|
|
|
81
81
|
|
|
82
82
|
## Router Options
|
|
83
83
|
|
|
84
|
-
On top of customizing options for [`vue-router`](https://router.vuejs.org/api/interfaces/routeroptions
|
|
84
|
+
On top of customizing options for [`vue-router`](https://router.vuejs.org/api/interfaces/routeroptions), Nuxt offers [additional options](/docs/3.x/api/nuxt-config#router) to customize the router.
|
|
85
85
|
|
|
86
86
|
### Using `app/router.options`
|
|
87
87
|
|
|
@@ -175,7 +175,7 @@ import type { RouterConfig } from '@nuxt/schema'
|
|
|
175
175
|
import { createMemoryHistory } from 'vue-router'
|
|
176
176
|
|
|
177
177
|
export default {
|
|
178
|
-
// https://router.vuejs.org/api/interfaces/routeroptions
|
|
178
|
+
// https://router.vuejs.org/api/interfaces/routeroptions
|
|
179
179
|
history: base => import.meta.client ? createMemoryHistory(base) : null, /* default */
|
|
180
180
|
} satisfies RouterConfig
|
|
181
181
|
```
|
|
@@ -26,6 +26,10 @@ First, we need to install the Vite plugin, for our example, we'll use `@rollup/p
|
|
|
26
26
|
bun add @rollup/plugin-yaml
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
```bash [deno]
|
|
30
|
+
deno add npm:@rollup/plugin-yaml
|
|
31
|
+
```
|
|
32
|
+
|
|
29
33
|
::
|
|
30
34
|
|
|
31
35
|
Next, we need to import and add it to our [`nuxt.config.ts`](/docs/3.x/directory-structure/nuxt-config) file:
|
|
@@ -98,7 +98,7 @@ import type { UseFetchOptions } from 'nuxt/app'
|
|
|
98
98
|
|
|
99
99
|
interface CustomError {
|
|
100
100
|
message: string
|
|
101
|
-
|
|
101
|
+
status: number
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
export function useAPI<T> (
|
|
@@ -116,7 +116,7 @@ export function useAPI<T> (
|
|
|
116
116
|
This example demonstrates how to use a custom `useFetch`, but the same structure is identical for a custom `useAsyncData`.
|
|
117
117
|
::
|
|
118
118
|
|
|
119
|
-
:link-example{to="/docs/examples/advanced/use-custom-fetch-composable"}
|
|
119
|
+
:link-example{to="/docs/3.x/examples/advanced/use-custom-fetch-composable"}
|
|
120
120
|
|
|
121
121
|
:video-accordion{title="Watch a video about custom $fetch and Repository Pattern in Nuxt" videoId="jXH8Tr-exhI"}
|
|
122
122
|
|
|
@@ -47,7 +47,7 @@ Let's create a `/api/login` API route that will accept a POST request with the e
|
|
|
47
47
|
import { z } from 'zod'
|
|
48
48
|
|
|
49
49
|
const bodySchema = z.object({
|
|
50
|
-
email: z.
|
|
50
|
+
email: z.email(),
|
|
51
51
|
password: z.string().min(8),
|
|
52
52
|
})
|
|
53
53
|
|
|
@@ -65,7 +65,7 @@ export default defineEventHandler(async (event) => {
|
|
|
65
65
|
return {}
|
|
66
66
|
}
|
|
67
67
|
throw createError({
|
|
68
|
-
|
|
68
|
+
status: 401,
|
|
69
69
|
message: 'Bad credentials',
|
|
70
70
|
})
|
|
71
71
|
})
|
|
@@ -155,7 +155,7 @@ export default defineEventHandler(async (event) => {
|
|
|
155
155
|
|
|
156
156
|
## Protect App Routes
|
|
157
157
|
|
|
158
|
-
Our data is safe with the server-side route in place, but without doing anything else, unauthenticated users would probably get some odd data when trying to access the `/users` page. We should create a [client-side middleware](https://nuxt.com/docs/
|
|
158
|
+
Our data is safe with the server-side route in place, but without doing anything else, unauthenticated users would probably get some odd data when trying to access the `/users` page. We should create a [client-side middleware](https://nuxt.com/docs/3.x/directory-structure/middleware) to protect the route on the client side and redirect users to the login page.
|
|
159
159
|
|
|
160
160
|
`nuxt-auth-utils` provides a convenient `useUserSession` composable which we'll use to check if the user is logged in, and redirect them if they are not.
|
|
161
161
|
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Events"
|
|
2
|
+
title: "Creating Custom Events"
|
|
3
3
|
description: "Nuxt provides a powerful event system powered by hookable."
|
|
4
|
+
navigation.title: "Custom Events"
|
|
4
5
|
---
|
|
5
6
|
|
|
6
|
-
# Events
|
|
7
|
-
|
|
8
7
|
Using events is a great way to decouple your application and allow for more flexible and modular communication between different parts of your code. Events can have multiple listeners that do not depend on each other. For example, you may wish to send an email to your user each time an order has shipped. Instead of coupling your order processing code to your email code, you can emit an event which a listener can receive and use to dispatch an email.
|
|
9
8
|
|
|
10
9
|
The Nuxt event system is powered by [unjs/hookable](https://github.com/unjs/hookable), which is the same library that powers the Nuxt hooks system.
|
|
@@ -58,6 +57,6 @@ await nuxtApp.callHook('app:user:registered', {
|
|
|
58
57
|
You can inspect all events using the **Nuxt DevTools** Hooks panel.
|
|
59
58
|
::
|
|
60
59
|
|
|
61
|
-
::read-more{to="/docs/guide/going-further/hooks"}
|
|
60
|
+
::read-more{to="/docs/3.x/guide/going-further/hooks"}
|
|
62
61
|
Learn more about Nuxt's built-in hooks and how to extend them
|
|
63
62
|
::
|