React Server Components for Turbopack

19.2.5 · active · verified Sun Apr 19

react-server-dom-turbopack provides the foundational bindings for integrating React Server Components (RSC) with Turbopack, a high-performance build system. It is specifically designed as a low-level API for meta-frameworks (e.g., Next.js) to manage the serialization and deserialization of RSC payloads between server and client, enabling features like streaming and Server Actions. This package is not intended for direct consumption by application developers, but rather as an internal dependency within frameworks. The current stable version is 19.2.5, aligning with React's release cycle, which sees frequent updates, often including security hardening and performance improvements related to RSC and Server Actions. Its key differentiator is its tight integration with Turbopack for optimal performance in environments leveraging that build tool.

Common errors

Warnings

Install

Imports

Quickstart

Illustrates the conceptual usage of `renderToPipeableStream` on the server and `createFromReadableStream` on the client, as a meta-framework would implement it. This is NOT for direct application use.

/* This quickstart illustrates how a meta-framework *might* conceptually use
 * react-server-dom-turbopack. Application developers should NOT use this directly.
 * Instead, use a framework like Next.js that integrates React Server Components.
 */

// Conceptual server-side rendering setup (framework-level)
import { renderToPipeableStream } from 'react-server-dom-turbopack/server';

async function serveReactServerComponent(req, res) {
  const Component = () => {
    // This would be a React Server Component in a real framework setup
    return <h1>Hello from a Server Component!</h1>;
  };

  const { pipe } = renderToPipeableStream(<Component />);
  res.setHeader('Content-Type', 'text/x-component');
  pipe(res);
}

// Conceptual client-side hydration (framework-level)
import { createFromReadableStream } from 'react-server-dom-turbopack/client';

async function hydrateClientComponent() {
  const response = await fetch('/rsc-payload'); // Imagine fetching the RSC payload
  const stream = response.body;

  if (!stream) {
    throw new Error('Response body is not a readable stream.');
  }

  const { default: ServerComponent } = createFromReadableStream(stream);
  // In a real framework, this 'ServerComponent' would be rendered into the DOM
  // using ReactDOM.createRoot().render(<ServerComponent />);
  // For this conceptual example, we'll just log its existence.
  console.log('Server component received and ready for hydration:', ServerComponent);
}

// Example usage simulation (not runnable as a standalone app)
// serveReactServerComponent(mockRequest, mockResponse);
// hydrateClientComponent();
console.log('This code illustrates framework-level integration and is not for direct use.');

view raw JSON →