@nuxt/docs 0.0.0 → 3.17.0
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/.navigation.yml +2 -0
- package/1.getting-started/.navigation.yml +3 -0
- package/1.getting-started/01.introduction.md +81 -0
- package/1.getting-started/02.installation.md +109 -0
- package/1.getting-started/03.configuration.md +226 -0
- package/1.getting-started/04.views.md +163 -0
- package/1.getting-started/05.assets.md +48 -0
- package/1.getting-started/06.styling.md +565 -0
- package/1.getting-started/07.routing.md +149 -0
- package/1.getting-started/08.seo-meta.md +360 -0
- package/1.getting-started/09.transitions.md +473 -0
- package/1.getting-started/10.data-fetching.md +795 -0
- package/1.getting-started/11.state-management.md +223 -0
- package/1.getting-started/12.error-handling.md +233 -0
- package/1.getting-started/13.server.md +94 -0
- package/1.getting-started/14.layers.md +92 -0
- package/1.getting-started/15.prerendering.md +194 -0
- package/1.getting-started/16.deployment.md +130 -0
- package/1.getting-started/17.testing.md +728 -0
- package/1.getting-started/18.upgrade.md +997 -0
- package/2.guide/.navigation.yml +2 -0
- package/2.guide/0.index.md +22 -0
- package/2.guide/1.concepts/.navigation.yml +3 -0
- package/2.guide/1.concepts/1.auto-imports.md +205 -0
- package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
- package/2.guide/1.concepts/2.vuejs-development.md +103 -0
- package/2.guide/1.concepts/3.rendering.md +255 -0
- package/2.guide/1.concepts/4.server-engine.md +62 -0
- package/2.guide/1.concepts/5.modules.md +48 -0
- package/2.guide/1.concepts/7.esm.md +299 -0
- package/2.guide/1.concepts/8.typescript.md +97 -0
- package/2.guide/1.concepts/9.code-style.md +22 -0
- package/2.guide/2.directory-structure/.navigation.yml +3 -0
- package/2.guide/2.directory-structure/0.nuxt.md +20 -0
- package/2.guide/2.directory-structure/0.output.md +18 -0
- package/2.guide/2.directory-structure/1.assets.md +16 -0
- package/2.guide/2.directory-structure/1.components.md +608 -0
- package/2.guide/2.directory-structure/1.composables.md +121 -0
- package/2.guide/2.directory-structure/1.content.md +64 -0
- package/2.guide/2.directory-structure/1.layouts.md +180 -0
- package/2.guide/2.directory-structure/1.middleware.md +209 -0
- package/2.guide/2.directory-structure/1.modules.md +66 -0
- package/2.guide/2.directory-structure/1.node_modules.md +12 -0
- package/2.guide/2.directory-structure/1.pages.md +440 -0
- package/2.guide/2.directory-structure/1.plugins.md +299 -0
- package/2.guide/2.directory-structure/1.public.md +27 -0
- package/2.guide/2.directory-structure/1.server.md +546 -0
- package/2.guide/2.directory-structure/1.shared.md +104 -0
- package/2.guide/2.directory-structure/1.utils.md +49 -0
- package/2.guide/2.directory-structure/2.env.md +75 -0
- package/2.guide/2.directory-structure/2.gitignore.md +37 -0
- package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
- package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
- package/2.guide/2.directory-structure/3.app-config.md +177 -0
- package/2.guide/2.directory-structure/3.app.md +72 -0
- package/2.guide/2.directory-structure/3.error.md +55 -0
- package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
- package/2.guide/2.directory-structure/3.package.md +32 -0
- package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
- package/2.guide/3.going-further/.navigation.yml +3 -0
- package/2.guide/3.going-further/1.experimental-features.md +689 -0
- package/2.guide/3.going-further/1.features.md +103 -0
- package/2.guide/3.going-further/1.internals.md +81 -0
- package/2.guide/3.going-further/10.runtime-config.md +174 -0
- package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
- package/2.guide/3.going-further/2.hooks.md +98 -0
- package/2.guide/3.going-further/3.modules.md +811 -0
- package/2.guide/3.going-further/4.kit.md +51 -0
- package/2.guide/3.going-further/6.nuxt-app.md +64 -0
- package/2.guide/3.going-further/7.layers.md +227 -0
- package/2.guide/3.going-further/9.debugging.md +115 -0
- package/2.guide/3.going-further/index.md +4 -0
- package/2.guide/4.recipes/.navigation.yml +3 -0
- package/2.guide/4.recipes/1.custom-routing.md +181 -0
- package/2.guide/4.recipes/2.vite-plugin.md +65 -0
- package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
- package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
- package/3.api/.navigation.yml +3 -0
- package/3.api/1.components/.navigation.yml +3 -0
- package/3.api/1.components/1.client-only.md +76 -0
- package/3.api/1.components/1.dev-only.md +51 -0
- package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
- package/3.api/1.components/10.nuxt-picture.md +27 -0
- package/3.api/1.components/11.teleports.md +40 -0
- package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
- package/3.api/1.components/13.nuxt-time.md +173 -0
- package/3.api/1.components/2.nuxt-page.md +154 -0
- package/3.api/1.components/3.nuxt-layout.md +156 -0
- package/3.api/1.components/4.nuxt-link.md +322 -0
- package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
- package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
- package/3.api/1.components/7.nuxt-welcome.md +25 -0
- package/3.api/1.components/8.nuxt-island.md +70 -0
- package/3.api/1.components/9.nuxt-img.md +43 -0
- package/3.api/2.composables/.navigation.yml +3 -0
- package/3.api/2.composables/on-prehydrate.md +60 -0
- package/3.api/2.composables/use-app-config.md +19 -0
- package/3.api/2.composables/use-async-data.md +212 -0
- package/3.api/2.composables/use-cookie.md +233 -0
- package/3.api/2.composables/use-error.md +32 -0
- package/3.api/2.composables/use-fetch.md +217 -0
- package/3.api/2.composables/use-head-safe.md +55 -0
- package/3.api/2.composables/use-head.md +69 -0
- package/3.api/2.composables/use-hydration.md +68 -0
- package/3.api/2.composables/use-lazy-async-data.md +47 -0
- package/3.api/2.composables/use-lazy-fetch.md +55 -0
- package/3.api/2.composables/use-loading-indicator.md +77 -0
- package/3.api/2.composables/use-nuxt-app.md +294 -0
- package/3.api/2.composables/use-nuxt-data.md +112 -0
- package/3.api/2.composables/use-preview-mode.md +118 -0
- package/3.api/2.composables/use-request-event.md +23 -0
- package/3.api/2.composables/use-request-fetch.md +52 -0
- package/3.api/2.composables/use-request-header.md +34 -0
- package/3.api/2.composables/use-request-headers.md +37 -0
- package/3.api/2.composables/use-request-url.md +41 -0
- package/3.api/2.composables/use-response-header.md +48 -0
- package/3.api/2.composables/use-route-announcer.md +60 -0
- package/3.api/2.composables/use-route.md +52 -0
- package/3.api/2.composables/use-router.md +92 -0
- package/3.api/2.composables/use-runtime-config.md +142 -0
- package/3.api/2.composables/use-runtime-hook.md +43 -0
- package/3.api/2.composables/use-seo-meta.md +80 -0
- package/3.api/2.composables/use-server-seo-meta.md +27 -0
- package/3.api/2.composables/use-state.md +48 -0
- package/3.api/3.utils/$fetch.md +98 -0
- package/3.api/3.utils/.navigation.yml +3 -0
- package/3.api/3.utils/abort-navigation.md +73 -0
- package/3.api/3.utils/add-route-middleware.md +88 -0
- package/3.api/3.utils/call-once.md +92 -0
- package/3.api/3.utils/clear-error.md +29 -0
- package/3.api/3.utils/clear-nuxt-data.md +23 -0
- package/3.api/3.utils/clear-nuxt-state.md +23 -0
- package/3.api/3.utils/create-error.md +55 -0
- package/3.api/3.utils/define-nuxt-component.md +53 -0
- package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
- package/3.api/3.utils/define-page-meta.md +234 -0
- package/3.api/3.utils/define-route-rules.md +52 -0
- package/3.api/3.utils/navigate-to.md +230 -0
- package/3.api/3.utils/on-before-route-leave.md +11 -0
- package/3.api/3.utils/on-before-route-update.md +11 -0
- package/3.api/3.utils/on-nuxt-ready.md +25 -0
- package/3.api/3.utils/prefetch-components.md +28 -0
- package/3.api/3.utils/preload-components.md +23 -0
- package/3.api/3.utils/preload-route-components.md +41 -0
- package/3.api/3.utils/prerender-routes.md +46 -0
- package/3.api/3.utils/refresh-cookie.md +46 -0
- package/3.api/3.utils/refresh-nuxt-data.md +91 -0
- package/3.api/3.utils/reload-nuxt-app.md +74 -0
- package/3.api/3.utils/set-page-layout.md +24 -0
- package/3.api/3.utils/set-response-status.md +36 -0
- package/3.api/3.utils/show-error.md +31 -0
- package/3.api/3.utils/update-app-config.md +27 -0
- package/3.api/4.commands/.navigation.yml +3 -0
- package/3.api/4.commands/add.md +112 -0
- package/3.api/4.commands/analyze.md +41 -0
- package/3.api/4.commands/build-module.md +42 -0
- package/3.api/4.commands/build.md +46 -0
- package/3.api/4.commands/cleanup.md +38 -0
- package/3.api/4.commands/dev.md +59 -0
- package/3.api/4.commands/devtools.md +38 -0
- package/3.api/4.commands/generate.md +41 -0
- package/3.api/4.commands/info.md +33 -0
- package/3.api/4.commands/init.md +46 -0
- package/3.api/4.commands/module.md +84 -0
- package/3.api/4.commands/prepare.md +36 -0
- package/3.api/4.commands/preview.md +43 -0
- package/3.api/4.commands/typecheck.md +42 -0
- package/3.api/4.commands/upgrade.md +37 -0
- package/3.api/5.kit/.navigation.yml +3 -0
- package/3.api/5.kit/1.modules.md +172 -0
- package/3.api/5.kit/10.runtime-config.md +27 -0
- package/3.api/5.kit/10.templates.md +283 -0
- package/3.api/5.kit/11.nitro.md +409 -0
- package/3.api/5.kit/12.resolving.md +268 -0
- package/3.api/5.kit/13.logging.md +65 -0
- package/3.api/5.kit/14.builder.md +491 -0
- package/3.api/5.kit/15.examples.md +41 -0
- package/3.api/5.kit/2.programmatic.md +125 -0
- package/3.api/5.kit/3.compatibility.md +230 -0
- package/3.api/5.kit/4.autoimports.md +144 -0
- package/3.api/5.kit/5.components.md +127 -0
- package/3.api/5.kit/6.context.md +130 -0
- package/3.api/5.kit/7.pages.md +295 -0
- package/3.api/5.kit/8.layout.md +80 -0
- package/3.api/5.kit/9.plugins.md +263 -0
- package/3.api/6.advanced/.navigation.yml +1 -0
- package/3.api/6.advanced/1.hooks.md +105 -0
- package/3.api/6.advanced/2.import-meta.md +60 -0
- package/3.api/6.nuxt-config.md +12 -0
- package/3.api/index.md +31 -0
- package/5.community/.navigation.yml +3 -0
- package/5.community/2.getting-help.md +48 -0
- package/5.community/3.reporting-bugs.md +50 -0
- package/5.community/4.contribution.md +205 -0
- package/5.community/5.framework-contribution.md +142 -0
- package/5.community/6.roadmap.md +79 -0
- package/5.community/7.changelog.md +92 -0
- package/6.bridge/.navigation.yml +3 -0
- package/6.bridge/1.overview.md +137 -0
- package/6.bridge/10.configuration.md +96 -0
- package/6.bridge/2.typescript.md +46 -0
- package/6.bridge/3.bridge-composition-api.md +132 -0
- package/6.bridge/4.plugins-and-middleware.md +65 -0
- package/6.bridge/5.nuxt3-compatible-api.md +204 -0
- package/6.bridge/6.meta.md +117 -0
- package/6.bridge/7.runtime-config.md +38 -0
- package/6.bridge/8.nitro.md +102 -0
- package/6.bridge/9.vite.md +37 -0
- package/7.migration/.navigation.yml +3 -0
- package/7.migration/1.overview.md +24 -0
- package/7.migration/10.bundling.md +28 -0
- package/7.migration/11.server.md +17 -0
- package/7.migration/2.configuration.md +240 -0
- package/7.migration/20.module-authors.md +94 -0
- package/7.migration/3.auto-imports.md +18 -0
- package/7.migration/4.meta.md +127 -0
- package/7.migration/5.plugins-and-middleware.md +80 -0
- package/7.migration/6.pages-and-layouts.md +233 -0
- package/7.migration/7.component-options.md +156 -0
- package/7.migration/8.runtime-config.md +58 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/package.json +16 -4
- package/dist/.gitkeep +0 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'composables'
|
|
3
|
+
head.title: 'composables/'
|
|
4
|
+
description: Use the composables/ directory to auto-import your Vue composables into your application.
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
**Method 1:** Using named export
|
|
11
|
+
|
|
12
|
+
```js [composables/useFoo.ts]
|
|
13
|
+
export const useFoo = () => {
|
|
14
|
+
return useState('foo', () => 'bar')
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Method 2:** Using default export
|
|
19
|
+
|
|
20
|
+
```js [composables/use-foo.ts or composables/useFoo.ts]
|
|
21
|
+
// It will be available as useFoo() (camelCase of file name without extension)
|
|
22
|
+
export default function () {
|
|
23
|
+
return useState('foo', () => 'bar')
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Usage:** You can now use auto imported composable in `.js`, `.ts` and `.vue` files
|
|
28
|
+
|
|
29
|
+
```vue [app.vue]
|
|
30
|
+
<script setup lang="ts">
|
|
31
|
+
const foo = useFoo()
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<template>
|
|
35
|
+
<div>
|
|
36
|
+
{{ foo }}
|
|
37
|
+
</div>
|
|
38
|
+
</template>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
::note
|
|
42
|
+
The `composables/` directory in Nuxt does not provide any additional reactivity capabilities to your code. Instead, any reactivity within composables is achieved using Vue's Composition API mechanisms, such as ref and reactive. Note that reactive code is also not limited to the boundaries of the `composables/` directory. You are free to employ reactivity features wherever they're needed in your application.
|
|
43
|
+
::
|
|
44
|
+
|
|
45
|
+
:read-more{to="/docs/guide/concepts/auto-imports"}
|
|
46
|
+
|
|
47
|
+
:link-example{to="/docs/examples/features/auto-imports"}
|
|
48
|
+
|
|
49
|
+
## Types
|
|
50
|
+
|
|
51
|
+
Under the hood, Nuxt auto generates the file `.nuxt/imports.d.ts` to declare the types.
|
|
52
|
+
|
|
53
|
+
Be aware that you have to run [`nuxi prepare`](/docs/api/commands/prepare), [`nuxi dev`](/docs/api/commands/dev) or [`nuxi build`](/docs/api/commands/build) in order to let Nuxt generate the types.
|
|
54
|
+
|
|
55
|
+
::note
|
|
56
|
+
If you create a composable without having the dev server running, TypeScript will throw an error, such as `Cannot find name 'useBar'.`
|
|
57
|
+
::
|
|
58
|
+
|
|
59
|
+
## Examples
|
|
60
|
+
|
|
61
|
+
### Nested Composables
|
|
62
|
+
|
|
63
|
+
You can use a composable within another composable using auto imports:
|
|
64
|
+
|
|
65
|
+
```js [composables/test.ts]
|
|
66
|
+
export const useFoo = () => {
|
|
67
|
+
const nuxtApp = useNuxtApp()
|
|
68
|
+
const bar = useBar()
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Access plugin injections
|
|
73
|
+
|
|
74
|
+
You can access [plugin injections](/docs/guide/directory-structure/plugins#providing-helpers) from composables:
|
|
75
|
+
|
|
76
|
+
```js [composables/test.ts]
|
|
77
|
+
export const useHello = () => {
|
|
78
|
+
const nuxtApp = useNuxtApp()
|
|
79
|
+
return nuxtApp.$hello
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## How Files Are Scanned
|
|
84
|
+
|
|
85
|
+
Nuxt only scans files at the top level of the [`composables/` directory](/docs/guide/directory-structure/composables), e.g.:
|
|
86
|
+
|
|
87
|
+
```bash [Directory Structure]
|
|
88
|
+
-| composables/
|
|
89
|
+
---| index.ts // scanned
|
|
90
|
+
---| useFoo.ts // scanned
|
|
91
|
+
---| nested/
|
|
92
|
+
-----| utils.ts // not scanned
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Only `composables/index.ts` and `composables/useFoo.ts` would be searched for imports.
|
|
96
|
+
|
|
97
|
+
To get auto imports working for nested modules, you could either re-export them (recommended) or configure the scanner to include nested directories:
|
|
98
|
+
|
|
99
|
+
**Example:** Re-export the composables you need from the `composables/index.ts` file:
|
|
100
|
+
|
|
101
|
+
```ts [composables/index.ts]
|
|
102
|
+
// Enables auto import for this export
|
|
103
|
+
export { utils } from './nested/utils.ts'
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**Example:** Scan nested directories inside the `composables/` folder:
|
|
107
|
+
|
|
108
|
+
```ts twoslash [nuxt.config.ts]
|
|
109
|
+
export default defineNuxtConfig({
|
|
110
|
+
imports: {
|
|
111
|
+
dirs: [
|
|
112
|
+
// Scan top-level modules
|
|
113
|
+
'composables',
|
|
114
|
+
// ... or scan modules nested one level deep with a specific name and file extension
|
|
115
|
+
'composables/*/index.{ts,js,mjs,mts}',
|
|
116
|
+
// ... or scan all modules within given directory
|
|
117
|
+
'composables/**'
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
```
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'content'
|
|
3
|
+
head.title: 'content/'
|
|
4
|
+
description: Use the content/ directory to create a file-based CMS for your application.
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
[Nuxt Content](https://content.nuxt.com) reads the [`content/` directory](/docs/guide/directory-structure/content) in your project and parses `.md`, `.yml`, `.csv` and `.json` files to create a file-based CMS for your application.
|
|
9
|
+
|
|
10
|
+
- Render your content with built-in components.
|
|
11
|
+
- Query your content with a MongoDB-like API.
|
|
12
|
+
- Use your Vue components in Markdown files with the MDC syntax.
|
|
13
|
+
- Automatically generate your navigation.
|
|
14
|
+
|
|
15
|
+
::read-more{to="https://content.nuxt.com" target="_blank"}
|
|
16
|
+
Learn more in **Nuxt Content** documentation.
|
|
17
|
+
::
|
|
18
|
+
|
|
19
|
+
## Enable Nuxt Content
|
|
20
|
+
|
|
21
|
+
Install the `@nuxt/content` module in your project as well as adding it to your `nuxt.config.ts` with one command:
|
|
22
|
+
|
|
23
|
+
```bash [Terminal]
|
|
24
|
+
npx nuxi module add content
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Create Content
|
|
28
|
+
|
|
29
|
+
Place your markdown files inside the `content/` directory:
|
|
30
|
+
|
|
31
|
+
```md [content/index.md]
|
|
32
|
+
# Hello Content
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The module automatically loads and parses them.
|
|
36
|
+
|
|
37
|
+
## Render Content
|
|
38
|
+
|
|
39
|
+
To render content pages, add a [catch-all route](/docs/guide/directory-structure/pages/#catch-all-route) using the [`<ContentRenderer>`](https://content.nuxt.com/docs/components/content-renderer) component:
|
|
40
|
+
|
|
41
|
+
```vue [pages/[...slug\\].vue]
|
|
42
|
+
<script lang="ts" setup>
|
|
43
|
+
const route = useRoute()
|
|
44
|
+
const { data: page } = await useAsyncData(route.path, () => {
|
|
45
|
+
return queryCollection('content').path(route.path).first()
|
|
46
|
+
})
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<template>
|
|
50
|
+
<div>
|
|
51
|
+
<header><!-- ... --></header>
|
|
52
|
+
|
|
53
|
+
<ContentRenderer v-if="page" :value="page" />
|
|
54
|
+
|
|
55
|
+
<footer><!-- ... --></footer>
|
|
56
|
+
</div>
|
|
57
|
+
</template>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Documentation
|
|
61
|
+
|
|
62
|
+
::tip{ icon="i-lucide-book" }
|
|
63
|
+
Head over to <https://content.nuxt.com> to learn more about the Content module features, such as how to build queries and use Vue components in your Markdown files with the MDC syntax.
|
|
64
|
+
::
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "layouts"
|
|
3
|
+
head.title: "layouts/"
|
|
4
|
+
description: "Nuxt provides a layouts framework to extract common UI patterns into reusable layouts."
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
::tip{icon="i-lucide-rocket" }
|
|
9
|
+
For best performance, components placed in this directory will be automatically loaded via asynchronous import when used.
|
|
10
|
+
::
|
|
11
|
+
|
|
12
|
+
## Enable Layouts
|
|
13
|
+
|
|
14
|
+
Layouts are enabled by adding [`<NuxtLayout>`](/docs/api/components/nuxt-layout) to your [`app.vue`](/docs/guide/directory-structure/app):
|
|
15
|
+
|
|
16
|
+
```vue [app.vue]
|
|
17
|
+
<template>
|
|
18
|
+
<NuxtLayout>
|
|
19
|
+
<NuxtPage />
|
|
20
|
+
</NuxtLayout>
|
|
21
|
+
</template>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
To use a layout:
|
|
25
|
+
- Set a `layout` property in your page with [definePageMeta](/docs/api/utils/define-page-meta).
|
|
26
|
+
- Set the `name` prop of `<NuxtLayout>`.
|
|
27
|
+
|
|
28
|
+
::note
|
|
29
|
+
The layout name is normalized to kebab-case, so `someLayout` becomes `some-layout`.
|
|
30
|
+
::
|
|
31
|
+
|
|
32
|
+
::note
|
|
33
|
+
If no layout is specified, `layouts/default.vue` will be used.
|
|
34
|
+
::
|
|
35
|
+
|
|
36
|
+
::important
|
|
37
|
+
If you only have a single layout in your application, we recommend using [`app.vue`](/docs/guide/directory-structure/app) instead.
|
|
38
|
+
::
|
|
39
|
+
|
|
40
|
+
::important
|
|
41
|
+
Unlike other components, your layouts must have a single root element to allow Nuxt to apply transitions between layout changes - and this root element cannot be a `<slot />`.
|
|
42
|
+
::
|
|
43
|
+
|
|
44
|
+
## Default Layout
|
|
45
|
+
|
|
46
|
+
Add a `~/layouts/default.vue`:
|
|
47
|
+
|
|
48
|
+
```vue [layouts/default.vue]
|
|
49
|
+
<template>
|
|
50
|
+
<div>
|
|
51
|
+
<p>Some default layout content shared across all pages</p>
|
|
52
|
+
<slot />
|
|
53
|
+
</div>
|
|
54
|
+
</template>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
In a layout file, the content of the page will be displayed in the `<slot />` component.
|
|
58
|
+
|
|
59
|
+
## Named Layout
|
|
60
|
+
|
|
61
|
+
```bash [Directory Structure]
|
|
62
|
+
-| layouts/
|
|
63
|
+
---| default.vue
|
|
64
|
+
---| custom.vue
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Then you can use the `custom` layout in your page:
|
|
68
|
+
|
|
69
|
+
```vue twoslash [pages/about.vue]
|
|
70
|
+
<script setup lang="ts">
|
|
71
|
+
definePageMeta({
|
|
72
|
+
layout: 'custom'
|
|
73
|
+
})
|
|
74
|
+
</script>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
::read-more{to="/docs/guide/directory-structure/pages#page-metadata"}
|
|
78
|
+
Learn more about `definePageMeta`.
|
|
79
|
+
::
|
|
80
|
+
|
|
81
|
+
You can directly override the default layout for all pages using the `name` property of [`<NuxtLayout>`](/docs/api/components/nuxt-layout):
|
|
82
|
+
|
|
83
|
+
```vue [app.vue]
|
|
84
|
+
<script setup lang="ts">
|
|
85
|
+
// You might choose this based on an API call or logged-in status
|
|
86
|
+
const layout = "custom";
|
|
87
|
+
</script>
|
|
88
|
+
|
|
89
|
+
<template>
|
|
90
|
+
<NuxtLayout :name="layout">
|
|
91
|
+
<NuxtPage />
|
|
92
|
+
</NuxtLayout>
|
|
93
|
+
</template>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
If you have a layout in nested directories, the layout's name will be based on its own path directory and filename, with duplicate segments being removed.
|
|
97
|
+
|
|
98
|
+
File | Layout Name
|
|
99
|
+
-- | --
|
|
100
|
+
`~/layouts/desktop/default.vue` | `desktop-default`
|
|
101
|
+
`~/layouts/desktop-base/base.vue` | `desktop-base`
|
|
102
|
+
`~/layouts/desktop/index.vue` | `desktop`
|
|
103
|
+
|
|
104
|
+
For clarity, we recommend that the layout's filename matches its name:
|
|
105
|
+
|
|
106
|
+
File | Layout Name
|
|
107
|
+
-- | --
|
|
108
|
+
`~/layouts/desktop/DesktopDefault.vue` | `desktop-default`
|
|
109
|
+
`~/layouts/desktop-base/DesktopBase.vue` | `desktop-base`
|
|
110
|
+
`~/layouts/desktop/Desktop.vue` | `desktop`
|
|
111
|
+
|
|
112
|
+
:link-example{to="/docs/examples/features/layouts"}
|
|
113
|
+
|
|
114
|
+
## Changing the Layout Dynamically
|
|
115
|
+
|
|
116
|
+
You can also use the [`setPageLayout`](/docs/api/utils/set-page-layout) helper to change the layout dynamically:
|
|
117
|
+
|
|
118
|
+
```vue twoslash
|
|
119
|
+
<script setup lang="ts">
|
|
120
|
+
function enableCustomLayout () {
|
|
121
|
+
setPageLayout('custom')
|
|
122
|
+
}
|
|
123
|
+
definePageMeta({
|
|
124
|
+
layout: false,
|
|
125
|
+
});
|
|
126
|
+
</script>
|
|
127
|
+
|
|
128
|
+
<template>
|
|
129
|
+
<div>
|
|
130
|
+
<button @click="enableCustomLayout">Update layout</button>
|
|
131
|
+
</div>
|
|
132
|
+
</template>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
:link-example{to="/docs/examples/features/layouts"}
|
|
136
|
+
|
|
137
|
+
## Overriding a Layout on a Per-page Basis
|
|
138
|
+
|
|
139
|
+
If you are using pages, you can take full control by setting `layout: false` and then using the `<NuxtLayout>` component within the page.
|
|
140
|
+
|
|
141
|
+
::code-group
|
|
142
|
+
|
|
143
|
+
```vue [pages/index.vue]
|
|
144
|
+
<script setup lang="ts">
|
|
145
|
+
definePageMeta({
|
|
146
|
+
layout: false,
|
|
147
|
+
})
|
|
148
|
+
</script>
|
|
149
|
+
|
|
150
|
+
<template>
|
|
151
|
+
<div>
|
|
152
|
+
<NuxtLayout name="custom">
|
|
153
|
+
<template #header> Some header template content. </template>
|
|
154
|
+
|
|
155
|
+
The rest of the page
|
|
156
|
+
</NuxtLayout>
|
|
157
|
+
</div>
|
|
158
|
+
</template>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```vue [layouts/custom.vue]
|
|
162
|
+
<template>
|
|
163
|
+
<div>
|
|
164
|
+
<header>
|
|
165
|
+
<slot name="header">
|
|
166
|
+
Default header content
|
|
167
|
+
</slot>
|
|
168
|
+
</header>
|
|
169
|
+
<main>
|
|
170
|
+
<slot />
|
|
171
|
+
</main>
|
|
172
|
+
</div>
|
|
173
|
+
</template>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
::
|
|
177
|
+
|
|
178
|
+
::important
|
|
179
|
+
If you use `<NuxtLayout>` within your pages, make sure it is not the root element (or [disable layout/page transitions](/docs/getting-started/transitions#disable-transitions)).
|
|
180
|
+
::
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "middleware"
|
|
3
|
+
description: "Nuxt provides middleware to run code before navigating to a particular route."
|
|
4
|
+
head.title: "middleware/"
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Nuxt provides a customizable **route middleware** framework you can use throughout your application, ideal for extracting code that you want to run before navigating to a particular route.
|
|
9
|
+
|
|
10
|
+
There are three kinds of route middleware:
|
|
11
|
+
|
|
12
|
+
1. Anonymous (or inline) route middleware are defined directly within the page.
|
|
13
|
+
2. Named route middleware, placed in the `middleware/` and automatically loaded via asynchronous import when used on a page.
|
|
14
|
+
3. Global route middleware, placed in the `middleware/` with a `.global` suffix and is run on every route change.
|
|
15
|
+
|
|
16
|
+
The first two kinds of route middleware can be defined in [`definePageMeta`](/docs/api/utils/define-page-meta).
|
|
17
|
+
|
|
18
|
+
::note
|
|
19
|
+
Name of middleware are normalized to kebab-case: `myMiddleware` becomes `my-middleware`.
|
|
20
|
+
::
|
|
21
|
+
|
|
22
|
+
::note
|
|
23
|
+
Route middleware run within the Vue part of your Nuxt app. Despite the similar name, they are completely different from [server middleware](/docs/guide/directory-structure/server#server-middleware), which are run in the Nitro server part of your app.
|
|
24
|
+
::
|
|
25
|
+
|
|
26
|
+
:video-accordion{title="Watch a video from Vue School on all 3 kinds of middleware" videoId="761471577" platform="vimeo"}
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Route middleware are navigation guards that receive the current route and the next route as arguments.
|
|
31
|
+
|
|
32
|
+
```ts twoslash [middleware/my-middleware.ts]
|
|
33
|
+
export default defineNuxtRouteMiddleware((to, from) => {
|
|
34
|
+
if (to.params.id === '1') {
|
|
35
|
+
return abortNavigation()
|
|
36
|
+
}
|
|
37
|
+
// In a real app you would probably not redirect every route to `/`
|
|
38
|
+
// however it is important to check `to.path` before redirecting or you
|
|
39
|
+
// might get an infinite redirect loop
|
|
40
|
+
if (to.path !== '/') {
|
|
41
|
+
return navigateTo('/')
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Nuxt provides two globally available helpers that can be returned directly from the middleware.
|
|
47
|
+
|
|
48
|
+
1. [`navigateTo`](/docs/api/utils/navigate-to) - Redirects to the given route
|
|
49
|
+
2. [`abortNavigation`](/docs/api/utils/abort-navigation) - Aborts the navigation, with an optional error message.
|
|
50
|
+
|
|
51
|
+
Unlike [navigation guards](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) from `vue-router`, a third `next()` argument is not passed, and **redirect or route cancellation is handled by returning a value from the middleware**.
|
|
52
|
+
|
|
53
|
+
Possible return values are:
|
|
54
|
+
|
|
55
|
+
* nothing (a simple `return` or no return at all) - does not block navigation and will move to the next middleware function, if any, or complete the route navigation
|
|
56
|
+
* `return navigateTo('/')` - redirects to the given path and will set the redirect code to [`302` Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) if the redirect happens on the server side
|
|
57
|
+
* `return navigateTo('/', { redirectCode: 301 })` - redirects to the given path and will set the redirect code to [`301` Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301) if the redirect happens on the server side
|
|
58
|
+
* `return abortNavigation()` - stops the current navigation
|
|
59
|
+
* `return abortNavigation(error)` - rejects the current navigation with an error
|
|
60
|
+
|
|
61
|
+
:read-more{to="/docs/api/utils/navigate-to"}
|
|
62
|
+
:read-more{to="/docs/api/utils/abort-navigation"}
|
|
63
|
+
|
|
64
|
+
::important
|
|
65
|
+
We recommend using the helper functions above for performing redirects or stopping navigation. Other possible return values described in [the vue-router docs](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) may work but there may be breaking changes in future.
|
|
66
|
+
::
|
|
67
|
+
|
|
68
|
+
## Middleware Order
|
|
69
|
+
|
|
70
|
+
Middleware runs in the following order:
|
|
71
|
+
|
|
72
|
+
1. Global Middleware
|
|
73
|
+
2. Page defined middleware order (if there are multiple middleware declared with the array syntax)
|
|
74
|
+
|
|
75
|
+
For example, assuming you have the following middleware and component:
|
|
76
|
+
|
|
77
|
+
```bash [middleware/ directory]
|
|
78
|
+
-| middleware/
|
|
79
|
+
---| analytics.global.ts
|
|
80
|
+
---| setup.global.ts
|
|
81
|
+
---| auth.ts
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```vue twoslash [pages/profile.vue]
|
|
85
|
+
<script setup lang="ts">
|
|
86
|
+
definePageMeta({
|
|
87
|
+
middleware: [
|
|
88
|
+
function (to, from) {
|
|
89
|
+
// Custom inline middleware
|
|
90
|
+
},
|
|
91
|
+
'auth',
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
</script>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
You can expect the middleware to be run in the following order:
|
|
98
|
+
|
|
99
|
+
1. `analytics.global.ts`
|
|
100
|
+
2. `setup.global.ts`
|
|
101
|
+
3. Custom inline middleware
|
|
102
|
+
4. `auth.ts`
|
|
103
|
+
|
|
104
|
+
### Ordering Global Middleware
|
|
105
|
+
|
|
106
|
+
By default, global middleware is executed alphabetically based on the filename.
|
|
107
|
+
|
|
108
|
+
However, there may be times you want to define a specific order. For example, in the last scenario, `setup.global.ts` may need to run before `analytics.global.ts`. In that case, we recommend prefixing global middleware with 'alphabetical' numbering.
|
|
109
|
+
|
|
110
|
+
```bash [Directory structure]
|
|
111
|
+
-| middleware/
|
|
112
|
+
---| 01.setup.global.ts
|
|
113
|
+
---| 02.analytics.global.ts
|
|
114
|
+
---| auth.ts
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
::note
|
|
118
|
+
In case you're new to 'alphabetical' numbering, remember that filenames are sorted as strings, not as numeric values. For example, `10.new.global.ts` would come before `2.new.global.ts`. This is why the example prefixes single digit numbers with `0`.
|
|
119
|
+
::
|
|
120
|
+
|
|
121
|
+
## When Middleware Runs
|
|
122
|
+
|
|
123
|
+
If your site is server-rendered or generated, middleware for the initial page will be executed both when the page is rendered and then again on the client. This might be needed if your middleware needs a browser environment, such as if you have a generated site, aggressively cache responses, or want to read a value from local storage.
|
|
124
|
+
|
|
125
|
+
However, if you want to avoid this behaviour you can do so:
|
|
126
|
+
|
|
127
|
+
```ts twoslash [middleware/example.ts]
|
|
128
|
+
export default defineNuxtRouteMiddleware(to => {
|
|
129
|
+
// skip middleware on server
|
|
130
|
+
if (import.meta.server) return
|
|
131
|
+
// skip middleware on client side entirely
|
|
132
|
+
if (import.meta.client) return
|
|
133
|
+
// or only skip middleware on initial client load
|
|
134
|
+
const nuxtApp = useNuxtApp()
|
|
135
|
+
if (import.meta.client && nuxtApp.isHydrating && nuxtApp.payload.serverRendered) return
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
This is true even if you throw an error in your middleware on the server, and an error page is rendered. The middleware will still run again in the browser.
|
|
140
|
+
|
|
141
|
+
::note
|
|
142
|
+
Rendering an error page is an entirely separate page load, meaning any registered middleware will run again. You can use [`useError`](/docs/getting-started/error-handling#useerror) in middleware to check if an error is being handled.
|
|
143
|
+
::
|
|
144
|
+
|
|
145
|
+
## Adding Middleware Dynamically
|
|
146
|
+
|
|
147
|
+
It is possible to add global or named route middleware manually using the [`addRouteMiddleware()`](/docs/api/utils/add-route-middleware) helper function, such as from within a plugin.
|
|
148
|
+
|
|
149
|
+
```ts twoslash
|
|
150
|
+
export default defineNuxtPlugin(() => {
|
|
151
|
+
addRouteMiddleware('global-test', () => {
|
|
152
|
+
console.log('this global middleware was added in a plugin and will be run on every route change')
|
|
153
|
+
}, { global: true })
|
|
154
|
+
|
|
155
|
+
addRouteMiddleware('named-test', () => {
|
|
156
|
+
console.log('this named middleware was added in a plugin and would override any existing middleware of the same name')
|
|
157
|
+
})
|
|
158
|
+
})
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Example
|
|
162
|
+
|
|
163
|
+
```bash [Directory Structure]
|
|
164
|
+
-| middleware/
|
|
165
|
+
---| auth.ts
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
In your page file, you can reference this route middleware:
|
|
169
|
+
|
|
170
|
+
```vue twoslash
|
|
171
|
+
<script setup lang="ts">
|
|
172
|
+
definePageMeta({
|
|
173
|
+
middleware: ["auth"]
|
|
174
|
+
// or middleware: 'auth'
|
|
175
|
+
})
|
|
176
|
+
</script>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Now, before navigation to that page can complete, the `auth` route middleware will be run.
|
|
180
|
+
|
|
181
|
+
:link-example{to="/docs/examples/routing/middleware"}
|
|
182
|
+
|
|
183
|
+
## Setting Middleware at Build Time
|
|
184
|
+
|
|
185
|
+
Instead of using `definePageMeta` on each page, you can add named route middleware within the `pages:extend` hook.
|
|
186
|
+
|
|
187
|
+
```ts twoslash [nuxt.config.ts]
|
|
188
|
+
import type { NuxtPage } from 'nuxt/schema'
|
|
189
|
+
|
|
190
|
+
export default defineNuxtConfig({
|
|
191
|
+
hooks: {
|
|
192
|
+
'pages:extend' (pages) {
|
|
193
|
+
function setMiddleware (pages: NuxtPage[]) {
|
|
194
|
+
for (const page of pages) {
|
|
195
|
+
if (/* some condition */ true) {
|
|
196
|
+
page.meta ||= {}
|
|
197
|
+
// Note that this will override any middleware set in `definePageMeta` in the page
|
|
198
|
+
page.meta.middleware = ['named']
|
|
199
|
+
}
|
|
200
|
+
if (page.children) {
|
|
201
|
+
setMiddleware(page.children)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
setMiddleware(pages)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
```
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'modules'
|
|
3
|
+
head.title: 'modules/'
|
|
4
|
+
description: Use the modules/ directory to automatically register local modules within your application.
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
It is a good place to place any local modules you develop while building your application.
|
|
9
|
+
|
|
10
|
+
The auto-registered files patterns are:
|
|
11
|
+
- `modules/*/index.ts`
|
|
12
|
+
- `modules/*.ts`
|
|
13
|
+
|
|
14
|
+
You don't need to add those local modules to your [`nuxt.config.ts`](/docs/guide/directory-structure/nuxt-config) separately.
|
|
15
|
+
|
|
16
|
+
::code-group
|
|
17
|
+
|
|
18
|
+
```ts twoslash [modules/hello/index.ts]
|
|
19
|
+
// `nuxt/kit` is a helper subpath import you can use when defining local modules
|
|
20
|
+
// that means you do not need to add `@nuxt/kit` to your project's dependencies
|
|
21
|
+
import { createResolver, defineNuxtModule, addServerHandler } from 'nuxt/kit'
|
|
22
|
+
|
|
23
|
+
export default defineNuxtModule({
|
|
24
|
+
meta: {
|
|
25
|
+
name: 'hello'
|
|
26
|
+
},
|
|
27
|
+
setup () {
|
|
28
|
+
const resolver = createResolver(import.meta.url)
|
|
29
|
+
|
|
30
|
+
// Add an API route
|
|
31
|
+
addServerHandler({
|
|
32
|
+
route: '/api/hello',
|
|
33
|
+
handler: resolver.resolve('./runtime/api-route')
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```ts twoslash [modules/hello/runtime/api-route.ts]
|
|
40
|
+
export default defineEventHandler(() => {
|
|
41
|
+
return { hello: 'world' }
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
::
|
|
46
|
+
|
|
47
|
+
When starting Nuxt, the `hello` module will be registered and the `/api/hello` route will be available.
|
|
48
|
+
|
|
49
|
+
Modules are executed in the following sequence:
|
|
50
|
+
- First, the modules defined in [`nuxt.config.ts`](/docs/api/nuxt-config#modules-1) are loaded.
|
|
51
|
+
- Then, modules found in the `modules/` directory are executed, and they load in alphabetical order.
|
|
52
|
+
|
|
53
|
+
You can change the order of local module by adding a number to the front of each directory name:
|
|
54
|
+
|
|
55
|
+
```bash [Directory structure]
|
|
56
|
+
modules/
|
|
57
|
+
1.first-module/
|
|
58
|
+
index.ts
|
|
59
|
+
2.second-module.ts
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
:read-more{to="/docs/guide/going-further/modules"}
|
|
63
|
+
|
|
64
|
+
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/creating-your-first-module-from-scratch?friend=nuxt" target="_blank"}
|
|
65
|
+
Watch Vue School video about Nuxt private modules.
|
|
66
|
+
::
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "node_modules"
|
|
3
|
+
description: "The package manager stores the dependencies of your project in the node_modules/ directory."
|
|
4
|
+
head.title: "node_modules/"
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
The package manager ([`npm`](https://docs.npmjs.com/cli/commands/npm) or [`yarn`](https://yarnpkg.com) or [`pnpm`](https://pnpm.io/cli/install) or [`bun`](https://bun.sh/package-manager)) creates this directory to store the dependencies of your project.
|
|
9
|
+
|
|
10
|
+
::important
|
|
11
|
+
This directory should be added to your [`.gitignore`](/docs/guide/directory-structure/gitignore) file to avoid pushing the dependencies to your repository.
|
|
12
|
+
::
|