Skip to content

Commit 12fa1eb

Browse files
committed
update bundleRenderer docs
1 parent c2c8741 commit 12fa1eb

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

packages/vue-server-renderer/README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ app.get('/', (req, res) => {
7676

7777
---
7878

79-
### createBundleRenderer(code, [[rendererOptions](#renderer-options)])
79+
### createBundleRenderer([bundle](#creating-the-server-bundle), [[rendererOptions](#renderer-options)])
8080

81-
Creates a `bundleRenderer` instance using pre-bundled application code (see [Creating the Server Bundle](#creating-the-server-bundle)). For each render call, the code will be re-run in a new context using Node.js' `vm` module. This ensures your application state is discrete between requests, and you don't need to worry about structuring your application in a limiting pattern just for the sake of SSR.
81+
Creates a `bundleRenderer` instance using pre-compiled application bundle (see [Creating the Server Bundle](#creating-the-server-bundle)). For each render call, the code will be re-run in a new context using Node.js' `vm` module. This ensures your application state is discrete between requests, and you don't need to worry about structuring your application in a limiting pattern just for the sake of SSR.
8282

8383
``` js
84-
const bundleRenderer = require('vue-server-renderer').createBundleRenderer(code)
84+
const bundleRenderer = require('vue-server-renderer').createBundleRenderer(bundle)
8585
```
8686

8787
---
@@ -180,11 +180,23 @@ Instead, it's more straightforward to run our app "fresh" for each request, so t
180180

181181
<img width="973" alt="screen shot 2016-08-11 at 6 06 57 pm" src="https://cloud.githubusercontent.com/assets/499550/17607895/786a415a-5fee-11e6-9c11-45a2cfdf085c.png">
182182

183-
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:
183+
The application bundle can be either a string of bundled code, or a special object of the following type:
184184

185-
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).
185+
``` js
186+
type RenderBundle = {
187+
entry: string; // name of the entry file
188+
files: { [filename: string]: string; }; // all files in the bundle
189+
maps: { [filename: string]: string; }; // source maps
190+
}
191+
```
192+
193+
Although theoretically you can use any build tool to generate the bundle, it is recommended to use Webpack + `vue-loader` + [vue-ssr-webpack-plugin](https://github.com/vuejs/vue-ssr-webpack-plugin) for this purpose. The usual workflow is setting up a base webpack configuration file for the client-side, then modify it to generate the server-side bundle with the following changes:
186194

187-
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:
195+
1. Set `target: 'node'` and `output: { libraryTarget: 'commonjs2' }` in your webpack config.
196+
197+
2. Add [vue-ssr-webpack-plugin](https://github.com/vuejs/vue-ssr-webpack-plugin) to your webpack plugins. This plugin automatically generates the bundle as a single JSON file which contains all the files and source maps of the entire bundle. This is particularly important if you use Webpack's code-splitting features that result in a multi-file bundle.
198+
199+
3. 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:
188200

189201
``` js
190202
// server-entry.js
@@ -203,6 +215,8 @@ The application bundle can be generated by any build tool, so you can easily use
203215
}
204216
```
205217

218+
4. It's also a good idea to externalize your dependencies (see below).
219+
206220
### Externals
207221

208222
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.

src/server/create-bundle-renderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ const INVALID_MSG =
1717

1818
// The render bundle can either be a string (single bundled file)
1919
// or a bundle manifest object generated by vue-ssr-webpack-plugin.
20-
type RenderBundle = string | {
20+
type RenderBundle = {
2121
entry: string;
2222
files: { [filename: string]: string; };
2323
maps: { [filename: string]: string; };
2424
};
2525

2626
export function createBundleRendererCreator (createRenderer: () => Renderer) {
27-
return (bundle: RenderBundle, rendererOptions?: RenderOptions) => {
27+
return (bundle: string | RenderBundle, rendererOptions?: RenderOptions) => {
2828
const renderer = createRenderer(rendererOptions)
2929
let files, entry, maps
3030
if (typeof bundle === 'object') {

0 commit comments

Comments
 (0)