React Server Components for Bun (DOM)
This package provides experimental bindings for React Server Components (RSC) specifically targeting the DOM environment and leveraging the Bun runtime. It is *not* designed for direct consumption by application developers, but rather as an internal utility for meta-frameworks to integrate RSC functionality. Being `0.0.0-experimental`, its API is highly unstable and subject to frequent changes, as evidenced by recent releases focusing on Denial-of-Service (DoS) mitigations, cycle protections, and general hardening. It is part of the broader React ecosystem, maintained by Facebook, and its release cadence is tied to React's experimental and canary channels. This means frequent, often internal-facing updates rather than strict semantic versioning, reflecting its role in cutting-edge React development. Key differentiators include its tight integration with Bun for performance and its essential role in enabling full-stack React Server Components rendering paradigms, bridging the server and client in a web application.
Common errors
-
Error: This package is experimental and not intended for direct use.
cause Attempting to import or use `react-server-dom-bun` directly in an application, rather than through a meta-framework.fixIntegrate React Server Components via a supported meta-framework (e.g., Next.js, Remix) that handles this package internally. -
TypeError: (0, _react_server_dom_bun_server__WEBPACK_IMPORTED_MODULE_0__.renderToReadableStream) is not a function
cause Incorrect import path for ESM or CJS, or attempting to use a function that is not exported or available in the specific runtime environment or subpath.fixVerify the correct import path (`/server` or `/client` subpath) and ensure your bundler/runtime correctly handles ESM imports from `react-server-dom-bun`. -
Invariant Violation: React Server Components require a Bun runtime.
cause Attempting to run code using `react-server-dom-bun` in a non-Bun environment (e.g., Node.js, Deno).fixEnsure your server-side rendering environment is configured to use the Bun runtime for projects leveraging this package.
Warnings
- breaking This package is explicitly marked `0.0.0-experimental`. Its API is highly unstable, subject to frequent breaking changes, and not guaranteed to maintain backwards compatibility.
- gotcha The package description explicitly states: 'It is not intended to be imported directly.' Application developers should not use this package, but rather consume its functionality indirectly through a meta-framework that integrates React Server Components.
- breaking Recent releases include critical security and stability patches, such as 'DoS mitigations to Server Actions' and 'cycle protections'. This indicates that previous experimental versions may have had vulnerabilities or instability, which are actively being addressed.
- gotcha This package is specifically designed for the Bun runtime. Attempting to use it in Node.js or other JavaScript runtimes is unlikely to work correctly and may lead to runtime errors or unexpected behavior.
Install
-
npm install react-server-dom-bun -
yarn add react-server-dom-bun -
pnpm add react-server-dom-bun
Imports
- renderToReadableStream
const { renderToReadableStream } = require('react-server-dom-bun/server');import { renderToReadableStream } from 'react-server-dom-bun/server'; - decodeReply
import { decodeReply } from 'react-server-dom-bun';import { decodeReply } from 'react-server-dom-bun/client'; - createFromReadableStream
import { createFromReadableStream } from 'react-server-dom-bun/server';import { createFromReadableStream } from 'react-server-dom-bun/client';
Quickstart
import { renderToReadableStream } from 'react-server-dom-bun/server';
// IMPORTANT: This package is NOT for direct application use.
// This quickstart demonstrates how a meta-framework *might* internally
// use react-server-dom-bun to render a React Server Component.
// Assume a simple Server Component for demonstration
function App() {
return (
<html>
<head>
<title>Bun RSC Demo</title>
</head>
<body>
<h1>Hello from React Server Component on Bun!</h1>
<p>Current time: {new Date().toLocaleTimeString()}</p>
</body>
</html>
);
}
// A simplified Bun server implementation for illustration purposes.
// In a real meta-framework, this would be part of a much larger setup.
const server = Bun.serve({
port: 3000,
async fetch(req: Request) {
const url = new URL(req.url);
if (url.pathname === '/') {
const stream = await renderToReadableStream(<App />);
return new Response(stream, {
headers: { 'Content-Type': 'text/html' }
});
}
// Serve static assets (e.g., a client bundle if one existed)
if (url.pathname.startsWith('/src')) {
try {
const filePath = `.${url.pathname}`;
return new Response(Bun.file(filePath));
} catch (error) {
console.error(`Error serving static file: ${url.pathname}`, error);
return new Response('Not Found', { status: 404 });
}
}
return new Response('Not Found', { status: 404 });
},
});
console.log(`Bun Server Components server running on http://localhost:${server.port}`);