Skip to content

Commit 82cb376

Browse files
committed
almost done
1 parent f98a23a commit 82cb376

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

en/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
- [Source Code Structure](structure.md)
44
- [Routing and Code-Splitting](routing.md)
55
- [Data Pre-fetching and State](data.md)
6+
- [Client Side Hydration](hydration.md)
67
- [Introducing Bundle Renderer](bundle-renderer.md)
78
- [Build Configuration](build-config.md)
89
- [CSS Management](css.md)
910
- [Head Management](head.md)
1011
- [Caching](caching.md)
1112
- [Streaming](streaming.md)
12-
- [Client Side Hydration](hydration.md)
13-
- [Security](security.md)
1413
- [API Reference](api.md)

en/hydration.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Client Side Hydration
2+
3+
In `entry-client.js`, we are simply mounting the app with this line:
4+
5+
``` js
6+
// this assumes App.vue template root element has id="app"
7+
app.$mount('#app')
8+
```
9+
10+
Since the server has already rendered the markup, we obviously do not want to throw that away and re-create all the DOM elements. Instead, we want to "hydrate" the static markup and make it interactive.
11+
12+
If you inspect the server-rendered output, you will notice that the app's root element has a special attribute:
13+
14+
``` js
15+
<div id="app" data-server-rendered="true">
16+
```
17+
18+
The `data-server-rendered` special attribute lets the client-side Vue know that the markup is rendered by the server and it should mount in hydration mode.
19+
20+
In development mode, Vue will assert the client-side generated virtual DOM tree matches the DOM structure rendered from the server. If there is a mismatch, it will bail hydration, discard existing DOM and render from scratch. **In production mode, this assertion is disabled for maximum performance.**
21+
22+
### Hydration Caveats
23+
24+
One thing to be aware of when using SSR + client hydration is some special HTML structures that may be altered by the browser. For example, when you write this in a Vue template:
25+
26+
``` html
27+
<table>
28+
<tr><td>hi</td></tr>
29+
</table>
30+
```
31+
32+
The browser will automatically inject `<tbody>` inside `<table>`, however, the virtual DOM generated by Vue does not contain `<tbody>`, so it will cause a mismatch. To ensure correct matching, make sure to write valid HTML in your templates.

en/security.md

Whitespace-only changes.

en/streaming.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Streaming
2+
3+
`vue-server-renderer` supports stream rendering out of the box, for both the base renderer and the bundle renderer. All you need to do is use `renderToStream` instead of `renderToString`:
4+
5+
``` js
6+
const stream = renderer.renderToStream(context)
7+
```
8+
9+
The returned value is a [Node.js stream](https://nodejs.org/api/stream.html):
10+
11+
``` js
12+
let html = ''
13+
14+
stream.on('data', data => {
15+
html += data.toString()
16+
})
17+
18+
stream.on('end', () => {
19+
console.log(html) // render complete
20+
})
21+
22+
stream.on('error', err => {
23+
// handle error...
24+
})
25+
```
26+
27+
## Streaming Caveats
28+
29+
In stream rendering mode, data is emitted as soon as possible when the renderer traverses the Virtual DOM tree. This means we can get an earlier "first chunk" and start sending it to the client faster.
30+
31+
However, when the first data chunk is emitted, the child components may not even be instantiated yet, neither will their lifecycle hooks get called. This means if the child components need to attach data to the render context in their lifecycle hooks, these data will not be available when the stream starts. Since a lot of the context information (like head information or inlined critical CSS) needs to be appear before the application markup, we essentially have to wait until the stream to complete before we can start making use of these context data.
32+
33+
It is therefore **NOT** recommended to use streaming mode if you rely on context data populated by component lifecycle hooks.

en/structure.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ import { createApp } from './app'
104104
// client-specific bootstrapping logic...
105105

106106
const { app } = createApp()
107+
108+
// this assumes App.vue template root element has id="app"
107109
app.$mount('#app')
108110
```
109111

0 commit comments

Comments
 (0)