Browse Source

[API] Legacy renderTo* SSR methods (#5326)

main
dan 2 years ago
committed by GitHub
parent
commit
51f0d2d76e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      beta/src/content/apis/react-dom/client/createRoot.md
  2. 36
      beta/src/content/apis/react-dom/client/hydrateRoot.md
  3. 74
      beta/src/content/apis/react-dom/server/renderToNodeStream.md
  4. 67
      beta/src/content/apis/react-dom/server/renderToStaticMarkup.md
  5. 68
      beta/src/content/apis/react-dom/server/renderToStaticNodeStream.md
  6. 124
      beta/src/content/apis/react-dom/server/renderToString.md
  7. 12
      beta/src/sidebarAPIs.json

2
beta/src/content/apis/react-dom/client/createRoot.md

@ -254,13 +254,13 @@ An app fully built with React will usually only have one `createRoot` call for i
#### Parameters {/*parameters*/}
* `domNode`: A [DOM element.](https://developer.mozilla.org/en-US/docs/Web/API/Element) React will create a root for this DOM element and allow you to call functions on the root, such as `render` to display rendered React content.
* **optional** `options`: A object contain options for this React root.
* `onRecoverableError`: optional callback called when React automatically recovers from errors.
* `identifierPrefix`: optional prefix React uses for IDs generated by [`useId`.](/apis/react/useId) Useful to avoid conflicts when using multiple roots on the same page.
#### Returns {/*returns*/}
`createRoot` returns an object with two methods: [`render`](#root-render) and [`unmount`.](#root-unmount)

36
beta/src/content/apis/react-dom/client/hydrateRoot.md

@ -96,6 +96,41 @@ React can recover from some hydration errors, but **you must fix them like other
</Pitfall>
---
### Hydrating an entire document {/*hydrating-an-entire-document*/}
Apps fully built with React can render the entire document from the root component, including the [`<html>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html) tag:
```js {3,10}
function App() {
return (
<html>
<head>
<title>My app</title>
</head>
<body>
<Page />
</body>
</html>
);
}
```
To hydrate the entire document, pass the [`document`](https://developer.mozilla.org/en-US/docs/Web/API/Window/document) global as the first argument to `hydrateRoot`:
```js {5}
import {hydrateRoot} from 'react-dom/client';
import App from './App.js';
hydrateRoot(
document,
<App />
);
```
---
### Updating a hydrated root component {/*updating-a-hydrated-root-component*/}
After the root has finished hydrating, you can call [`root.render`](#root-render) to update the root React component. **Unlike with [`createRoot`](/apis/react-dom/client/createRoot), you don't usually need to do this because the initial content was already rendered as HTML.**
@ -177,6 +212,7 @@ React will attach to the HTML that exists inside the `domNode`, and take over ma
`hydrateRoot` returns an object with two methods: [`render`](#root-render) and [`unmount`.](#root-unmount)
#### Caveats {/*caveats*/}
* `hydrateRoot()` expects the rendered content to be identical with the server-rendered content. You should treat mismatches as bugs and fix them.
* In development mode, React warns about mismatches during hydration. There are no guarantees that attribute differences will be patched up in case of mismatches. This is important for performance reasons because in most apps, mismatches are rare, and so validating all markup would be prohibitively expensive.
* You'll likely have only one `hydrateRoot` call in your app. If you use a framework, it might do this call for you.

74
beta/src/content/apis/react-dom/server/renderToNodeStream.md

@ -2,22 +2,82 @@
title: renderToNodeStream
---
<Wip>
<Deprecated>
This section is incomplete, please see the old docs for [renderToNodeStream.](https://reactjs.org/docs/react-dom-server.html#rendertonodestream)
</Wip>
This API will be removed in a future major version of React. Use [`renderToPipeableStream`](/apis/react-dom/server/renderToPipeableStream) instead.
</Deprecated>
<Intro>
Render a React element to its initial HTML.
`renderToNodeStream` renders a React tree to a [Node.js Readable stream.](https://nodejs.org/api/stream.html#readable-streams)
```js
renderToNodeStream(element)
const stream = renderToNodeStream(<App />)
```
</Intro>
<InlineToc />
---
## Usage {/*usage*/}
### Rendering a React tree as HTML to a Node.js Readable stream {/*rendering-a-react-tree-as-html-to-a-nodejs-readable-stream*/}
<Deprecated>
This API will be removed in a future major version of React. Use [`renderToPipeableStream`](/apis/react-dom/server/renderToPipeableStream) instead.
</Deprecated>
Call `renderToNodeStream` to get a [Node.js Readable stream](https://nodejs.org/api/stream.html#readable-streams) which you can pipe to your server response:
```js {3-4}
// The route handler syntax depends on your backend framework
app.use('/', (request, response) => {
const stream = renderToNodeStream(<App />);
stream.pipe(response);
});
```
The stream will produce the initial non-interactive HTML output of your React components. On the client, you will need to call [`hydrateRoot`](/apis/react-dom/client/hydrateRoot) to *hydrate* that server-generated HTML and make it interactive.
---
## Reference {/*reference*/}
### `renderToNodeStream(element)` {/*rendertonodestream*/}
<Deprecated>
This API will be removed in a future major version of React. Use [`renderToPipeableStream`](/apis/react-dom/server/renderToPipeableStream) instead.
</Deprecated>
On the server, call `renderToNodeStream` to get a [Node.js Readable stream](https://nodejs.org/api/stream.html#readable-streams) which you can pipe into the response.
```js
const stream = renderToNodeStream(<App />);
stream.pipe(response);
```
On the client, call [`hydrateRoot`](/apis/react-dom/client/hydrateRoot) to make the server-generated HTML interactive.
#### Parameters {/*parameters*/}
* `element`: A React element you want to render to HTML. For example, a JSX element like `<App />`.
#### Returns {/*returns*/}
A [Node.js Readable stream](https://nodejs.org/api/stream.html#readable-streams) that outputs an HTML string.
#### Caveats {/*caveats*/}
* This method will wait for all [Suspense boundaries](/apis/react/Suspense) to complete before returning any output.
* As of React 18, this method buffers all of its output, so it doesn't actually provide any streaming benefits. This is why it's recommended that you migrate to [`renderToPipeableStream`](/apis/react-dom/server/renderToPipeableStream) instead.
* The returned stream is a byte stream encoded in utf-8. If you need a stream in another encoding, take a look at a project like [iconv-lite](https://www.npmjs.com/package/iconv-lite), which provides transform streams for transcoding text.

67
beta/src/content/apis/react-dom/server/renderToStaticMarkup.md

@ -2,24 +2,71 @@
title: renderToStaticMarkup
---
<Wip>
<Intro>
This section is incomplete, please see the old docs for [renderToStaticMarkup.](https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup)
`renderToStaticMarkup` renders a non-interactive React tree to an HTML string.
</Wip>
```js
const html = renderToStaticMarkup(<Page />)
```
</Intro>
<Intro>
<InlineToc />
Similar to renderToString, except this doesn’t create extra DOM attributes that React uses internally, such as data-reactroot. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save some bytes.
---
## Usage {/*usage*/}
```js
renderToStaticMarkup(element)
### Rendering a non-interactive React tree as HTML to a string {/*rendering-a-non-interactive-react-tree-as-html-to-a-string*/}
Call `renderToStaticMarkup` to render your app to an HTML string which you can send with your server response:
```js {3-4}
// The route handler syntax depends on your backend framework
app.use('/', (request, response) => {
const html = renderToStaticMarkup(<Page />);
response.send(html);
});
```
If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use renderToString on the server and ReactDOM.hydrateRoot() on the client.
This will produce the initial non-interactive HTML output of your React components.
</Intro>
<Pitfall>
This method renders **non-interactive HTML that cannot be hydrated.** This is useful if you want to use React as a simple static page generator, or if you're rendering completely static content like emails.
Interactive apps should use [`renderToString`](/apis/react-dom/server/renderToString) on the server and [`hydrateRoot`](/apis/react-dom/client/hydrateRoot) on the client.
</Pitfall>
---
## Reference {/*reference*/}
### `renderToStaticMarkup(node)` {/*rendertostaticmarkup*/}
On the server, call `renderToStaticMarkup` to render your app to HTML.
```js {3-4}
const html = renderToStaticMarkup(<Page />);
```
It will produce non-interactive HTML output of your React components.
#### Parameters {/*parameters*/}
* `node`: A React node you want to render to HTML. For example, a JSX node like `<Page />`.
#### Returns {/*returns*/}
An HTML string.
#### Caveats {/*caveats*/}
* `renderToStaticMarkup` output cannot be hydrated.
* `renderToStaticMarkup` has limited Suspense support. If a component suspends, `renderToStaticMarkup` immediately sends its fallback as HTML.
* `renderToStaticMarkup` works in the browser, but using it in the client code is not recommended. If you need to render a component to HTML in the browser, [get the HTML by rendering it into a DOM node.](/apis/react-dom/server/renderToString#removing-rendertostring-from-the-client-code)
<InlineToc />

68
beta/src/content/apis/react-dom/server/renderToStaticNodeStream.md

@ -2,22 +2,74 @@
title: renderToStaticNodeStream
---
<Wip>
<Intro>
`renderToStaticNodeStream` renders a non-interactive React tree to a [Node.js Readable stream.](https://nodejs.org/api/stream.html#readable-streams)
This section is incomplete, please see the old docs for [renderToStaticNodeStream.](https://reactjs.org/docs/react-dom-server.html#rendertostaticnodestream)
```js
const stream = renderToStaticNodeStream(<Page />)
```
</Wip>
</Intro>
<InlineToc />
<Intro>
---
## Usage {/*usage*/}
### Rendering a React tree as static HTML to a Node.js Readable stream {/*rendering-a-react-tree-as-static-html-to-a-nodejs-readable-stream*/}
Call `renderToStaticNodeStream` to get a [Node.js Readable stream](https://nodejs.org/api/stream.html#readable-streams) which you can pipe to your server response:
```js {3-4}
// The route handler syntax depends on your backend framework
app.use('/', (request, response) => {
const stream = renderToStaticNodeStream(<Page />);
stream.pipe(response);
});
```
The stream will produce the initial non-interactive HTML output of your React components.
<Pitfall>
This method renders **non-interactive HTML that cannot be hydrated.** This is useful if you want to use React as a simple static page generator, or if you're rendering completely static content like emails.
Similar to renderToNodeStream, except this doesn’t create extra DOM attributes that React uses internally, such as `data-reactroot`. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save some bytes.
Interactive apps should use [`renderToPipeableStream`](/apis/react-dom/server/renderToPipeableStream) on the server and [`hydrateRoot`](/apis/react-dom/client/hydrateRoot) on the client.
</Pitfall>
---
## Reference {/*reference*/}
### `renderToStaticNodeStream(element)` {/*rendertostaticnodestream*/}
On the server, call `renderToStaticNodeStream` to get a [Node.js Readable stream](https://nodejs.org/api/stream.html#readable-streams).
```js
renderToStaticNodeStream(element)
const stream = renderToStaticNodeStream(<Page />);
stream.pipe(response);
```
</Intro>
The stream will produce non-interactive HTML output of your React components.
#### Parameters {/*parameters*/}
* `element`: A React element you want to render to HTML. For example, a JSX element like `<Page />`.
#### Returns {/*returns*/}
A [Node.js Readable stream](https://nodejs.org/api/stream.html#readable-streams) that outputs an HTML string. The resulting HTML can't be hydrated on the client.
#### Caveats {/*caveats*/}
* `renderToStaticNodeStream` output cannot be hydrated.
* This method will wait for all [Suspense boundaries](/apis/react/Suspense) to complete before returning any output.
* As of React 18, this method buffers all of its output, so it doesn't actually provide any streaming benefits.
* The returned stream is a byte stream encoded in utf-8. If you need a stream in another encoding, take a look at a project like [iconv-lite](https://www.npmjs.com/package/iconv-lite), which provides transform streams for transcoding text.
<InlineToc />

124
beta/src/content/apis/react-dom/server/renderToString.md

@ -2,22 +2,132 @@
title: renderToString
---
<Wip>
<Pitfall>
This section is incomplete, please see the old docs for [renderToString.](https://reactjs.org/docs/react-dom-server.html#rendertostring)
</Wip>
`renderToString` does not support streaming or waiting for data. [See the alternatives.](#alternatives)
</Pitfall>
<Intro>
Render a React element to its initial HTML. React will return an HTML string. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.
`renderToString` renders a React tree to an HTML string.
```js
renderToString(element)
const html = renderToString(<App />)
```
</Intro>
<InlineToc />
---
## Usage {/*usage*/}
### Rendering a React tree as HTML to a string {/*rendering-a-react-tree-as-html-to-a-string*/}
Call `renderToString` to render your app to an HTML string which you can send with your server response:
```js {3-4}
// The route handler syntax depends on your backend framework
app.use('/', (request, response) => {
const html = renderToString(<App />);
response.send(html);
});
```
This will produce the initial non-interactive HTML output of your React components. On the client, you will need to call [`hydrateRoot`](/apis/react-dom/client/hydrateRoot) to *hydrate* that server-generated HTML and make it interactive.
<Pitfall>
`renderToString` does not support streaming or waiting for data. [See the alternatives.](#alternatives)
</Pitfall>
---
## Alternatives {/*alternatives*/}
### Migrating from `renderToString` to a streaming method 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.
When possible, we recommend to use these fully-featured alternatives:
* If you use Node.js, use [`renderToPipeableStream`.](/apis/react-dom/server/renderToPipeableStream)
* If you use Deno or a modern edge runtime with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API), use [`renderToReadableStream`.](/apis/react-dom/server/renderToReadableStream)
You can continue using `renderToString` if your server 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.
```js {1-2}
// 🚩 Unnecessary: using renderToString on the client
import { renderToString } from 'react-dom/server';
const html = renderToString(<MyIcon />);
console.log(html); // For example, "<svg>...</svg>"
```
Importing `react-dom/server` **on the client** unnecessarily increases your bundle size and should be avoided. If you need to render some component to HTML in the browser, use [`createRoot`](/apis/react-dom/client/createRoot) and read HTML from the DOM:
```js
import { createRoot } from 'react-dom/client';
import { flushSync } from 'react-dom';
const div = document.createElement('div');
const root = createRoot(div);
flushSync(() => {
root.render(<MyIcon />);
});
console.log(div.innerHTML); // For example, "<svg>...</svg>"
```
The [`flushSync`](/apis/react-dom/flushSync) call is necessary so that the DOM is updated before reading its [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) property.
---
## Reference {/*reference*/}
### `renderToString(node)` {/*rendertostring*/}
On the server, call `renderToString` to render your app to HTML.
```js {3-4}
const html = renderToString(<App />);
```
On the client, call [`hydrateRoot`](/apis/react-dom/client/hydrateRoot) to make the server-generated HTML interactive.
#### Parameters {/*parameters*/}
* `node`: A React node you want to render to HTML. For example, a JSX node like `<App />`.
#### Returns {/*returns*/}
An HTML string.
#### Caveats {/*caveats*/}
* `renderToString` has limited Suspense support. If a component suspends, `renderToString` immediately sends its fallback as HTML.
* `renderToString` works in the browser, but using it in the client code is [not recommended.](#removing-rendertostring-from-the-client-code)
---
## Troubleshooting {/*troubleshooting*/}
### When a component suspends, the HTML always contains a fallback {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}
`renderToString` does not fully support Suspense.
If some component suspends (for example, because it's defined with [`lazy`](/apis/react/lazy) or fetches data), `renderToString` will not wait for its content to resolve. Instead, `renderToString` will find the closest [`<Suspense>`](/apis/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 even before the client code loads.

12
beta/src/sidebarAPIs.json

@ -168,8 +168,7 @@
"routes": [
{
"title": "renderToNodeStream",
"path": "/apis/react-dom/server/renderToNodeStream",
"wip": true
"path": "/apis/react-dom/server/renderToNodeStream"
},
{
"title": "renderToPipeableStream",
@ -183,18 +182,15 @@
},
{
"title": "renderToStaticMarkup",
"path": "/apis/react-dom/server/renderToStaticMarkup",
"wip": true
"path": "/apis/react-dom/server/renderToStaticMarkup"
},
{
"title": "renderToStaticNodeStream",
"path": "/apis/react-dom/server/renderToStaticNodeStream",
"wip": true
"path": "/apis/react-dom/server/renderToStaticNodeStream"
},
{
"title": "renderToString",
"path": "/apis/react-dom/server/renderToString",
"wip": true
"path": "/apis/react-dom/server/renderToString"
}
]
},

Loading…
Cancel
Save