Skip to content

Commit 0ee1b77

Browse files
committed
Add prerender APIs
1 parent 3c6c1fd commit 0ee1b77

File tree

7 files changed

+678
-5
lines changed

7 files changed

+678
-5
lines changed

src/content/blog/2024/04/25/react-19.md

+24
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,30 @@ The `use` API can only be called in render, similar to hooks. Unlike hooks, `use
312312

313313
For more information, see the docs for [`use`](/reference/react/use).
314314

315+
## New React DOM Static APIs {/*new-react-dom-static-apis*/}
316+
317+
We've added two new APIs to `react-dom/static` for static site generation:
318+
- [`prerender`](/reference/react-dom/static/prerender)
319+
- [`prerenderToNodeStream`](/reference/react-dom/static/prerenderToNodeStream)
320+
321+
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+
async function handler(request) {
327+
const {prelude} = await prerender(<App />, {
328+
bootstrapScripts: ['/main.js']
329+
});
330+
return new Response(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).
315339

316340
## React Server Components {/*react-server-components*/}
317341

src/content/reference/react-dom/server/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Server React DOM APIs
44

55
<Intro>
66

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.
88

99
</Intro>
1010

@@ -26,7 +26,7 @@ These methods are only available in the environments with [Web Streams](https://
2626

2727
---
2828

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*/}
3030

3131
These methods can be used in the environments that don't support streams:
3232

src/content/reference/react-dom/server/renderToString.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ This will produce the initial non-interactive HTML output of your React componen
8686
8787
## Alternatives {/*alternatives*/}
8888
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*/}
9090
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.
9292
9393
When possible, we recommend using these fully-featured alternatives:
9494
@@ -99,6 +99,19 @@ You can continue using `renderToString` if your server environment does not supp
9999
100100
---
101101
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+
102115
### Removing `renderToString` from the client code {/*removing-rendertostring-from-the-client-code*/}
103116
104117
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
137150
138151
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.
139152
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.
141154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Static React DOM APIs
3+
---
4+
5+
<Intro>
6+
7+
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)
27+
28+

0 commit comments

Comments
 (0)