2018-10-27 03:44:50 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								# @vue/server-renderer
 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-19 03:56:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-22 10:55:10 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								**Note: as of 3.2.13+, this package is included as a dependency of the main `vue`  package and can be accessed as `vue/server-renderer` . This means you no longer need to explicitly install this package and ensure its version match that of `vue` 's. Just use the `vue/server-renderer`  deep import instead.**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## Basic API
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### `renderToString`
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Signature**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function renderToString(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  input: App | VNode,
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  context?: SSRContext,
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								): Promise< string > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-02-19 03:56:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								const { createSSRApp } = require('vue')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const { renderToString } = require('@vue/server-renderer')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const app = createSSRApp({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  data: () => ({ msg: 'hello' }),
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  template: `<div>{{ msg }}</div>` ,
							 
						 
					
						
							
								
									
										
										
										
											2020-02-19 03:56:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								;(async () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  const html = await renderToString(app)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  console.log(html)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								})()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### Handling Teleports
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								If the rendered app contains teleports, the teleported content will not be part of the rendered string. Instead, they are exposed under the `teleports`  property of the ssr context object:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const ctx = {}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const html = await renderToString(app, ctx)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								console.log(ctx.teleports) // { '#teleported': 'teleported content' }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Streaming API
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### `renderToNodeStream`
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Renders input as a [Node.js Readable stream ](https://nodejs.org/api/stream.html#stream_class_stream_readable ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Signature**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```ts
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								function renderToNodeStream(input: App | VNode, context?: SSRContext): Readable
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// inside a Node.js http handler
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								renderToNodeStream(app).pipe(res)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								**Note:** This method is not supported in the ESM build of `@vue/server-renderer` , which is decoupled from Node.js environments. Use `pipeToNodeWritable`  instead.
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								### `pipeToNodeWritable`
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Render and pipe to an existing [Node.js Writable stream ](https://nodejs.org/api/stream.html#stream_writable_streams ) instance.
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								**Signature**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function pipeToNodeWritable(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  input: App | VNode,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  context: SSRContext = {},
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  writable: Writable,
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								): void
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// inside a Node.js http handler
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								pipeToNodeWritable(app, {}, res)
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### `renderToWebStream`
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Renders input as a [Web ReadableStream ](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Signature**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function renderToWebStream(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  input: App | VNode,
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  context?: SSRContext,
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								): ReadableStream
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// inside an environment with ReadableStream support
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								return new Response(renderToWebStream(app))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								**Note:** in environments that do not expose `ReadableStream`  constructor in the global scope, `pipeToWebWritable`  should be used instead.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### `pipeToWebWritable`
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Render and pipe to an existing [Web WritableStream ](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream ) instance.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Signature**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function pipeToWebWritable(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  input: App | VNode,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  context: SSRContext = {},
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  writable: WritableStream,
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								): void
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								This is typically used in combination with [`TransformStream` ](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream ):
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// TransformStream is available in environments such as CloudFlare workers.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// in Node.js, TransformStream needs to be explicitly imported from 'stream/web'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const { readable, writable } = new TransformStream()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								pipeToWebWritable(app, {}, writable)
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								return new Response(readable)
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-07 00:41:40 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								### `renderToSimpleStream`
 
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Renders input in streaming mode using a simple readable interface.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Signature**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```ts
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								function renderToSimpleStream(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  input: App | VNode,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  context: SSRContext,
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  options: SimpleReadable,
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								): SimpleReadable
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								interface SimpleReadable {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  push(content: string | null): void
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  destroy(err: any): void
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								**Usage**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								let res = ''
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								renderToSimpleStream(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  app,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  {},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    push(chunk) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if (chunk === null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        // done
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        console(`render complete: ${res}`)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        res += chunk
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    destroy(err) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      // error encountered
							 
						 
					
						
							
								
									
										
										
										
											2024-08-07 10:57:18 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  },
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 01:12:50 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```