React Server Components for Turbopack
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
-
Module not found: Can't resolve 'react-server-dom-turbopack' in '...'
cause Attempting to import the package directly without specifying the client or server entry point, or within an environment where Turbopack is not correctly configured.fixEnsure you are using a meta-framework that handles this integration. If you are a framework developer, confirm your build system correctly resolves `react-server-dom-turbopack/client` or `react-server-dom-turbopack/server` based on the target environment. -
TypeError: Cannot read properties of undefined (reading 'createFromReadableStream') at Object.<anonymous> (react-server-dom-turbopack/client)
cause This error typically indicates that the `react-server-dom-turbopack/client` module failed to initialize correctly, often due to an incorrect build environment or missing peer dependencies (`react`, `react-dom`).fixVerify that `react` and `react-dom` peer dependencies are installed and compatible with version 19.x. Also, ensure your build toolchain (Turbopack) is configured to correctly process and bundle this module for the client environment.
Warnings
- breaking This package is explicitly not intended for direct import or use by application developers. It is a low-level internal API for meta-frameworks (e.g., Next.js) that integrate React Server Components with Turbopack. Direct usage will likely lead to integration problems, unexpected behavior, and lack of support.
- gotcha Security hardening and DoS mitigations for Server Actions and Server Components are frequently updated in minor/patch releases (e.g., v19.2.4, v19.1.5, v19.0.4). While this is handled by your framework, being on the latest stable version of your framework is crucial to benefit from these protections.
- gotcha The package depends heavily on specific server and client entry points (e.g., `/server`, `/client`). Incorrectly importing from the root `react-server-dom-turbopack` or using the wrong entry point for the environment (server vs. client) will result in module resolution errors or runtime failures.
Install
-
npm install react-server-dom-turbopack -
yarn add react-server-dom-turbopack -
pnpm add react-server-dom-turbopack
Imports
- createFromReadableStream
import { createFromReadableStream } from 'react-server-dom-turbopack';import { createFromReadableStream } from 'react-server-dom-turbopack/client'; - decodeReply
const { decodeReply } = require('react-server-dom-turbopack/client');import { decodeReply } from 'react-server-dom-turbopack/client'; - renderToPipeableStream
import { renderToPipeableStream } from 'react-server-dom-turbopack/server';
Quickstart
/* 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.');