Skip to content

Commit e1a923e

Browse files
committed
3.5: lazy hydration
1 parent b9ed0eb commit e1a923e

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/guide/components/async.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,105 @@ If a loading component is provided, it will be displayed first while the inner c
108108

109109
If an error component is provided, it will be displayed when the Promise returned by the loader function is rejected. You can also specify a timeout to show the error component when the request is taking too long.
110110

111+
## Lazy Hydration <sup class="vt-badge" data-text="3.5+" /> {#lazy-hydration}
112+
113+
> This section only applies if you are using [Server-Side Rendering](/guide/scaling-up/ssr).
114+
115+
In Vue 3.5+, async components can control when they are hydrated by providing a hydration strategy.
116+
117+
- Vue provides a number of built-in hydration strategies. These built-in strategies need to be individually imported so they can be tree-shaken if not used.
118+
119+
- The design is intentionally low-level for flexibility. Compiler syntax sugar can potentially be built on top of this in the future either in core or in higher level solutions (e.g. Nuxt).
120+
121+
### Hydrate on Idle
122+
123+
Hydrates via `requestIdleCallback`:
124+
125+
```js
126+
import { defineAsyncComponent, hydrateOnIdle } from 'vue'
127+
128+
const AsyncComp = defineAsyncComponent({
129+
loader: () => import('./Comp.vue'),
130+
hydrate: hydrateOnIdle(/* optionally pass a max timeout */)
131+
})
132+
```
133+
134+
### Hydrate on Visible
135+
136+
Hydrate when element(s) become visible via `IntersectionObserver`.
137+
138+
```js
139+
import { defineAsyncComponent, hydrateOnVisible } from 'vue'
140+
141+
const AsyncComp = defineAsyncComponent({
142+
loader: () => import('./Comp.vue'),
143+
hydrate: hydrateOnVisible()
144+
})
145+
```
146+
147+
Can optionally pass in an options object value for the observer:
148+
149+
```js
150+
hydrateOnVisible({ rootMargin: '100px' })
151+
```
152+
153+
### Hydrate on Media Query
154+
155+
Hydrates when the specified media query matches.
156+
157+
```js
158+
import { defineAsyncComponent, hydrateOnMediaQuery } from 'vue'
159+
160+
const AsyncComp = defineAsyncComponent({
161+
loader: () => import('./Comp.vue'),
162+
hydrate: hydrateOnMediaQuery('(max-width:500px)')
163+
})
164+
```
165+
166+
### Hydrate on Interaction
167+
168+
Hydrates when specified event(s) are triggered on the component element(s). The event that triggered the hydration will also be replayed once hydration is complete.
169+
170+
```js
171+
import { defineAsyncComponent, hydrateOnInteraction } from 'vue'
172+
173+
const AsyncComp = defineAsyncComponent({
174+
loader: () => import('./Comp.vue'),
175+
hydrate: hydrateOnInteraction('click')
176+
})
177+
```
178+
179+
Can also be a list of multiple event types:
180+
181+
```js
182+
hydrateOnInteraction(['wheel', 'mouseover'])
183+
```
184+
185+
### Custom Strategy
186+
187+
```ts
188+
import { defineAsyncComponent, type HydrationStrategy } from 'vue'
189+
190+
const myStrategy: HydrationStrategy = (hydrate, forEachElement) => {
191+
// forEachElement is a helper to iterate through all the root elememts
192+
// in the component's non-hydrated DOM, since the root can be a fragment
193+
// instead of a single element
194+
forEachElement(el => {
195+
// ...
196+
})
197+
// call `hydrate` when ready
198+
hydrate()
199+
return () => {
200+
// return a teardown function if needed
201+
}
202+
}
203+
204+
const AsyncComp = defineAsyncComponent({
205+
loader: () => import('./Comp.vue'),
206+
hydrate: myStrategy
207+
})
208+
```
209+
111210
## Using with Suspense {#using-with-suspense}
112211

113212
Async components can be used with the `<Suspense>` built-in component. The interaction between `<Suspense>` and async components is documented in the [dedicated chapter for `<Suspense>`](/guide/built-ins/suspense).

0 commit comments

Comments
 (0)