Skip to main content
Install react and react-dom:
terminal
# Any package manager can be used
bun add react react-dom

To render a React component to an HTML stream server-side (SSR):
ssr-react.tsx
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:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bserver.tsx
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 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 for details.