@nuxt/docs-nightly 4.0.0-29182580.8b9e1a1e → 4.0.0-29183591.b617f15e
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.
|
@@ -187,7 +187,7 @@ Hydrates the component after a specified interaction (e.g., click, mouseover).
|
|
|
187
187
|
</template>
|
|
188
188
|
```
|
|
189
189
|
|
|
190
|
-
If you do not pass an event or list of events, it defaults to hydrating on `pointerenter` and `focus`.
|
|
190
|
+
If you do not pass an event or list of events, it defaults to hydrating on `pointerenter`, `click` and `focus`.
|
|
191
191
|
|
|
192
192
|
::note
|
|
193
193
|
Under the hood, this uses Vue's built-in [`hydrateOnInteraction` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-interaction).
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'defineLazyHydrationComponent'
|
|
3
|
+
description: 'Define a lazy hydration component with a specific strategy.'
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/components/plugins/lazy-hydration-macro-transform.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`defineLazyHydrationComponent` is a compiler macro that helps you create a component with a specific lazy hydration strategy. Lazy hydration defers hydration until components become visible or until the browser has completed more critical tasks. This can significantly reduce the initial performance cost, especially for non-essential components.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Visibility Strategy
|
|
16
|
+
|
|
17
|
+
Hydrates the component when it becomes visible in the viewport.
|
|
18
|
+
|
|
19
|
+
```vue
|
|
20
|
+
<script setup lang="ts">
|
|
21
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
22
|
+
'visible',
|
|
23
|
+
() => import('./components/MyComponent.vue')
|
|
24
|
+
)
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<template>
|
|
28
|
+
<div>
|
|
29
|
+
<!--
|
|
30
|
+
Hydration will be triggered when
|
|
31
|
+
the element(s) is 100px away from entering the viewport.
|
|
32
|
+
-->
|
|
33
|
+
<LazyHydrationMyComponent :hydrate-on-visible="{ rootMargin: '100px' }" />
|
|
34
|
+
</div>
|
|
35
|
+
</template>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The `hydrateOnVisible` prop is optional. You can pass an object to customize the behavior of the `IntersectionObserver` under the hood.
|
|
39
|
+
|
|
40
|
+
::read-more{to="https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver" title="IntersectionObserver options"}
|
|
41
|
+
Read more about the options for `hydrate-on-visible`.
|
|
42
|
+
::
|
|
43
|
+
|
|
44
|
+
::note
|
|
45
|
+
Under the hood, this uses Vue's built-in [`hydrateOnVisible` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-visible).
|
|
46
|
+
::
|
|
47
|
+
|
|
48
|
+
### Idle Strategy
|
|
49
|
+
|
|
50
|
+
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.
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
55
|
+
'idle',
|
|
56
|
+
() => import('./components/MyComponent.vue')
|
|
57
|
+
)
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<template>
|
|
61
|
+
<div>
|
|
62
|
+
<!-- Hydration will be triggered when the browser is idle or after 2000ms. -->
|
|
63
|
+
<LazyHydrationMyComponent :hydrate-on-idle="2000" />
|
|
64
|
+
</div>
|
|
65
|
+
</template>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The `hydrateOnIdle` prop is optional. You can pass a positive number to specify the maximum timeout.
|
|
69
|
+
|
|
70
|
+
Idle strategy is for components that can be hydrated when the browser is idle.
|
|
71
|
+
|
|
72
|
+
::note
|
|
73
|
+
Under the hood, this uses Vue's built-in [`hydrateOnIdle` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-idle).
|
|
74
|
+
::
|
|
75
|
+
|
|
76
|
+
### Interaction Strategy
|
|
77
|
+
|
|
78
|
+
Hydrates the component after a specified interaction (e.g., click, mouseover).
|
|
79
|
+
|
|
80
|
+
```vue
|
|
81
|
+
<script setup lang="ts">
|
|
82
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
83
|
+
'interaction',
|
|
84
|
+
() => import('./components/MyComponent.vue')
|
|
85
|
+
)
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<template>
|
|
89
|
+
<div>
|
|
90
|
+
<!--
|
|
91
|
+
Hydration will be triggered when
|
|
92
|
+
the element(s) is hovered over by the pointer.
|
|
93
|
+
-->
|
|
94
|
+
<LazyHydrationMyComponent hydrate-on-interaction="mouseover" />
|
|
95
|
+
</div>
|
|
96
|
+
</template>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The `hydrateOnInteraction` prop is optional. If you do not pass an event or a list of events, it defaults to hydrating on `pointerenter`, `click`, and `focus`.
|
|
100
|
+
|
|
101
|
+
::note
|
|
102
|
+
Under the hood, this uses Vue's built-in [`hydrateOnInteraction` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-interaction).
|
|
103
|
+
::
|
|
104
|
+
|
|
105
|
+
### Media Query Strategy
|
|
106
|
+
|
|
107
|
+
Hydrates the component when the window matches a media query.
|
|
108
|
+
|
|
109
|
+
```vue
|
|
110
|
+
<script setup lang="ts">
|
|
111
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
112
|
+
'mediaQuery',
|
|
113
|
+
() => import('./components/MyComponent.vue')
|
|
114
|
+
)
|
|
115
|
+
</script>
|
|
116
|
+
|
|
117
|
+
<template>
|
|
118
|
+
<div>
|
|
119
|
+
<!--
|
|
120
|
+
Hydration will be triggered when
|
|
121
|
+
the window width is greater than or equal to 768px.
|
|
122
|
+
-->
|
|
123
|
+
<LazyHydrationMyComponent hydrate-on-media-query="(min-width: 768px)" />
|
|
124
|
+
</div>
|
|
125
|
+
</template>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
::note
|
|
129
|
+
Under the hood, this uses Vue's built-in [`hydrateOnMediaQuery` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-media-query).
|
|
130
|
+
::
|
|
131
|
+
|
|
132
|
+
### Time Strategy
|
|
133
|
+
|
|
134
|
+
Hydrates the component after a specified delay (in milliseconds).
|
|
135
|
+
|
|
136
|
+
```vue
|
|
137
|
+
<script setup lang="ts">
|
|
138
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
139
|
+
'time',
|
|
140
|
+
() => import('./components/MyComponent.vue')
|
|
141
|
+
)
|
|
142
|
+
</script>
|
|
143
|
+
|
|
144
|
+
<template>
|
|
145
|
+
<div>
|
|
146
|
+
<!-- Hydration is triggered after 1000ms. -->
|
|
147
|
+
<LazyHydrationMyComponent :hydrate-after="1000" />
|
|
148
|
+
</div>
|
|
149
|
+
</template>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Time strategy is for components that can wait a specific amount of time.
|
|
153
|
+
|
|
154
|
+
### If Strategy
|
|
155
|
+
|
|
156
|
+
Hydrates the component based on a boolean condition.
|
|
157
|
+
|
|
158
|
+
```vue
|
|
159
|
+
<script setup lang="ts">
|
|
160
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
161
|
+
'if',
|
|
162
|
+
() => import('./components/MyComponent.vue')
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
const isReady = ref(false)
|
|
166
|
+
|
|
167
|
+
function myFunction() {
|
|
168
|
+
// Trigger custom hydration strategy...
|
|
169
|
+
isReady.value = true
|
|
170
|
+
}
|
|
171
|
+
</script>
|
|
172
|
+
|
|
173
|
+
<template>
|
|
174
|
+
<div>
|
|
175
|
+
<!-- Hydration is triggered when isReady becomes true. -->
|
|
176
|
+
<LazyHydrationMyComponent :hydrate-when="isReady" />
|
|
177
|
+
</div>
|
|
178
|
+
</template>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
If strategy is best for components that might not always need to be hydrated.
|
|
182
|
+
|
|
183
|
+
### Never Hydrate
|
|
184
|
+
|
|
185
|
+
Never hydrates the component.
|
|
186
|
+
|
|
187
|
+
```vue
|
|
188
|
+
<script setup lang="ts">
|
|
189
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
190
|
+
'never',
|
|
191
|
+
() => import('./components/MyComponent.vue')
|
|
192
|
+
)
|
|
193
|
+
</script>
|
|
194
|
+
|
|
195
|
+
<template>
|
|
196
|
+
<div>
|
|
197
|
+
<!-- This component will never be hydrated by Vue. -->
|
|
198
|
+
<LazyHydrationMyComponent />
|
|
199
|
+
</div>
|
|
200
|
+
</template>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Listening to Hydration Events
|
|
204
|
+
|
|
205
|
+
All delayed hydration components emit a `@hydrated` event when they are hydrated.
|
|
206
|
+
|
|
207
|
+
```vue
|
|
208
|
+
<script setup lang="ts">
|
|
209
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
210
|
+
'visible',
|
|
211
|
+
() => import('./components/MyComponent.vue')
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
function onHydrate() {
|
|
215
|
+
console.log("Component has been hydrated!")
|
|
216
|
+
}
|
|
217
|
+
</script>
|
|
218
|
+
|
|
219
|
+
<template>
|
|
220
|
+
<div>
|
|
221
|
+
<LazyHydrationMyComponent
|
|
222
|
+
:hydrate-on-visible="{ rootMargin: '100px' }"
|
|
223
|
+
@hydrated="onHydrated"
|
|
224
|
+
/>
|
|
225
|
+
</div>
|
|
226
|
+
</template>
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Parameters
|
|
230
|
+
|
|
231
|
+
::warning
|
|
232
|
+
To ensure that the compiler correctly recognizes this macro, avoid using external variables. The following approach will prevent the macro from being properly recognized:
|
|
233
|
+
|
|
234
|
+
```vue
|
|
235
|
+
<script setup lang="ts">
|
|
236
|
+
const strategy = 'visible'
|
|
237
|
+
const source = () => import('./components/MyComponent.vue')
|
|
238
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(strategy, source)
|
|
239
|
+
</script>
|
|
240
|
+
```
|
|
241
|
+
::
|
|
242
|
+
|
|
243
|
+
### `strategy`
|
|
244
|
+
|
|
245
|
+
- **Type**: `'visible' | 'idle' | 'interaction' | 'mediaQuery' | 'if' | 'time' | 'never'`
|
|
246
|
+
- **Required**: `true`
|
|
247
|
+
|
|
248
|
+
| Strategy | Description |
|
|
249
|
+
|---------------|----------------------------------------------------------------|
|
|
250
|
+
| `visible` | Hydrates when the component becomes visible in the viewport. |
|
|
251
|
+
| `idle` | Hydrates when the browser is idle or after a delay. |
|
|
252
|
+
| `interaction` | Hydrates upon user interaction (e.g., click, hover). |
|
|
253
|
+
| `mediaQuery` | Hydrates when the specified media query condition is met. |
|
|
254
|
+
| `if` | Hydrates when a specified boolean condition is met. |
|
|
255
|
+
| `time` | Hydrates after a specified time delay. |
|
|
256
|
+
| `never` | Prevents Vue from hydrating the component. |
|
|
257
|
+
|
|
258
|
+
### `source`
|
|
259
|
+
|
|
260
|
+
- **Type**: `() => Promise<Component>`
|
|
261
|
+
- **Required**: `true`
|