Skip to content

Commit

Permalink
Add prerender APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhanlonii committed Dec 5, 2024
1 parent 3c6c1fd commit 76a46c1
Show file tree
Hide file tree
Showing 7 changed files with 678 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/content/blog/2024/04/25/react-19.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,30 @@ The `use` API can only be called in render, similar to hooks. Unlike hooks, `use

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

## New React DOM Static APIs {/*new-react-dom-static-apis*/}

We've added two new APIs to `react-dom/static` for static site generation:
- [`prerender`](/reference/react-dom/static/prerender)
- [`prerenderToNodeStream`](/reference/react-dom/static/prerenderToNodeStream)

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`:

```js
import { prerender } from 'react-dom/static';

async function handler(request) {
const {prelude} = await prerender(<App />, {
bootstrapScripts: ['/main.js']
});
return new Response(prelude, {
headers: { 'content-type': 'text/html' },
});
}
```

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

For more information, see [React DOM Static APIs](/reference/react-dom/static).

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

Expand Down
4 changes: 2 additions & 2 deletions src/content/reference/react-dom/server/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Server React DOM APIs

<Intro>

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

</Intro>

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

---

## Server APIs for non-streaming environments {/*server-apis-for-non-streaming-environments*/}
## Legacy Server APIs for non-streaming environments {/*legacy-server-apis-for-non-streaming-environments*/}

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

Expand Down
19 changes: 16 additions & 3 deletions src/content/reference/react-dom/server/renderToString.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ This will produce the initial non-interactive HTML output of your React componen
## Alternatives {/*alternatives*/}
### Migrating from `renderToString` to a streaming method on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
### Migrating from `renderToString` to a streaming render on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
`renderToString` returns a string immediately, so it does not support streaming or waiting for data.
`renderToString` returns a string immediately, so it does not support streaming content as it loads.
When possible, we recommend using these fully-featured alternatives:
Expand All @@ -99,6 +99,19 @@ You can continue using `renderToString` if your server environment does not supp
---
### Migrating from `renderToString` to a static prerender on the server {/*migrating-from-rendertostring-to-a-static-prerender-on-the-server*/}
`renderToString` returns a string immediately, so it does not support waiting for data to load for static HTML generation.
We recommend using these fully-featured alternatives:
* If you use Node.js, use [`prerenderToNodeStream`.](/reference/react-dom/static/prerenderToNodeStream)
* 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)
You can continue using `renderToString` if your static site generation environment does not support streams.
---
### Removing `renderToString` from the client code {/*removing-rendertostring-from-the-client-code*/}
Sometimes, `renderToString` is used on the client to convert some component to HTML.
Expand Down Expand Up @@ -137,5 +150,5 @@ The [`flushSync`](/reference/react-dom/flushSync) call is necessary so that the
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.
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.
To solve this, use one of the [recommended streaming solutions.](#alternatives) 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.
28 changes: 28 additions & 0 deletions src/content/reference/react-dom/static/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Static React DOM APIs
---

<Intro>

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.

</Intro>

---

## Static APIs for Web Streams {/*static-apis-for-web-streams*/}

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:

* [`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)


---

## Static APIs for Node.js Streams {/*static-apis-for-nodejs-streams*/}

These methods are only available in the environments with [Node.js Streams:](https://nodejs.org/api/stream.html)

* [`prerenderToNodeStream`](/reference/react-dom/server/renderToPipeableStream) renders a React tree to static HTML with a [Node.js Stream.](https://nodejs.org/api/stream.html)


Loading

0 comments on commit 76a46c1

Please sign in to comment.