You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These new APIs improve on `renderToString` by waiting for data to load for static HTML generation. They are designed to work with streaming environments like Node.js Streams and Web Streams. For example, in a Web Stream environment, you can prerender a React tree to static HTML with `prerender`:
322
+
323
+
```js
324
+
import { prerender } from'react-dom/static';
325
+
326
+
asyncfunctionhandler(request) {
327
+
const {prelude} =awaitprerender(<App />, {
328
+
bootstrapScripts: ['/main.js']
329
+
});
330
+
returnnewResponse(prelude, {
331
+
headers: { 'content-type':'text/html' },
332
+
});
333
+
}
334
+
```
335
+
336
+
Prerender APIs will wait for all data to load before returning the static HTML stream. Streams can be converted to strings, or sent with a streaming response. They do not support streaming content as it loads, which is supported by the existing [React DOM server rendering APIs](/reference/react-dom/server).
337
+
338
+
For more information, see [React DOM Static APIs](/reference/react-dom/static).
315
339
316
340
## React Server Components {/*react-server-components*/}
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/server/index.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Server React DOM APIs
4
4
5
5
<Intro>
6
6
7
-
The `react-dom/server` APIs let you render React components to HTML on the server. These APIs are only used on the server at the top level of your app to generate the initial HTML. A [framework](/learn/start-a-new-react-project#production-grade-react-frameworks) may call them for you. Most of your components don't need to import or use them.
7
+
The `react-dom/server` APIs let you server-side render React components to HTML. These APIs are only used on the server at the top level of your app to generate the initial HTML. A [framework](/learn/start-a-new-react-project#production-grade-react-frameworks) may call them for you. Most of your components don't need to import or use them.
8
8
9
9
</Intro>
10
10
@@ -26,7 +26,7 @@ These methods are only available in the environments with [Web Streams](https://
26
26
27
27
---
28
28
29
-
## Server APIs for non-streaming environments {/*server-apis-for-non-streaming-environments*/}
29
+
## Legacy Server APIs for non-streaming environments {/*legacy-server-apis-for-non-streaming-environments*/}
30
30
31
31
These methods can be used in the environments that don't support streams:
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/server/renderToString.md
+16-3
Original file line number
Diff line number
Diff line change
@@ -86,9 +86,9 @@ This will produce the initial non-interactive HTML output of your React componen
86
86
87
87
## Alternatives {/*alternatives*/}
88
88
89
-
### Migrating from `renderToString` to a streaming method on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
89
+
### Migrating from `renderToString` to a streaming render on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
90
90
91
-
`renderToString` returns a string immediately, so it does not support streaming or waiting for data.
91
+
`renderToString` returns a string immediately, so it does not support streaming content as it loads.
92
92
93
93
When possible, we recommend using these fully-featured alternatives:
94
94
@@ -99,6 +99,19 @@ You can continue using `renderToString` if your server environment does not supp
99
99
100
100
---
101
101
102
+
### Migrating from `renderToString` to a streaming render on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
103
+
104
+
`renderToString` returns a string immediately, so it does not support waiting for data to load for static HTML generation.
105
+
106
+
We recommend using these fully-featured alternatives:
107
+
108
+
* If you use Node.js, use [`prerenderToNodeStream`.](/reference/react-dom/static/prerenderToNodeStream)
109
+
* If you use Deno or a modern edge runtime with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API), use [`prerender`.](/reference/react-dom/static/prerender)
110
+
111
+
You can continue using `renderToString` if your static site generation environment does not support streams.
112
+
113
+
---
114
+
102
115
### Removing `renderToString` from the client code {/*removing-rendertostring-from-the-client-code*/}
103
116
104
117
Sometimes, `renderToString` is used on the client to convert some component to HTML.
@@ -137,5 +150,5 @@ The [`flushSync`](/reference/react-dom/flushSync) call is necessary so that the
137
150
138
151
If some component suspends (for example, because it's defined with [`lazy`](/reference/react/lazy) or fetches data), `renderToString` will not wait for its content to resolve. Instead, `renderToString` will find the closest [`<Suspense>`](/reference/react/Suspense) boundary above it and render its `fallback` prop in the HTML. The content will not appear until the client code loads.
139
152
140
-
To solve this, use one of the [recommended streaming solutions.](#migrating-from-rendertostring-to-a-streaming-method-on-the-server) They can stream content in chunks as it resolves on the server so that the user sees the page being progressively filled in before the client code loads.
153
+
To solve this, use one of the [recommended streaming solutions.](#migrating-from-rendertostring-to-a-streaming-method-on-the-server) For server side rendering, they can stream content in chunks as it resolves on the server so that the user sees the page being progressively filled in before the client code loads. For static site generation, they can wait for all the content to resolve before generating the static HTML.
The `react-dom/static` APIs let you generate static HTML for React components. They have limited functionality compared to the streaming APIs. A [framework](/learn/start-a-new-react-project#production-grade-react-frameworks) may call them for you. Most of your components don't need to import or use them.
8
+
9
+
</Intro>
10
+
11
+
---
12
+
13
+
## Static APIs for Web Streams {/*static-apis-for-web-streams*/}
14
+
15
+
These methods are only available in the environments with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API), which includes browsers, Deno, and some modern edge runtimes:
16
+
17
+
*[`prerender`](/reference/react-dom/server/renderToReadableStream) renders a React tree to static HTML with a [Readable Web Stream.](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
18
+
19
+
20
+
---
21
+
22
+
## Static APIs for Node.js Streams {/*static-apis-for-nodejs-streams*/}
23
+
24
+
These methods are only available in the environments with [Node.js Streams:](https://nodejs.org/api/stream.html)
25
+
26
+
*[`prerenderToNodeStream`](/reference/react-dom/server/renderToPipeableStream) renders a React tree to static HTML with a [Node.js Stream.](https://nodejs.org/api/stream.html)
0 commit comments