> ## Documentation Index
> Fetch the complete documentation index at: https://bun-1dd33a4e-farm-47265618-file-slice-stream-hang.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Server-side render (SSR) a React component

Install `react` and `react-dom`:

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
# Any package manager can be used
bun add react react-dom
```

***

To render a React component to an HTML stream server-side (SSR):

```tsx ssr-react.tsx icon="file-code" theme={"theme":{"light":"github-light","dark":"dracula"}}
import { renderToReadableStream } from "react-dom/server";

function Component(props: { message: string }) {
  return (
    <body>
      <h1>{props.message}</h1>
    </body>
  );
}

const stream = await renderToReadableStream(<Component message="Hello from server!" />);
```

***

Combine this with `Bun.serve()` to get an SSR HTTP server:

```tsx server.tsx icon="https://mintcdn.com/bun-1dd33a4e-farm-47265618-file-slice-stream-hang/ZiIlD0efumrA_ap0/icons/typescript.svg?fit=max&auto=format&n=ZiIlD0efumrA_ap0&q=85&s=d3ea8d53d37fb8d8e83641ee2f63f641" theme={"theme":{"light":"github-light","dark":"dracula"}}
Bun.serve({
  async fetch() {
    const stream = await renderToReadableStream(<Component message="Hello from server!" />);
    return new Response(stream, {
      headers: { "Content-Type": "text/html" },
    });
  },
});
```

***

React `19` and later include an [SSR optimization](https://github.com/facebook/react/pull/25597) that takes advantage of Bun's "direct" `ReadableStream` implementation. If you run into an error like `export named 'renderToReadableStream' not found`, install version `19` of `react` and `react-dom`, or import from `react-dom/server.browser` instead of `react-dom/server`. See [facebook/react#28941](https://github.com/facebook/react/issues/28941) for details.
