|
| 1 | +# API 参考 |
| 2 | + |
| 3 | +## `createRenderer([options])` |
| 4 | + |
| 5 | +Create a [`Renderer`](#class-renderer) instance with (optional) [options](#renderer-options). |
| 6 | + |
| 7 | +```js |
| 8 | +const { createRenderer } = require('vue-server-renderer') |
| 9 | +const renderer = createRenderer({ ... }) |
| 10 | +``` |
| 11 | + |
| 12 | +## `createBundleRenderer(bundle[, options])` |
| 13 | + |
| 14 | +Create a [`BundleRenderer`](#class-bundlerenderer) instance with a server bundle and (optional) [options](#renderer-options). |
| 15 | + |
| 16 | +```js |
| 17 | +const { createBundleRenderer } = require('vue-server-renderer') |
| 18 | +const renderer = createBundleRenderer(serverBundle, { ... }) |
| 19 | +``` |
| 20 | + |
| 21 | +The `serverBundle` argument can be one of the following: |
| 22 | + |
| 23 | +- 绝对路径,指向一个已经构建好的 bundle 文件(`.js` 或 `.json`)。必须以 `/` 开头才会被识别为文件路径。 |
| 24 | +- A bundle object generated by webpack + `vue-server-renderer/server-plugin`. |
| 25 | +- A string of JavaScript code (not recommended). |
| 26 | + |
| 27 | +See [Introducing the Server Bundle](./bundle-renderer.md) and [Build Configuration](./build-config.md) for more details. |
| 28 | + |
| 29 | +## `Class: Renderer` |
| 30 | + |
| 31 | +- #### `renderer.renderToString(vm[, context], callback)` |
| 32 | + |
| 33 | + 将 Vue 实例渲染为字符串。上下文对象(context object)可选。回调函数是典型的 Node.js 风格回调,其中第一个参数是 error,第二个参数是渲染的字符串。 |
| 34 | + |
| 35 | +- #### `renderer.renderToStream(vm[, context])` |
| 36 | + |
| 37 | + Render a Vue instance to a Node.js stream. The context object is optional. See [Streaming](./streaming.md) for more details. |
| 38 | + |
| 39 | +## `Class: BundleRenderer` |
| 40 | + |
| 41 | +- #### `bundleRenderer.renderToString([context, ]callback)` |
| 42 | + |
| 43 | + Render the bundle to a string. The context object is optional. The callback is a typical Node.js style callback where the first argument is the error and the second argument is the rendered string. |
| 44 | + |
| 45 | +- #### `bundleRenderer.renderToStream([context])` |
| 46 | + |
| 47 | + Render the bundle to a Node.js stream. The context object is optional. See [Streaming](./streaming.md) for more details. |
| 48 | + |
| 49 | +## Renderer Options |
| 50 | + |
| 51 | +- #### `template` |
| 52 | + |
| 53 | + Provide a template for the entire page's HTML. The template should contain a comment `<!--vue-ssr-outlet-->` which serves as the placeholder for rendered app content. |
| 54 | + |
| 55 | + The template also supports basic interpolation using the render context: |
| 56 | + |
| 57 | +- Use double-mustache for HTML-escaped interpolation; |
| 58 | +- Use triple-mustache for non-HTML-escaped interpolation. |
| 59 | + |
| 60 | + The template automatically injects appropriate content when certain data is found on the render context: |
| 61 | + |
| 62 | +- `context.head`: (string) any head markup that should be injected into the head of the page. |
| 63 | +- `context.styles`: (string) any inline CSS that should be injected into the head of the page. Note this property will be automatically populated if using `vue-loader` + `vue-style-loader` for component CSS. |
| 64 | +- `context.state`: (Object) initial Vuex store state that should be inlined in the page as `window.__INITIAL_STATE__`. The inlined JSON is automatically sanitized with [serialize-javascript](https://github.com/yahoo/serialize-javascript) to prevent XSS. |
| 65 | + |
| 66 | + In addition, when `clientManifest` is also provided, the template automatically injects the following: |
| 67 | + |
| 68 | +- Client-side JavaScript and CSS assets needed by the render (with async chunks automatically inferred); |
| 69 | +- Optimal `<link rel="preload/prefetch">` resource hints for the rendered page. |
| 70 | + |
| 71 | + You can disable all automatic injections by also passing `inject: false` to the renderer. |
| 72 | + |
| 73 | + See also: |
| 74 | + |
| 75 | +- [Using a Page Template](./basic.md#using-a-page-template) |
| 76 | +- [Manual Asset Injection](./build-config.md#manual-asset-injection) |
| 77 | + - #### `clientManifest` |
| 78 | +- 2.3.0+ |
| 79 | + |
| 80 | + Provide a client build manifest object generated by `vue-server-renderer/client-plugin`. The client manifest provides the bundle renderer with the proper information for automatic asset injection into the HTML template. For more details, see [Generating clientManifest](./build-config.md#generating-clientmanifest). |
| 81 | + |
| 82 | +- |
| 83 | +#### `inject` |
| 84 | + |
| 85 | + - 2.3.0+ |
| 86 | + |
| 87 | + Controls whether to perform automatic injections when using `template`. Defaults to `true`. |
| 88 | + |
| 89 | + See also: [Manual Asset Injection](./build-config.md#manual-asset-injection). |
| 90 | + |
| 91 | +- |
| 92 | +#### `shouldPreload` |
| 93 | + |
| 94 | + - 2.3.0+ |
| 95 | + |
| 96 | + A function to control what files should have `<link rel="preload">` resource hints generated. |
| 97 | + |
| 98 | + By default, only JavaScript and CSS files will be preloaded, as they are absolutely needed for your application to boot. |
| 99 | + |
| 100 | + For other types of assets such as images or fonts, preloading too much may waste bandwidth and even hurt performance, so what to preload will be scenario-dependent. You can control precisely what to preload using the `shouldPreload` option: |
| 101 | + |
| 102 | +```js |
| 103 | + const renderer = createBundleRenderer(bundle, { |
| 104 | + template, |
| 105 | + clientManifest, |
| 106 | + shouldPreload: (file, type) => { |
| 107 | + // type is inferred based on the file extension. |
| 108 | + // https://fetch.spec.whatwg.org/#concept-request-destination |
| 109 | + if (type === 'script' || type === 'style') { |
| 110 | + return true |
| 111 | + } |
| 112 | + if (type === 'font') { |
| 113 | + // only preload woff2 fonts |
| 114 | + return /\.woff2$/.test(file) |
| 115 | + } |
| 116 | + if (type === 'image') { |
| 117 | + // only preload important images |
| 118 | + return file === 'hero.jpg' |
| 119 | + } |
| 120 | + } |
| 121 | + }) |
| 122 | +``` |
| 123 | + |
| 124 | +- |
| 125 | +#### `runInNewContext` |
| 126 | + |
| 127 | + - 2.3.0+ |
| 128 | + - only used in `createBundleRenderer` |
| 129 | + - Expects: `boolean | 'once'` (`'once'` only supported in 2.3.1+) |
| 130 | + |
| 131 | + By default, for each render the bundle renderer will create a fresh V8 context and re-execute the entire bundle. This has some benefits - for example, the app code is isolated from the server process and we don't need to worry about the [stateful singleton problem](./structure.md#avoid-stateful-singletons) mentioned in the docs. However, this mode comes at some considerable performance cost because re-executing the bundle is expensive especially when the app gets bigger. |
| 132 | + |
| 133 | + This option defaults to `true` for backwards compatibility, but it is recommended to use `runInNewContext: false` or `runInNewContext: 'once'` whenever you can. |
| 134 | + |
| 135 | +> In 2.3.0 this option has a bug where `runInNewContext: false` still executes the bundle using a separate global context. The following information assumes version 2.3.1+. |
| 136 | +
|
| 137 | + With `runInNewContext: false`, the bundle code will run in the same `global` context with the server process, so be careful about code that modifies `global` in your application code. |
| 138 | + |
| 139 | + With `runInNewContext: 'once'` (2.3.1+), the bundle is evaluated in a separate `global` context, however only once at startup. This provides better app code isolation since it prevents the bundle from accidentally polluting the server process' `global` object. The caveats are that: |
| 140 | + |
| 141 | +1. Dependencies that modifies `global` (e.g. polyfills) cannot be externalized in this mode; |
| 142 | +2. Values returned from the bundle execution will be using different global constructors, e.g. an error caught inside the bundle will not be an instance of `Error` in the server process. |
| 143 | + |
| 144 | + See also: [Source Code Structure](./structure.md) |
| 145 | + |
| 146 | +- |
| 147 | +#### `basedir` |
| 148 | + |
| 149 | + - 2.2.0+ |
| 150 | + - only used in `createBundleRenderer` |
| 151 | + |
| 152 | + Explicitly declare the base directory for the server bundle to resolve `node_modules` dependencies from. This is only needed if your generated bundle file is placed in a different location from where the externalized NPM dependencies are installed, or your `vue-server-renderer` is npm-linked into your current project. |
| 153 | + |
| 154 | +- #### `cache` |
| 155 | + |
| 156 | + Provide a [component cache](./caching.md#component-level-caching) implementation. The cache object must implement the following interface (using Flow notations): |
| 157 | + |
| 158 | +```js |
| 159 | + type RenderCache = { |
| 160 | + get: (key: string, cb?: Function) => string | void; |
| 161 | + set: (key: string, val: string) => void; |
| 162 | + has?: (key: string, cb?: Function) => boolean | void; |
| 163 | + }; |
| 164 | +``` |
| 165 | + |
| 166 | + A typical usage is passing in an [lru-cache](https://github.com/isaacs/node-lru-cache): |
| 167 | + |
| 168 | +```js |
| 169 | + const LRU = require('lru-cache') |
| 170 | + const renderer = createRenderer({ |
| 171 | + cache: LRU({ |
| 172 | + max: 10000 |
| 173 | + }) |
| 174 | + }) |
| 175 | +``` |
| 176 | + |
| 177 | + Note that the cache object should at least implement `get` and `set`. In addition, `get` and `has` can be optionally async if they accept a second argument as callback. This allows the cache to make use of async APIs, e.g. a redis client: |
| 178 | + |
| 179 | +```js |
| 180 | + const renderer = createRenderer({ |
| 181 | + cache: { |
| 182 | + get: (key, cb) => { |
| 183 | + redisClient.get(key, (err, res) => { |
| 184 | + // handle error if any |
| 185 | + cb(res) |
| 186 | + }) |
| 187 | + }, |
| 188 | + set: (key, val) => { |
| 189 | + redisClient.set(key, val) |
| 190 | + } |
| 191 | + } |
| 192 | + }) |
| 193 | +``` |
| 194 | + |
| 195 | +- #### `directives` |
| 196 | + |
| 197 | + Allows you to provide server-side implementations for your custom directives: |
| 198 | + |
| 199 | +```js |
| 200 | + const renderer = createRenderer({ |
| 201 | + directives: { |
| 202 | + example (vnode, directiveMeta) { |
| 203 | + // transform vnode based on directive binding metadata |
| 204 | + } |
| 205 | + } |
| 206 | + }) |
| 207 | +``` |
| 208 | + |
| 209 | + As an example, check out [`v-show`'s server-side implementation](https://github.com/vuejs/vue/blob/dev/src/platforms/web/server/directives/show.js). |
| 210 | + |
| 211 | +## webpack Plugins |
| 212 | + |
| 213 | +The webpack plugins are provided as standalone files and should be required directly: |
| 214 | + |
| 215 | +```js |
| 216 | +const VueSSRServerPlugin = require('vue-server-renderer/server-plugin') |
| 217 | +const VueSSRClientPlugin = require('vue-server-renderer/client-plugin') |
| 218 | +``` |
| 219 | + |
| 220 | +The default files generated are: |
| 221 | + |
| 222 | +- `vue-ssr-server-bundle.json` for the server plugin; |
| 223 | +- `vue-ssr-client-manifest.json` for the client plugin. |
| 224 | + |
| 225 | +The filenames can be customized when creating the plugin instances: |
| 226 | + |
| 227 | +```js |
| 228 | +const plugin = new VueSSRServerPlugin({ |
| 229 | + filename: 'my-server-bundle.json' |
| 230 | +}) |
| 231 | +``` |
| 232 | + |
| 233 | +See [Build Configuration](./build-config.md) for more information. |
0 commit comments