React Server Components for Bun (DOM)

0.0.0-experimental-603e6108-20241029 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Illustrates how a meta-framework might internally use `renderToReadableStream` to serve a React Server Component with Bun. Not intended for direct app use.

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}`);

view raw JSON →