resumeAndPrerenderToNodeStream
使用 Node.js 流。 将预渲染的 React 树继续为静态 HTML 字符串。
¥resumeAndPrerenderToNodeStream
continues a prerendered React tree to a static HTML string using a a Node.js Stream..
const {prelude, postponed} = await resumeAndPrerenderToNodeStream(reactNode, postponedState, options?)
参考
¥Reference
resumeAndPrerenderToNodeStream(reactNode, postponedState, options?)
调用 resumeAndPrerenderToNodeStream
将预渲染的 React 树延续到静态 HTML 字符串。
¥Call resumeAndPrerenderToNodeStream
to continue a prerendered React tree to a static HTML string.
import { resumeAndPrerenderToNodeStream } from 'react-dom/static';
import { getPostponedState } from 'storage';
async function handler(request, writable) {
const postponedState = getPostponedState(request);
const { prelude } = await resumeAndPrerenderToNodeStream(<App />, JSON.parse(postponedState));
prelude.pipe(writable);
}
在客户端,调用 hydrateRoot
使服务器生成的 HTML 具有交互性。
¥On the client, call hydrateRoot
to make the server-generated HTML interactive.
参数
¥Parameters
-
reactNode
:调用prerender
(或之前的resumeAndPrerenderToNodeStream
)的 React 节点。例如,像<App />
这样的 JSX 元素。它应该代表整个文档,所以App
组件应该渲染<html>
标签。¥
reactNode
: The React node you calledprerender
(or a previousresumeAndPrerenderToNodeStream
) with. For example, a JSX element like<App />
. It is expected to represent the entire document, so theApp
component should render the<html>
tag. -
postponedState
:从 预渲染 API 返回的不透明postpone
对象,从存储它的任何位置加载(例如,Redis、文件或 S3)。¥
postponedState
: The opaquepostpone
object returned from a prerender API, loaded from wherever you stored it (e.g. redis, a file, or S3). -
可选
options
:具有流选项的对象。¥optional
options
: An object with streaming options.-
可选
signal
:一个 中止信号,它允许你 中止服务器渲染 并在客户端上渲染其余部分。¥optional
signal
: An abort signal that lets you abort server rendering and render the rest on the client. -
可选
onError
:每当出现服务器错误时都会触发的回调,无论是 recoverable 还是 不。 默认情况下,这只会调用console.error
。如果你将其改写为 记录崩溃报告,,请确保你仍然调用console.error
。¥optional
onError
: A callback that fires whenever there is a server error, whether recoverable or not. By default, this only callsconsole.error
. If you override it to log crash reports, make sure that you still callconsole.error
.
-
返回
¥Returns
resumeAndPrerenderToNodeStream
返回一个 Promise:
¥resumeAndPrerenderToNodeStream
returns a Promise:
-
如果渲染成功,则 Promise 将解析为包含以下内容的对象:
¥If rendering the is successful, the Promise will resolve to an object containing:
-
prelude
:Web 流 的 HTML。你可以使用此流分块发送响应,也可以将整个流读入字符串。¥
prelude
: a Web Stream of HTML. You can use this stream to send a response in chunks, or you can read the entire stream into a string. -
postponed
:一个 JSON 可序列化的不透明对象,如果resumeAndPrerenderToNodeStream
中止,则可以传递给resumeToNodeStream
或resumeAndPrerenderToNodeStream
。¥
postponed
: an JSON-serializeable, opaque object that can be passed toresumeToNodeStream
orresumeAndPrerenderToNodeStream
ifresumeAndPrerenderToNodeStream
is aborted.
-
-
如果渲染失败,则 Promise 将被拒绝。使用它来输出回退 shell。
¥If rendering fails, the Promise will be rejected. Use this to output a fallback shell.
注意事项
¥Caveats
nonce
在预渲染时不可用。每个请求的 nonce 必须是唯一的,如果你使用 nonce 来通过 CSP 保护你的应用,则将 nonce 值包含在预渲染本身中是不合适且不安全的。
¥nonce
is not an available option when prerendering. Nonces must be unique per request and if you use nonces to secure your application with CSP it would be inappropriate and insecure to include the nonce value in the prerender itself.
用法
¥Usage
延伸阅读
¥Further reading
resumeAndPrerenderToNodeStream
的行为与 prerender
类似,但可用于继续先前启动但已中止的预渲染进程。有关恢复预渲染树的更多信息,请参阅 恢复文档。
¥resumeAndPrerenderToNodeStream
behaves similarly to prerender
but can be used to continue a previously started prerendering process that was aborted.
For more information about resuming a prerendered tree, see the resume documentation.