Skip to content

Commit c0ef494

Browse files
authored
[18] renderToPipeableStream doc (#4485)
* new streaming ssr api * add readable stream * code snippets
1 parent 931e2fc commit c0ef494

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

content/docs/reference-react-dom-server.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ The following methods can be used in both the server and browser environments:
2424

2525
These additional methods depend on a package (`stream`) that is **only available on the server**, and won't work in the browser.
2626

27-
- [`renderToNodeStream()`](#rendertonodestream)
27+
- [`renderToPipeableStream()`](#rendertopipeablestream)
28+
- [`renderToReadableStream()`](#rendertoreadablestream)
29+
- [`renderToNodeStream()`](#rendertonodestream) (Deprecated)
2830
- [`renderToStaticNodeStream()`](#rendertostaticnodestream)
2931

3032
* * *
@@ -55,7 +57,84 @@ If you plan to use React on the client to make the markup interactive, do not us
5557

5658
* * *
5759

58-
### `renderToNodeStream()` {#rendertonodestream}
60+
### `renderToPipeableStream()` {#rendertopipeablestream}
61+
62+
```javascript
63+
ReactDOMServer.renderToPipeableStream(element, options)
64+
```
65+
66+
Render a React element to its initial HTML. Returns a [Control object](https://github.com/facebook/react/blob/3f8990898309c61c817fbf663f5221d9a00d0eaa/packages/react-dom/src/server/ReactDOMFizzServerNode.js#L49-L54) that allows you to pipe the output or abort the request. Fully supports Suspense and streaming of HTML with "delayed" content blocks "popping in" later through javascript execution. [Read more](https://github.com/reactwg/react-18/discussions/37)
67+
68+
If you call [`ReactDOM.hydrateRoot()`](/docs/react-dom-client.html#hydrateroot) on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.
69+
70+
> Note:
71+
>
72+
> This is a Node.js specific API and modern server environments should use renderToReadableStream instead.
73+
>
74+
75+
```
76+
const {pipe, abort} = renderToPipeableStream(
77+
<App />,
78+
{
79+
onAllReady() {
80+
res.statusCode = 200;
81+
res.setHeader('Content-type', 'text/html');
82+
pipe(res);
83+
},
84+
onShellError(x) {
85+
res.statusCode = 500;
86+
res.send(
87+
'<!doctype html><p>Loading...</p><script src="clientrender.js"></script>'
88+
);
89+
}
90+
}
91+
);
92+
```
93+
94+
* * *
95+
96+
### `renderToReadableStream()` {#rendertoreadablestream}
97+
98+
```javascript
99+
ReactDOMServer.renderToReadableStream(element, options);
100+
```
101+
102+
Streams a React element to its initial HTML. Returns a [Readable Stream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream). Fully supports Suspense and streaming of HTML. [Read more](https://github.com/reactwg/react-18/discussions/127)
103+
104+
If you call [`ReactDOM.hydrateRoot()`](/docs/react-dom-client.html#hydrateroot) on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.
105+
106+
```
107+
let controller = new AbortController();
108+
try {
109+
let stream = await renderToReadableStream(
110+
<html>
111+
<body>Success</body>
112+
</html>,
113+
{
114+
signal: controller.signal,
115+
}
116+
);
117+
118+
// This is to wait for all suspense boundaries to be ready. You can uncomment
119+
// this line if you don't want to stream to the client
120+
// await stream.allReady;
121+
122+
return new Response(stream, {
123+
headers: {'Content-Type': 'text/html'},
124+
});
125+
} catch (error) {
126+
return new Response(
127+
'<!doctype html><p>Loading...</p><script src="clientrender.js"></script>',
128+
{
129+
status: 500,
130+
headers: {'Content-Type': 'text/html'},
131+
}
132+
);
133+
}
134+
```
135+
* * *
136+
137+
### `renderToNodeStream()` {#rendertonodestream} (Deprecated)
59138

60139
```javascript
61140
ReactDOMServer.renderToNodeStream(element)

0 commit comments

Comments
 (0)