@vue/server-renderer
Advanced tools
Comparing version 3.2.0-beta.6 to 3.2.0-beta.7
@@ -8,3 +8,2 @@ 'use strict'; | ||
var compilerSsr = require('@vue/compiler-ssr'); | ||
var stream = require('stream'); | ||
@@ -498,6 +497,6 @@ /*! ***************************************************************************** | ||
} | ||
function renderToStream(input, context = {}) { | ||
function renderToSimpleStream(input, context, stream) { | ||
if (isVNode$1(input)) { | ||
// raw vnode, wrap with app (for context) | ||
return renderToStream(vue.createApp({ render: () => input }), context); | ||
return renderToSimpleStream(vue.createApp({ render: () => input }), context, stream); | ||
} | ||
@@ -509,13 +508,64 @@ // rendering an app | ||
input.provide(vue.ssrContextKey, context); | ||
const stream$1 = new stream.Readable(); | ||
Promise.resolve(renderComponentVNode(vnode)) | ||
.then(buffer => unrollBuffer$1(buffer, stream$1)) | ||
.then(buffer => unrollBuffer$1(buffer, stream)) | ||
.then(() => { | ||
stream$1.push(null); | ||
stream.push(null); | ||
}) | ||
.catch(error => { | ||
stream$1.destroy(error); | ||
stream.destroy(error); | ||
}); | ||
return stream$1; | ||
return stream; | ||
} | ||
/** | ||
* @deprecated | ||
*/ | ||
function renderToStream(input, context = {}) { | ||
console.warn(`[@vue/server-renderer] renderToStream is deprecated - use renderToNodeStream instead.`); | ||
return renderToNodeStream(input, context); | ||
} | ||
function renderToNodeStream(input, context = {}, UserReadable) { | ||
const stream = UserReadable | ||
? new UserReadable() | ||
: new (require('stream').Readable)() | ||
; | ||
if (!stream) { | ||
throw new Error(`ESM build of renderToStream() requires explicitly passing in the Node.js ` + | ||
`Readable constructor the 3rd argument. Example:\n\n` + | ||
` import { Readable } from 'stream'\n` + | ||
` const stream = renderToStream(app, {}, Readable)`); | ||
} | ||
return renderToSimpleStream(input, context, stream); | ||
} | ||
const hasGlobalWebStream = typeof ReadableStream === 'function'; | ||
function renderToWebStream(input, context = {}, Ctor) { | ||
if (!Ctor && !hasGlobalWebStream) { | ||
throw new Error(`ReadableStream constructor is not avaialbe in the global scope and ` + | ||
`must be explicitly passed in as the 3rd argument:\n\n` + | ||
` import { ReadableStream } from 'stream/web'\n` + | ||
` const stream = renderToWebStream(app, {}, ReadableStream)`); | ||
} | ||
let cancelled = false; | ||
return new (Ctor || ReadableStream)({ | ||
start(controller) { | ||
renderToSimpleStream(input, context, { | ||
push(content) { | ||
if (cancelled) | ||
return; | ||
if (content != null) { | ||
controller.enqueue(content); | ||
} | ||
else { | ||
controller.close(); | ||
} | ||
}, | ||
destroy(err) { | ||
controller.error(err); | ||
} | ||
}); | ||
}, | ||
cancel() { | ||
cancelled = true; | ||
} | ||
}); | ||
} | ||
@@ -894,4 +944,7 @@ function ssrRenderComponent(comp, props = null, children = null, parentComponent = null, slotScopeId) { | ||
exports.renderToNodeStream = renderToNodeStream; | ||
exports.renderToSimpleStream = renderToSimpleStream; | ||
exports.renderToStream = renderToStream; | ||
exports.renderToString = renderToString; | ||
exports.renderToWebStream = renderToWebStream; | ||
exports.ssrGetDynamicModelProps = ssrGetDynamicModelProps; | ||
@@ -898,0 +951,0 @@ exports.ssrInterpolate = ssrInterpolate; |
@@ -8,3 +8,2 @@ 'use strict'; | ||
var compilerSsr = require('@vue/compiler-ssr'); | ||
var stream = require('stream'); | ||
@@ -495,6 +494,6 @@ /*! ***************************************************************************** | ||
} | ||
function renderToStream(input, context = {}) { | ||
function renderToSimpleStream(input, context, stream) { | ||
if (isVNode$1(input)) { | ||
// raw vnode, wrap with app (for context) | ||
return renderToStream(vue.createApp({ render: () => input }), context); | ||
return renderToSimpleStream(vue.createApp({ render: () => input }), context, stream); | ||
} | ||
@@ -506,13 +505,64 @@ // rendering an app | ||
input.provide(vue.ssrContextKey, context); | ||
const stream$1 = new stream.Readable(); | ||
Promise.resolve(renderComponentVNode(vnode)) | ||
.then(buffer => unrollBuffer$1(buffer, stream$1)) | ||
.then(buffer => unrollBuffer$1(buffer, stream)) | ||
.then(() => { | ||
stream$1.push(null); | ||
stream.push(null); | ||
}) | ||
.catch(error => { | ||
stream$1.destroy(error); | ||
stream.destroy(error); | ||
}); | ||
return stream$1; | ||
return stream; | ||
} | ||
/** | ||
* @deprecated | ||
*/ | ||
function renderToStream(input, context = {}) { | ||
console.warn(`[@vue/server-renderer] renderToStream is deprecated - use renderToNodeStream instead.`); | ||
return renderToNodeStream(input, context); | ||
} | ||
function renderToNodeStream(input, context = {}, UserReadable) { | ||
const stream = UserReadable | ||
? new UserReadable() | ||
: new (require('stream').Readable)() | ||
; | ||
if (!stream) { | ||
throw new Error(`ESM build of renderToStream() requires explicitly passing in the Node.js ` + | ||
`Readable constructor the 3rd argument. Example:\n\n` + | ||
` import { Readable } from 'stream'\n` + | ||
` const stream = renderToStream(app, {}, Readable)`); | ||
} | ||
return renderToSimpleStream(input, context, stream); | ||
} | ||
const hasGlobalWebStream = typeof ReadableStream === 'function'; | ||
function renderToWebStream(input, context = {}, Ctor) { | ||
if (!Ctor && !hasGlobalWebStream) { | ||
throw new Error(`ReadableStream constructor is not avaialbe in the global scope and ` + | ||
`must be explicitly passed in as the 3rd argument:\n\n` + | ||
` import { ReadableStream } from 'stream/web'\n` + | ||
` const stream = renderToWebStream(app, {}, ReadableStream)`); | ||
} | ||
let cancelled = false; | ||
return new (Ctor || ReadableStream)({ | ||
start(controller) { | ||
renderToSimpleStream(input, context, { | ||
push(content) { | ||
if (cancelled) | ||
return; | ||
if (content != null) { | ||
controller.enqueue(content); | ||
} | ||
else { | ||
controller.close(); | ||
} | ||
}, | ||
destroy(err) { | ||
controller.error(err); | ||
} | ||
}); | ||
}, | ||
cancel() { | ||
cancelled = true; | ||
} | ||
}); | ||
} | ||
@@ -646,4 +696,7 @@ function ssrRenderComponent(comp, props = null, children = null, parentComponent = null, slotScopeId) { | ||
exports.renderToNodeStream = renderToNodeStream; | ||
exports.renderToSimpleStream = renderToSimpleStream; | ||
exports.renderToStream = renderToStream; | ||
exports.renderToString = renderToString; | ||
exports.renderToWebStream = renderToWebStream; | ||
exports.ssrGetDynamicModelProps = ssrGetDynamicModelProps; | ||
@@ -650,0 +703,0 @@ exports.ssrInterpolate = ssrInterpolate; |
@@ -13,2 +13,9 @@ /// <reference types="node" /> | ||
export declare function renderToNodeStream(input: App | VNode, context?: SSRContext, UserReadable?: typeof Readable): Readable; | ||
export declare function renderToSimpleStream<T extends SimpleReadable>(input: App | VNode, context: SSRContext, stream: T): T; | ||
/** | ||
* @deprecated | ||
*/ | ||
export declare function renderToStream(input: App | VNode, context?: SSRContext): Readable; | ||
@@ -18,2 +25,11 @@ | ||
export declare function renderToWebStream(input: App | VNode, context?: SSRContext, Ctor?: { | ||
new (): ReadableStream; | ||
}): ReadableStream; | ||
export declare interface SimpleReadable { | ||
push(chunk: string | null): void; | ||
destroy(err: any): void; | ||
} | ||
declare type SSRBuffer = SSRBufferItem[] & { | ||
@@ -20,0 +36,0 @@ hasAsync?: boolean; |
{ | ||
"name": "@vue/server-renderer", | ||
"version": "3.2.0-beta.6", | ||
"version": "3.2.0-beta.7", | ||
"description": "@vue/server-renderer", | ||
"main": "index.js", | ||
"module": "dist/server-renderer.esm-bundler.js", | ||
"types": "dist/server-renderer.d.ts", | ||
@@ -12,3 +13,5 @@ "files": [ | ||
"buildOptions": { | ||
"name": "VueServerRenderer", | ||
"formats": [ | ||
"esm-bundler", | ||
"cjs" | ||
@@ -32,8 +35,8 @@ ] | ||
"peerDependencies": { | ||
"vue": "3.2.0-beta.6" | ||
"vue": "3.2.0-beta.7" | ||
}, | ||
"dependencies": { | ||
"@vue/shared": "3.2.0-beta.6", | ||
"@vue/compiler-ssr": "3.2.0-beta.6" | ||
"@vue/shared": "3.2.0-beta.7", | ||
"@vue/compiler-ssr": "3.2.0-beta.7" | ||
} | ||
} |
127
README.md
# @vue/server-renderer | ||
``` js | ||
## Basic API | ||
### `renderToString` | ||
**Signature** | ||
```ts | ||
function renderToString( | ||
input: App | VNode, | ||
context?: SSRContext | ||
): Promise<string> | ||
``` | ||
**Usage** | ||
```js | ||
const { createSSRApp } = require('vue') | ||
@@ -17,1 +32,111 @@ const { renderToString } = require('@vue/server-renderer') | ||
``` | ||
### 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 | ||
function renderToNodeStream(input: App | VNode, context?: SSRContext): Readable | ||
``` | ||
**Usage** | ||
```js | ||
// inside a Node.js http handler | ||
renderToNodeStream(app).pipe(res) | ||
``` | ||
In the ESM build of `@vue/server-renderer`, which is decoupled from Node.js environments, the `Readable` constructor must be explicitly passed in as the 3rd argument: | ||
```js | ||
import { Readable } from 'stream' | ||
renderToNodeStream(app, {}, Readable).pipe(res) | ||
``` | ||
### `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, | ||
context?: SSRContext, | ||
Ctor?: { new (): ReadableStream } | ||
): ReadableStream | ||
``` | ||
**Usage** | ||
```js | ||
// e.g. inside a Cloudflare Worker | ||
return new Response(renderToWebStream(app)) | ||
``` | ||
Note in environments that do not expose `ReadableStream` constructor in the global scope, the constructor must be explicitly passed in as the 3rd argument. For example in Node.js 16.5.0+ where web streams are also supported: | ||
```js | ||
import { ReadableStream } from 'stream/web' | ||
const stream = renderToWebStream(app, {}, ReadableStream) | ||
``` | ||
## `renderToSimpleStream` | ||
Renders input in streaming mode using a simple readable interface. | ||
**Signature** | ||
```ts | ||
function renderToSimpleStream( | ||
input: App | VNode, | ||
context: SSRContext, | ||
options: SimpleReadable | ||
): 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 | ||
} | ||
} | ||
) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104528
8
2600
142
4
+ Added@vue/compiler-core@3.2.0-beta.7(transitive)
+ Added@vue/compiler-dom@3.2.0-beta.7(transitive)
+ Added@vue/compiler-ssr@3.2.0-beta.7(transitive)
+ Added@vue/reactivity@3.2.0-beta.7(transitive)
+ Added@vue/runtime-core@3.2.0-beta.7(transitive)
+ Added@vue/runtime-dom@3.2.0-beta.7(transitive)
+ Added@vue/shared@3.2.0-beta.7(transitive)
+ Addedvue@3.2.0-beta.7(transitive)
- Removed@vue/compiler-core@3.2.0-beta.6(transitive)
- Removed@vue/compiler-dom@3.2.0-beta.6(transitive)
- Removed@vue/compiler-ssr@3.2.0-beta.6(transitive)
- Removed@vue/reactivity@3.2.0-beta.6(transitive)
- Removed@vue/runtime-core@3.2.0-beta.6(transitive)
- Removed@vue/runtime-dom@3.2.0-beta.6(transitive)
- Removed@vue/shared@3.2.0-beta.6(transitive)
- Removedvue@3.2.0-beta.6(transitive)
Updated@vue/shared@3.2.0-beta.7