React Server Components (RSC) Architecture
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- compiler/packages/react-mcp-server/src/index.ts
- packages/react-dom/src/tests/ReactServerRendering-test.js
- packages/react-server-dom-webpack/src/tests/ReactFlightDOM-test.js
- packages/react-client/src/tests/ReactFlight-test.js
- packages/react-server-dom-webpack/src/tests/ReactFlightDOMForm-test.js
- fixtures/flight/server/region.js
- packages/react-dom/src/tests/ReactDOMFizzServerBrowser-test.js
- packages/react-markup/package.json
- packages/react-reconciler/README.md
React Server Components (RSC), also known as "Flight", is an architecture that allows developers to build applications that span the server and client, combining the rich interactivity of client-side apps with the improved performance of traditional server rendering. RSC introduces a new paradigm where components can be designated to run exclusively on the server, reducing the amount of JavaScript sent to the client, improving initial page load times, and allowing components to access server-side data sources directly.
This architecture is distinct from traditional Server-Side Rendering (SSR). While SSR renders components to HTML on the server, RSC renders to a special serialized format that describes the UI. This format is streamed to the client, where it's used to reconstruct the component tree and hydrate interactive elements without re-fetching data. This enables features like progressive rendering and server-driven updates to the client-side UI.
Sources: README.md, packages/react-server-dom-webpack/src/tests/ReactFlightDOM-test.js:129-145, packages/react-dom/src/tests/ReactServerRendering-test.js
Core Concepts
The RSC architecture divides components into two main types: Server Components and Client Components. This separation is the foundation of the model.
Server vs. Client Components
The distinction between component types is determined by the presence of a "use client" directive at the top of a file. Files without this directive contain Server Components by default.
| Feature | Server Components | Client Components |
|---|---|---|
| Environment | Run exclusively on the server. | Run on the client (can be pre-rendered on the server via SSR). |
| State & Interactivity | Cannot use state (e.g., useState) or lifecycle effects (e.g., useEffect). | Can use state, effects, and browser-only APIs. |
| Data Fetching | Can directly access server-side resources (databases, file systems, internal services). | Fetch data via API endpoints. |
| Code Sent to Client | Code is never sent to the client. Only the rendered output is serialized and sent. | Code is bundled and sent to the client to enable interactivity. |
| Directive | Default. No directive needed. | Requires "use client" at the top of the file. |
| Imports | Can import Server or Client Components. | Can only import other Client Components. |
This division allows developers to keep data-heavy logic and non-interactive UI on the server, minimizing the client-side bundle size and improving performance.
Sources: compiler/packages/react-mcp-server/src/index.ts:316-319, packages/react-server-dom-webpack/src/tests/ReactFlightDOM-test.js:118-128
Data Flow and Rendering Lifecycle
The RSC lifecycle involves rendering components on the server, streaming the serialized output to the client, and then reconstructing the UI in the browser.
This diagram illustrates the high-level data flow for an initial page load with React Server Components.
Server-Side Rendering and Serialization
On the server, react-server-dom-webpack/server's renderToPipeableStream (or a similar package-specific function) is used to render the root Server Component.
- Rendering: React walks the component tree on the server.
- Serialization: As Server Components are rendered, their output (a description of the UI, not HTML) is serialized into a stream.
- Client Component Placeholders: When a Client Component is encountered (e.g.,
<UserClient greeting="Hello" />), it is not rendered. Instead, a placeholder is added to the stream containing a reference to the component's module ($$id) and its props. - Streaming: This serialized stream is sent to the client. If the tree contains
Suspenseboundaries, content is streamed progressively as async operations (like data fetching) complete.
Sources: packages/react-server-dom-webpack/src/tests/ReactFlightDOM-test.js:148-156, packages/react-client/src/tests/ReactFlight-test.js:103-125
Client-Side Deserialization and Hydration
The client uses react-server-dom-webpack/client's createFromReadableStream to process the incoming stream.
- Reading the Stream: The client reads the serialized UI description.
- Reconstructing the Tree: It parses the stream and reconstructs the React element tree.
- Loading Client Components: When it encounters a placeholder for a Client Component, it uses the module reference (
$$id) to dynamically import the component's code. - Hydration: The reconstructed tree is then rendered by React on the client. Client Components become interactive, and their state is initialized.
This process is illustrated in the sequence diagram below.
Server Actions
Server Actions allow Client Components to trigger functions that execute on the server. This is essential for handling user interactions like form submissions or mutations that require server-side logic.
A function is marked as a Server Action by using the "use server" directive at the top of its body or the file it's in.
Form Submission with a Server Action
The following diagram shows the flow of a form submission that triggers a server action.
The decodeAction and decodeReply functions from react-server-dom-webpack/server are used to handle the request payload and execute the corresponding server function. The result of the action can be a new UI state, which is then streamed back to the client to update the page without a full reload. The useActionState hook is often used on the client to manage pending states and display results from the action.
Sources: packages/react-server-dom-webpack/src/tests/ReactFlightDOMForm-test.js:100-120, fixtures/flight/server/region.js:188-223
Bundling and Module References
A crucial part of the RSC architecture is how the bundler (e.g., Webpack, Turbopack) differentiates between Server and Client components and manages dependencies.
Client Manifest
The bundler generates a client manifest file (e.g., react-client-manifest.json). This manifest is a map of all Client Components, associating each component with a unique ID ($$id) and information about its bundled file(s).
When the server renders a Client Component, it doesn't include the component's code in the RSC payload. Instead, it serializes a reference containing the $$id and the props. The client then uses this ID to look up the component's file in the manifest and dynamically load it.
The server loads this manifest to correctly create references to client modules during the render process.
Sources: fixtures/flight/server/region.js:80-96, packages/react-server-dom-webpack/src/tests/ReactFlightDOM-test.js:70-74
Key Packages and Their Roles
| Package/File | Role |
|---|---|
react (react-server condition) | Provides the core React APIs for the server environment, including useId and lazy. |
react-dom/server | Provides traditional SSR (renderToString) and streaming SSR (renderToPipeableStream) for the HTML shell. |
react-server-dom-webpack/server | The core package for rendering RSCs on the server. Contains renderToPipeableStream which creates the serialized UI stream. |
react-server-dom-webpack/client | The core package for consuming the RSC stream on the client. Contains createFromReadableStream. |
react-client | Contains the underlying logic for parsing the Flight stream, used by packages like react-server-dom-webpack/client. |
react-reconciler | The underlying engine that powers React's rendering logic, adaptable to different host environments (DOM, Native, Server). |
Sources: packages/react-dom/package.json, packages/react-reconciler/README.md, packages/react-server-dom-webpack/src/tests/ReactFlightDOM-test.js, packages/react-client/src/tests/ReactFlight-test.js
Conclusion
The React Server Components architecture fundamentally changes how React applications can be built by introducing a dual-environment component model. By moving component rendering and data fetching to the server, it aims to deliver better performance, smaller client-side bundles, and a more streamlined developer experience for building data-driven applications. The architecture relies on a sophisticated system of serialization, streaming, and bundler integration to seamlessly blend server-executed code with an interactive client-side experience.