You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/vue-server-renderer/README.md
+67-38Lines changed: 67 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,14 @@
4
4
5
5
This package offers Node.js server-side rendering for Vue 2.0.
6
6
7
+
-[Installation](#installation)
8
+
-[API](#api)
9
+
-[Renderer Options](#renderer-options)
10
+
-[Why Use `bundleRenderer`?](#why-use-bundlerenderer)
11
+
-[Creating the Server Bundle](#creating-the-server-bundle)
12
+
-[Component Caching](#component-caching)
13
+
-[Client Side Hydration](#client-side-hydration)
14
+
7
15
## Installation
8
16
9
17
```bash
@@ -100,31 +108,6 @@ bundleRenderer
100
108
.pipe(writableStream)
101
109
```
102
110
103
-
## Creating the Server Bundle
104
-
105
-
The application bundle can be generated by any build tool, so you can easily use Webpack + `vue-loader` with the bundleRenderer. You do need to use a slightly different webpack config and entry point for your server-side bundle, but the difference is rather minimal:
106
-
107
-
1. add `target: 'node'`, and use `output: { libraryTarget: 'commonjs2' }` for your webpack config.
108
-
109
-
2. In your server-side entry point, export a function. The function will receive the render context object (passed to `bundleRenderer.renderToString` or `bundleRenderer.renderToStream`), and should return a Promise, which should eventually resolve to the app's root Vue instance:
110
-
111
-
```js
112
-
// server-entry.js
113
-
importVuefrom'vue'
114
-
importAppfrom'./App.vue'
115
-
116
-
constapp=newVue(App)
117
-
118
-
// the default export should be a function
119
-
// which will receive the context of the render call
In a typical Node.js app, the server is a long-running process. If we directly require our application code, the instantiated modules will be shared across every request. This imposes some inconvenient restrictions to the application structure: we will have to avoid any use of global stateful singletons (e.g. the store), otherwise state mutations caused by one request will affect the result of the next.
176
+
177
+
Instead, it's more straightforward to run our app "fresh" for each request, so that we don't have to think about avoiding state contamination across requests. This is exactly what `bundleRenderer` helps us achieve.
178
+
179
+
## Creating the Server Bundle
180
+
181
+
The application bundle can be generated by any build tool, so you can easily use Webpack + `vue-loader` with the bundleRenderer. You do need to use a slightly different webpack config and entry point for your server-side bundle, but the difference is rather minimal:
182
+
183
+
1. add `target: 'node'`, and use `output: { libraryTarget: 'commonjs2' }` for your webpack config. Also, it's probably a good idea to [externalize your dependencies](#externals).
184
+
185
+
2. In your server-side entry point, export a function. The function will receive the render context object (passed to `bundleRenderer.renderToString` or `bundleRenderer.renderToStream`), and should return a Promise, which should eventually resolve to the app's root Vue instance:
186
+
187
+
```js
188
+
// server-entry.js
189
+
importVuefrom'vue'
190
+
importAppfrom'./App.vue'
191
+
192
+
constapp=newVue(App)
193
+
194
+
// the default export should be a function
195
+
// which will receive the context of the render call
When using the `bundleRenderer`, we will by default bundle every dependency of our app into the server bundle as well. This means on each request these depdencies will need to be parsed and evaluated again, which is unnecessary in most cases.
207
+
208
+
We can optimize this by externalizing dependencies from your bundle. During the render, any raw `require()` calls found in the bundle will return the actual Node module from your rendering process. With Webpack, we can simply list the modules we want to externalize using the [`externals` config option](https://webpack.github.io/docs/configuration.html#externals):
209
+
210
+
```js
211
+
// webpack.config.js
212
+
module.exports= {
213
+
// this will externalize all modules listed under "dependencies"
Since externalized modules will be shared across every request, you need to make sure that the dependency is **idempotent**. That is, using it across different requests should always yield the same result - it cannot have global state that may be changed by your application. Interactions between externalized modules are fine (e.g. using a Vue plugin).
222
+
223
+
## Component Caching
193
224
194
225
You can easily cache components during SSR by implementing the `serverCacheKey` function:
195
226
196
227
```js
197
228
exportdefault {
229
+
name:'item', // required
198
230
props: ['item'],
199
231
serverCacheKey:props=>props.item.id,
200
232
render (h) {
@@ -203,10 +235,15 @@ export default {
203
235
}
204
236
```
205
237
206
-
The cache key is per-component, and it should contain sufficient information to represent the shape of the render result. The above is a good implementation because the render result is solely determined by `props.item.id`. However, if the render result also relies on another prop, then you need to modify your `getCacheKey` implementation to take that other prop into account.
238
+
Note that cachable component **must also define a unique "name" option**. This is necessary for Vue to determine the identity of the component when using the
239
+
bundle renderer.
240
+
241
+
With a unique name, the cache key is thus per-component: you don't need to worry about two components returning the same key. A cache key should contain sufficient information to represent the shape of the render result. The above is a good implementation if the render result is solely determined by `props.item.id`. However, if the item with the same id may change over time, or if render result also relies on another prop, then you need to modify your `getCacheKey` implementation to take those other variables into account.
207
242
208
243
Returning a constant will cause the component to always be cached, which is good for purely static components.
209
244
245
+
### When to use component caching
246
+
210
247
If the renderer hits a cache for a component during render, it will directly reuse the cached result for the entire sub tree. So **do not cache a component containing child components that rely on global state**.
211
248
212
249
In most cases, you shouldn't and don't need to cache single-instance components. The most common type of components that need caching are ones in big lists. Since these components are usually driven by objects in database collections, they can make use of a simple caching strategy: generate their cache keys using their unique id plus the last updated timestamp:
@@ -215,14 +252,6 @@ In most cases, you shouldn't and don't need to cache single-instance components.
By default, we will bundle every dependency of our app into the server bundle as well. V8 is very good at optimizing running the same code over and over again, so in most cases the cost of re-running it on every request is a worthwhile tradeoff in return for more freedom in application structure.
221
-
222
-
You can also further optimize the re-run cost by externalizing dependencies from your bundle. When running the bundle, any raw `require()` calls found in the bundle will return the actual module from your rendering process. With Webpack, you can simply list the modules you want to externalize using the `externals` config option. This avoids having to re-initialize the same module on each request and can also be beneficial for memory usage.
223
-
224
-
However, since the same module instance will be shared across every request, you need to make sure that the dependency is **idempotent**. That is, using it across different requests should always yield the same result - it cannot have global state that may be changed by your application. Because of this, you should avoid externalizing Vue itself and its plugins.
225
-
226
255
## Client Side Hydration
227
256
228
257
In server-rendered output, the root element will have the `server-rendered="true"` attribute. On the client, when you mount a Vue instance to an element with this attribute, it will attempt to "hydrate" the existing DOM instead of creating new DOM nodes.
0 commit comments