React Server Components (DOM/Webpack Bindings)

19.2.5 · active · verified Sun Apr 19

react-server-dom-webpack is a core utility package providing the necessary bindings for integrating React Server Components with the DOM environment when using Webpack for bundling. Currently stable at version 19.2.5, it receives frequent updates, primarily focusing on security hardening, DoS mitigations for Server Actions, and protection against rendering cycles. Unlike most React libraries, this package is explicitly designed for integration into meta-frameworks (such as Next.js or Remix) rather than for direct consumption by application developers. Its role is to bridge the server and client rendering processes, enabling the hydration of server-generated component trees on the client side and facilitating communication for Server Actions. Key differentiators include its tight coupling with Webpack's module system and its foundational role in the React Server Components paradigm, managing the serialization and deserialization of component payloads and server references.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a conceptual usage of `createFromFetch` within a simplified meta-framework client-side runtime to hydrate React Server Components from a server payload. This package is not intended for direct application developer use.

// This code is a highly simplified conceptual example of how a meta-framework
// might internally use react-server-dom-webpack on the client side.
// Application developers should NOT import this package directly.

import { createFromFetch } from 'react-server-dom-webpack/client';
import { hydrateRoot } from 'react-dom/client';

async function renderServerComponentRoot(rootElementId: string, serverComponentUrl: string) {
  const rootElement = document.getElementById(rootElementId);
  if (!rootElement) {
    console.error(`Root element with ID "${rootElementId}" not found.`);
    return;
  }

  try {
    // Simulate fetching the Server Component payload from a server endpoint.
    // In a real meta-framework, this would be handled by a sophisticated router.
    const response = await fetch(serverComponentUrl, {
      headers: {
        Accept: 'text/x-component', // Standard header for RSC payloads
      },
    });

    if (!response.ok) {
      throw new Error(`Failed to fetch Server Component: ${response.statusText}`);
    }

    // createFromFetch reads the RSC payload from the response body.
    // It returns a React element that represents the Server Component tree.
    const serverComponent = createFromFetch(response, {
      // client-reference map (Webpack bundle map) would be provided here
      // by the meta-framework to resolve client components
      async callServer(id, args) {
        // This function is called when a Server Action is invoked from the client.
        // It's part of the RSC client runtime, facilitating communication back to the server.
        const body = JSON.stringify({ id, args });
        const res = await fetch('/_server_action', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body,
        });
        return res.json();
      },
    });

    // Hydrate the existing HTML generated by the server with the React Server Component tree.
    // This connects client-side interactivity to the server-rendered content.
    hydrateRoot(rootElement, serverComponent);

    console.log('React Server Components hydrated successfully.');
  } catch (error) {
    console.error('Error hydrating React Server Components:', error);
  }
}

// Example usage within a simulated meta-framework client entry point
document.addEventListener('DOMContentLoaded', () => {
  renderServerComponentRoot('root', '/rsc-payload'); // Imagine '/rsc-payload' serves your RSC data
});

// A placeholder for a basic HTML file (index.html) that this might hydrate:
// <div id="root"><!-- Server-rendered content will go here, or be replaced/hydrated --></div>
// Note: This example is illustrative. A real setup is far more complex.

view raw JSON →