@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,608 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "components"
|
|
3
|
+
head.title: "components/"
|
|
4
|
+
description: "The components/ directory is where you put all your Vue components."
|
|
5
|
+
navigation.icon: i-lucide-folder
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
Nuxt automatically imports any components in this directory (along with components that are registered by any modules you may be using).
|
|
9
|
+
|
|
10
|
+
```bash [Directory Structure]
|
|
11
|
+
-| components/
|
|
12
|
+
---| AppHeader.vue
|
|
13
|
+
---| AppFooter.vue
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```html [app.vue]
|
|
17
|
+
<template>
|
|
18
|
+
<div>
|
|
19
|
+
<AppHeader />
|
|
20
|
+
<NuxtPage />
|
|
21
|
+
<AppFooter />
|
|
22
|
+
</div>
|
|
23
|
+
</template>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Component Names
|
|
27
|
+
|
|
28
|
+
If you have a component in nested directories such as:
|
|
29
|
+
|
|
30
|
+
```bash [Directory Structure]
|
|
31
|
+
-| components/
|
|
32
|
+
---| base/
|
|
33
|
+
-----| foo/
|
|
34
|
+
-------| Button.vue
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
... then the component's name will be based on its own path directory and filename, with duplicate segments being removed. Therefore, the component's name will be:
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<BaseFooButton />
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
::note
|
|
44
|
+
For clarity, we recommend that the component's filename matches its name. So, in the example above, you could rename `Button.vue` to be `BaseFooButton.vue`.
|
|
45
|
+
::
|
|
46
|
+
|
|
47
|
+
If you want to auto-import components based only on its name, not path, then you need to set `pathPrefix` option to `false` using extended form of the configuration object:
|
|
48
|
+
|
|
49
|
+
```ts twoslash [nuxt.config.ts]
|
|
50
|
+
export default defineNuxtConfig({
|
|
51
|
+
components: [
|
|
52
|
+
{
|
|
53
|
+
path: '~/components',
|
|
54
|
+
pathPrefix: false, // [!code ++]
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This registers the components using the same strategy as used in Nuxt 2. For example, `~/components/Some/MyComponent.vue` will be usable as `<MyComponent>` and not `<SomeMyComponent>`.
|
|
61
|
+
|
|
62
|
+
## Dynamic Components
|
|
63
|
+
|
|
64
|
+
If you want to use the Vue `<component :is="someComputedComponent">`{lang=vue} syntax, you need to use the `resolveComponent` helper provided by Vue or import the component directly from `#components` and pass it into `is` prop.
|
|
65
|
+
|
|
66
|
+
For example:
|
|
67
|
+
|
|
68
|
+
```vue [pages/index.vue]
|
|
69
|
+
<script setup lang="ts">
|
|
70
|
+
import { SomeComponent } from '#components'
|
|
71
|
+
|
|
72
|
+
const MyButton = resolveComponent('MyButton')
|
|
73
|
+
</script>
|
|
74
|
+
|
|
75
|
+
<template>
|
|
76
|
+
<component :is="clickable ? MyButton : 'div'" />
|
|
77
|
+
<component :is="SomeComponent" />
|
|
78
|
+
</template>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
::important
|
|
82
|
+
If you are using `resolveComponent` to handle dynamic components, make sure not to insert anything but the name of the component, which must be a literal string and not be or contain a variable. The string is statically analyzed at compilation step.
|
|
83
|
+
::
|
|
84
|
+
|
|
85
|
+
:video-accordion{title="Watch Daniel Roe's short video about resolveComponent()" videoId="4kq8E5IUM2U"}
|
|
86
|
+
|
|
87
|
+
Alternatively, though not recommended, you can register all your components globally, which will create async chunks for all your components and make them available throughout your application.
|
|
88
|
+
|
|
89
|
+
```diff
|
|
90
|
+
export default defineNuxtConfig({
|
|
91
|
+
components: {
|
|
92
|
+
+ global: true,
|
|
93
|
+
+ dirs: ['~/components']
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
You can also selectively register some components globally by placing them in a `~/components/global` directory, or by using a `.global.vue` suffix in the filename. As noted above, each global component is rendered in a separate chunk, so be careful not to overuse this feature.
|
|
99
|
+
|
|
100
|
+
::note
|
|
101
|
+
The `global` option can also be set per component directory.
|
|
102
|
+
::
|
|
103
|
+
|
|
104
|
+
## Dynamic Imports
|
|
105
|
+
|
|
106
|
+
To dynamically import a component (also known as lazy-loading a component) all you need to do is add the `Lazy` prefix to the component's name. This is particularly useful if the component is not always needed.
|
|
107
|
+
|
|
108
|
+
By using the `Lazy` prefix you can delay loading the component code until the right moment, which can be helpful for optimizing your JavaScript bundle size.
|
|
109
|
+
|
|
110
|
+
```vue [pages/index.vue]
|
|
111
|
+
<script setup lang="ts">
|
|
112
|
+
const show = ref(false)
|
|
113
|
+
</script>
|
|
114
|
+
|
|
115
|
+
<template>
|
|
116
|
+
<div>
|
|
117
|
+
<h1>Mountains</h1>
|
|
118
|
+
<LazyMountainsList v-if="show" />
|
|
119
|
+
<button v-if="!show" @click="show = true">Show List</button>
|
|
120
|
+
</div>
|
|
121
|
+
</template>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Delayed (or Lazy) Hydration
|
|
125
|
+
|
|
126
|
+
Lazy components are great for controlling the chunk sizes in your app, but they don't always enhance runtime performance, as they still load eagerly unless conditionally rendered. In real-world applications, some pages may include a lot of content and a lot of components, and most of the time not all of them need to be interactive as soon as the page is loaded. Having them all load eagerly can negatively impact performance.
|
|
127
|
+
|
|
128
|
+
In order to optimize your app, you may want to delay the hydration of some components until they're visible, or until the browser is done with more important tasks.
|
|
129
|
+
|
|
130
|
+
Nuxt supports this using lazy (or delayed) hydration, allowing you to control when components become interactive.
|
|
131
|
+
|
|
132
|
+
### Hydration Strategies
|
|
133
|
+
|
|
134
|
+
Nuxt provides a range of built-in hydration strategies. Only one strategy can be used per lazy component.
|
|
135
|
+
|
|
136
|
+
::warning
|
|
137
|
+
Currently Nuxt's built-in lazy hydration only works in single-file components (SFCs), and requires you to define the prop in the template (rather than spreading an object of props via `v-bind`). It also does not work with direct imports from `#components`.
|
|
138
|
+
::
|
|
139
|
+
|
|
140
|
+
#### `hydrate-on-visible`
|
|
141
|
+
|
|
142
|
+
Hydrates the component when it becomes visible in the viewport.
|
|
143
|
+
|
|
144
|
+
```vue [pages/index.vue]
|
|
145
|
+
<template>
|
|
146
|
+
<div>
|
|
147
|
+
<LazyMyComponent hydrate-on-visible />
|
|
148
|
+
</div>
|
|
149
|
+
</template>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
::read-more{to="https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver" title="IntersectionObserver options"}
|
|
153
|
+
Read more about the options for `hydrate-on-visible`.
|
|
154
|
+
::
|
|
155
|
+
|
|
156
|
+
::note
|
|
157
|
+
Under the hood, this uses Vue's built-in [`hydrateOnVisible` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-visible).
|
|
158
|
+
::
|
|
159
|
+
|
|
160
|
+
#### `hydrate-on-idle`
|
|
161
|
+
|
|
162
|
+
Hydrates the component when the browser is idle. This is suitable if you need the component to load as soon as possible, but not block the critical rendering path.
|
|
163
|
+
|
|
164
|
+
You can also pass a number which serves as a max timeout.
|
|
165
|
+
|
|
166
|
+
```vue [pages/index.vue]
|
|
167
|
+
<template>
|
|
168
|
+
<div>
|
|
169
|
+
<LazyMyComponent hydrate-on-idle />
|
|
170
|
+
</div>
|
|
171
|
+
</template>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
::note
|
|
175
|
+
Under the hood, this uses Vue's built-in [`hydrateOnIdle` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-idle).
|
|
176
|
+
::
|
|
177
|
+
|
|
178
|
+
#### `hydrate-on-interaction`
|
|
179
|
+
|
|
180
|
+
Hydrates the component after a specified interaction (e.g., click, mouseover).
|
|
181
|
+
|
|
182
|
+
```vue [pages/index.vue]
|
|
183
|
+
<template>
|
|
184
|
+
<div>
|
|
185
|
+
<LazyMyComponent hydrate-on-interaction="mouseover" />
|
|
186
|
+
</div>
|
|
187
|
+
</template>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
If you do not pass an event or list of events, it defaults to hydrating on `pointerenter` and `focus`.
|
|
191
|
+
|
|
192
|
+
::note
|
|
193
|
+
Under the hood, this uses Vue's built-in [`hydrateOnInteraction` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-interaction).
|
|
194
|
+
::
|
|
195
|
+
|
|
196
|
+
#### `hydrate-on-media-query`
|
|
197
|
+
|
|
198
|
+
Hydrates the component when the window matches a media query.
|
|
199
|
+
|
|
200
|
+
```vue [pages/index.vue]
|
|
201
|
+
<template>
|
|
202
|
+
<div>
|
|
203
|
+
<LazyMyComponent hydrate-on-media-query="(max-width: 768px)" />
|
|
204
|
+
</div>
|
|
205
|
+
</template>
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
::note
|
|
209
|
+
Under the hood, this uses Vue's built-in [`hydrateOnMediaQuery` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-media-query).
|
|
210
|
+
::
|
|
211
|
+
|
|
212
|
+
#### `hydrate-after`
|
|
213
|
+
|
|
214
|
+
Hydrates the component after a specified delay (in milliseconds).
|
|
215
|
+
|
|
216
|
+
```vue [pages/index.vue]
|
|
217
|
+
<template>
|
|
218
|
+
<div>
|
|
219
|
+
<LazyMyComponent :hydrate-after="2000" />
|
|
220
|
+
</div>
|
|
221
|
+
</template>
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### `hydrate-when`
|
|
225
|
+
|
|
226
|
+
Hydrates the component based on a boolean condition.
|
|
227
|
+
|
|
228
|
+
```vue [pages/index.vue]
|
|
229
|
+
<template>
|
|
230
|
+
<div>
|
|
231
|
+
<LazyMyComponent :hydrate-when="isReady" />
|
|
232
|
+
</div>
|
|
233
|
+
</template>
|
|
234
|
+
<script setup lang="ts">
|
|
235
|
+
const isReady = ref(false)
|
|
236
|
+
function myFunction() {
|
|
237
|
+
// trigger custom hydration strategy...
|
|
238
|
+
isReady.value = true
|
|
239
|
+
}
|
|
240
|
+
</script>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### `hydrate-never`
|
|
244
|
+
|
|
245
|
+
Never hydrates the component.
|
|
246
|
+
|
|
247
|
+
```vue [pages/index.vue]
|
|
248
|
+
<template>
|
|
249
|
+
<div>
|
|
250
|
+
<LazyMyComponent hydrate-never />
|
|
251
|
+
</div>
|
|
252
|
+
</template>
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Listening to Hydration Events
|
|
256
|
+
|
|
257
|
+
All delayed hydration components emit a `@hydrated` event when they are hydrated.
|
|
258
|
+
|
|
259
|
+
```vue [pages/index.vue]
|
|
260
|
+
<template>
|
|
261
|
+
<div>
|
|
262
|
+
<LazyMyComponent hydrate-on-visible @hydrated="onHydrate" />
|
|
263
|
+
</div>
|
|
264
|
+
</template>
|
|
265
|
+
|
|
266
|
+
<script setup lang="ts">
|
|
267
|
+
function onHydrate() {
|
|
268
|
+
console.log("Component has been hydrated!")
|
|
269
|
+
}
|
|
270
|
+
</script>
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Caveats and Best Practices
|
|
274
|
+
|
|
275
|
+
Delayed hydration can offer performance benefits, but it's essential to use it correctly:
|
|
276
|
+
|
|
277
|
+
1. **Prioritize In-Viewport Content:** Avoid delayed hydration for critical, above-the-fold content. It's best suited for content that isn't immediately needed.
|
|
278
|
+
|
|
279
|
+
2. **Conditional Rendering:** When using `v-if="false"` on a lazy component, you might not need delayed hydration. You can just use a normal lazy component.
|
|
280
|
+
|
|
281
|
+
3. **Shared State:** Be mindful of shared state (`v-model`) across multiple components. Updating the model in one component can trigger hydration in all components bound to that model.
|
|
282
|
+
|
|
283
|
+
4. **Use Each Strategy's Intended Use Case:** Each strategy is optimized for a specific purpose.
|
|
284
|
+
* `hydrate-when` is best for components that might not always need to be hydrated.
|
|
285
|
+
* `hydrate-after` is for components that can wait a specific amount of time.
|
|
286
|
+
* `hydrate-on-idle` is for components that can be hydrated when the browser is idle.
|
|
287
|
+
|
|
288
|
+
5. **Avoid `hydrate-never` on interactive components:** If a component requires user interaction, it should not be set to never hydrate.
|
|
289
|
+
|
|
290
|
+
## Direct Imports
|
|
291
|
+
|
|
292
|
+
You can also explicitly import components from `#components` if you want or need to bypass Nuxt's auto-importing functionality.
|
|
293
|
+
|
|
294
|
+
```vue [pages/index.vue]
|
|
295
|
+
<script setup lang="ts">
|
|
296
|
+
import { NuxtLink, LazyMountainsList } from '#components'
|
|
297
|
+
|
|
298
|
+
const show = ref(false)
|
|
299
|
+
</script>
|
|
300
|
+
|
|
301
|
+
<template>
|
|
302
|
+
<div>
|
|
303
|
+
<h1>Mountains</h1>
|
|
304
|
+
<LazyMountainsList v-if="show" />
|
|
305
|
+
<button v-if="!show" @click="show = true">Show List</button>
|
|
306
|
+
<NuxtLink to="/">Home</NuxtLink>
|
|
307
|
+
</div>
|
|
308
|
+
</template>
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
## Custom Directories
|
|
312
|
+
|
|
313
|
+
By default, only the `~/components` directory is scanned. If you want to add other directories, or change how the components are scanned within a subfolder of this directory, you can add additional directories to the configuration:
|
|
314
|
+
|
|
315
|
+
```ts twoslash [nuxt.config.ts]
|
|
316
|
+
export default defineNuxtConfig({
|
|
317
|
+
components: [
|
|
318
|
+
// ~/calendar-module/components/event/Update.vue => <EventUpdate />
|
|
319
|
+
{ path: '~/calendar-module/components' },
|
|
320
|
+
|
|
321
|
+
// ~/user-module/components/account/UserDeleteDialog.vue => <UserDeleteDialog />
|
|
322
|
+
{ path: '~/user-module/components', pathPrefix: false },
|
|
323
|
+
|
|
324
|
+
// ~/components/special-components/Btn.vue => <SpecialBtn />
|
|
325
|
+
{ path: '~/components/special-components', prefix: 'Special' },
|
|
326
|
+
|
|
327
|
+
// It's important that this comes last if you have overrides you wish to apply
|
|
328
|
+
// to sub-directories of `~/components`.
|
|
329
|
+
//
|
|
330
|
+
// ~/components/Btn.vue => <Btn />
|
|
331
|
+
// ~/components/base/Btn.vue => <BaseBtn />
|
|
332
|
+
'~/components'
|
|
333
|
+
]
|
|
334
|
+
})
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
::note
|
|
338
|
+
Any nested directories need to be added first as they are scanned in order.
|
|
339
|
+
::
|
|
340
|
+
|
|
341
|
+
## npm Packages
|
|
342
|
+
|
|
343
|
+
If you want to auto-import components from an npm package, you can use [`addComponent`](/docs/api/kit/components#addcomponent) in a [local module](/docs/guide/directory-structure/modules) to register them.
|
|
344
|
+
|
|
345
|
+
::code-group
|
|
346
|
+
|
|
347
|
+
```ts twoslash [~/modules/register-component.ts]
|
|
348
|
+
import { addComponent, defineNuxtModule } from '@nuxt/kit'
|
|
349
|
+
|
|
350
|
+
export default defineNuxtModule({
|
|
351
|
+
setup() {
|
|
352
|
+
// import { MyComponent as MyAutoImportedComponent } from 'my-npm-package'
|
|
353
|
+
addComponent({
|
|
354
|
+
name: 'MyAutoImportedComponent',
|
|
355
|
+
export: 'MyComponent',
|
|
356
|
+
filePath: 'my-npm-package',
|
|
357
|
+
})
|
|
358
|
+
},
|
|
359
|
+
})
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
```vue [app.vue]
|
|
363
|
+
<template>
|
|
364
|
+
<div>
|
|
365
|
+
<!-- the component uses the name we specified and is auto-imported -->
|
|
366
|
+
<MyAutoImportedComponent />
|
|
367
|
+
</div>
|
|
368
|
+
</template>
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
::
|
|
372
|
+
|
|
373
|
+
## Component Extensions
|
|
374
|
+
|
|
375
|
+
By default, any file with an extension specified in the [extensions key of `nuxt.config.ts`](/docs/api/nuxt-config#extensions) is treated as a component.
|
|
376
|
+
If you need to restrict the file extensions that should be registered as components, you can use the extended form of the components directory declaration and its `extensions` key:
|
|
377
|
+
|
|
378
|
+
```ts twoslash [nuxt.config.ts]
|
|
379
|
+
export default defineNuxtConfig({
|
|
380
|
+
components: [
|
|
381
|
+
{
|
|
382
|
+
path: '~/components',
|
|
383
|
+
extensions: ['.vue'], // [!code ++]
|
|
384
|
+
}
|
|
385
|
+
]
|
|
386
|
+
})
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
## Client Components
|
|
390
|
+
|
|
391
|
+
If a component is meant to be rendered only client-side, you can add the `.client` suffix to your component.
|
|
392
|
+
|
|
393
|
+
```bash [Directory Structure]
|
|
394
|
+
| components/
|
|
395
|
+
--| Comments.client.vue
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
```vue [pages/example.vue]
|
|
399
|
+
<template>
|
|
400
|
+
<div>
|
|
401
|
+
<!-- this component will only be rendered on client side -->
|
|
402
|
+
<Comments />
|
|
403
|
+
</div>
|
|
404
|
+
</template>
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
::note
|
|
408
|
+
This feature only works with Nuxt auto-imports and `#components` imports. Explicitly importing these components from their real paths does not convert them into client-only components.
|
|
409
|
+
::
|
|
410
|
+
|
|
411
|
+
::important
|
|
412
|
+
`.client` components are rendered only after being mounted. To access the rendered template using `onMounted()`, add `await nextTick()` in the callback of the `onMounted()` hook.
|
|
413
|
+
::
|
|
414
|
+
|
|
415
|
+
::read-more{to="/docs/api/components/client-only"}
|
|
416
|
+
You can also achieve a similar result with the `<ClientOnly>` component.
|
|
417
|
+
::
|
|
418
|
+
|
|
419
|
+
## Server Components
|
|
420
|
+
|
|
421
|
+
Server components allow server-rendering individual components within your client-side apps. It's possible to use server components within Nuxt, even if you are generating a static site. That makes it possible to build complex sites that mix dynamic components, server-rendered HTML and even static chunks of markup.
|
|
422
|
+
|
|
423
|
+
Server components can either be used on their own or paired with a [client component](#paired-with-a-client-component).
|
|
424
|
+
|
|
425
|
+
:video-accordion{title="Watch Learn Vue video about Nuxt Server Components" videoId="u1yyXe86xJM"}
|
|
426
|
+
|
|
427
|
+
::tip{icon="i-lucide-newspaper" to="https://roe.dev/blog/nuxt-server-components" target="_blank"}
|
|
428
|
+
Read Daniel Roe's guide to Nuxt Server Components.
|
|
429
|
+
::
|
|
430
|
+
|
|
431
|
+
### Standalone server components
|
|
432
|
+
|
|
433
|
+
Standalone server components will always be rendered on the server, also known as Islands components.
|
|
434
|
+
|
|
435
|
+
When their props update, this will result in a network request that will update the rendered HTML in-place.
|
|
436
|
+
|
|
437
|
+
Server components are currently experimental and in order to use them, you need to enable the 'component islands' feature in your nuxt.config:
|
|
438
|
+
|
|
439
|
+
```ts twoslash [nuxt.config.ts]
|
|
440
|
+
export default defineNuxtConfig({
|
|
441
|
+
experimental: {
|
|
442
|
+
componentIslands: true
|
|
443
|
+
}
|
|
444
|
+
})
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
Now you can register server-only components with the `.server` suffix and use them anywhere in your application automatically.
|
|
448
|
+
|
|
449
|
+
```bash [Directory Structure]
|
|
450
|
+
-| components/
|
|
451
|
+
---| HighlightedMarkdown.server.vue
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
```vue [pages/example.vue]
|
|
455
|
+
<template>
|
|
456
|
+
<div>
|
|
457
|
+
<!--
|
|
458
|
+
this will automatically be rendered on the server, meaning your markdown parsing + highlighting
|
|
459
|
+
libraries are not included in your client bundle.
|
|
460
|
+
-->
|
|
461
|
+
<HighlightedMarkdown markdown="# Headline" />
|
|
462
|
+
</div>
|
|
463
|
+
</template>
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
Server-only components use [`<NuxtIsland>`](/docs/api/components/nuxt-island) under the hood, meaning that `lazy` prop and `#fallback` slot are both passed down to it.
|
|
467
|
+
|
|
468
|
+
::warning
|
|
469
|
+
Server components (and islands) must have a single root element. (HTML comments are considered elements as well.)
|
|
470
|
+
::
|
|
471
|
+
|
|
472
|
+
::warning
|
|
473
|
+
Props are passed to server components via URL query parameters, and are therefore limited by the possible length of a URL, so be careful not to pass enormous amounts of data to server components via props.
|
|
474
|
+
::
|
|
475
|
+
|
|
476
|
+
::warning
|
|
477
|
+
Be careful when nesting islands within other islands as each island adds some extra overhead.
|
|
478
|
+
::
|
|
479
|
+
|
|
480
|
+
::warning
|
|
481
|
+
Most features for server-only components and island components, such as slots and client components, are only available for single file components.
|
|
482
|
+
::
|
|
483
|
+
|
|
484
|
+
#### Client components within server components
|
|
485
|
+
|
|
486
|
+
::note
|
|
487
|
+
This feature needs `experimental.componentIslands.selectiveClient` within your configuration to be true.
|
|
488
|
+
::
|
|
489
|
+
|
|
490
|
+
You can partially hydrate a component by setting a `nuxt-client` attribute on the component you wish to be loaded client-side.
|
|
491
|
+
|
|
492
|
+
```vue [components/ServerWithClient.vue]
|
|
493
|
+
<template>
|
|
494
|
+
<div>
|
|
495
|
+
<HighlightedMarkdown markdown="# Headline" />
|
|
496
|
+
<!-- Counter will be loaded and hydrated client-side -->
|
|
497
|
+
<Counter nuxt-client :count="5" />
|
|
498
|
+
</div>
|
|
499
|
+
</template>
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
::note
|
|
503
|
+
This only works within a server component. Slots for client components are working only with `experimental.componentIsland.selectiveClient` set to `'deep'` and since they are rendered server-side, they are not interactive once client-side.
|
|
504
|
+
::
|
|
505
|
+
|
|
506
|
+
#### Server Component Context
|
|
507
|
+
|
|
508
|
+
When rendering a server-only or island component, `<NuxtIsland>` makes a fetch request which comes back with a `NuxtIslandResponse`. (This is an internal request if rendered on the server, or a request that you can see in the network tab if it's rendering on client-side navigation.)
|
|
509
|
+
|
|
510
|
+
This means:
|
|
511
|
+
|
|
512
|
+
* A new Vue app will be created server-side to create the `NuxtIslandResponse`.
|
|
513
|
+
* A new 'island context' will be created while rendering the component.
|
|
514
|
+
* You can't access the 'island context' from the rest of your app and you can't access the context of the rest of your app from the island component. In other words, the server component or island is _isolated_ from the rest of your app.
|
|
515
|
+
* Your plugins will run again when rendering the island, unless they have `env: { islands: false }` set (which you can do in an object-syntax plugin).
|
|
516
|
+
|
|
517
|
+
Within an island component, you can access its island context through `nuxtApp.ssrContext.islandContext`. Note that while island components are still marked as experimental, the format of this context may change.
|
|
518
|
+
|
|
519
|
+
::note
|
|
520
|
+
Slots can be interactive and are wrapped within a `<div>` with `display: contents;`
|
|
521
|
+
::
|
|
522
|
+
|
|
523
|
+
### Paired with a Client component
|
|
524
|
+
|
|
525
|
+
In this case, the `.server` + `.client` components are two 'halves' of a component and can be used in advanced use cases for separate implementations of a component on server and client side.
|
|
526
|
+
|
|
527
|
+
```bash [Directory Structure]
|
|
528
|
+
-| components/
|
|
529
|
+
---| Comments.client.vue
|
|
530
|
+
---| Comments.server.vue
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
```vue [pages/example.vue]
|
|
534
|
+
<template>
|
|
535
|
+
<div>
|
|
536
|
+
<!-- this component will render Comments.server on the server then Comments.client once mounted in the browser -->
|
|
537
|
+
<Comments />
|
|
538
|
+
</div>
|
|
539
|
+
</template>
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
## Built-In Nuxt Components
|
|
543
|
+
|
|
544
|
+
There are a number of components that Nuxt provides, including `<ClientOnly>` and `<DevOnly>`. You can read more about them in the API documentation.
|
|
545
|
+
|
|
546
|
+
::read-more{to="/docs/api"}
|
|
547
|
+
::
|
|
548
|
+
|
|
549
|
+
## Library Authors
|
|
550
|
+
|
|
551
|
+
Making Vue component libraries with automatic tree-shaking and component registration is super easy. ✨
|
|
552
|
+
|
|
553
|
+
You can use the [`addComponentsDir`](/docs/api/kit/components#addcomponentsdir) method provided from the `@nuxt/kit` to register your components directory in your Nuxt module.
|
|
554
|
+
|
|
555
|
+
Imagine a directory structure like this:
|
|
556
|
+
|
|
557
|
+
```bash [Directory Structure]
|
|
558
|
+
-| node_modules/
|
|
559
|
+
---| awesome-ui/
|
|
560
|
+
-----| components/
|
|
561
|
+
-------| Alert.vue
|
|
562
|
+
-------| Button.vue
|
|
563
|
+
-----| nuxt.ts
|
|
564
|
+
-| pages/
|
|
565
|
+
---| index.vue
|
|
566
|
+
-| nuxt.config.ts
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
Then in `awesome-ui/nuxt.ts` you can use the `addComponentsDir` hook:
|
|
570
|
+
|
|
571
|
+
```ts twoslash
|
|
572
|
+
import { createResolver, defineNuxtModule, addComponentsDir } from '@nuxt/kit'
|
|
573
|
+
|
|
574
|
+
export default defineNuxtModule({
|
|
575
|
+
setup() {
|
|
576
|
+
const resolver = createResolver(import.meta.url)
|
|
577
|
+
|
|
578
|
+
// Add ./components dir to the list
|
|
579
|
+
addComponentsDir({
|
|
580
|
+
path: resolver.resolve('./components'),
|
|
581
|
+
prefix: 'awesome',
|
|
582
|
+
})
|
|
583
|
+
},
|
|
584
|
+
})
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
That's it! Now in your project, you can import your UI library as a Nuxt module in your `nuxt.config` file:
|
|
588
|
+
|
|
589
|
+
```ts twoslash [nuxt.config.ts]
|
|
590
|
+
export default defineNuxtConfig({
|
|
591
|
+
modules: ['awesome-ui/nuxt']
|
|
592
|
+
})
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
... and directly use the module components (prefixed with `awesome-`) in our `pages/index.vue`:
|
|
596
|
+
|
|
597
|
+
```vue
|
|
598
|
+
<template>
|
|
599
|
+
<div>
|
|
600
|
+
My <AwesomeButton>UI button</AwesomeButton>!
|
|
601
|
+
<awesome-alert>Here's an alert!</awesome-alert>
|
|
602
|
+
</div>
|
|
603
|
+
</template>
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
It will automatically import the components only if used and also support HMR when updating your components in `node_modules/awesome-ui/components/`.
|
|
607
|
+
|
|
608
|
+
:link-example{to="/docs/examples/features/auto-imports"}
|