When working with React Server Components, it's important to consider three elements:
- Your browser (the client)
- The server side, Next.js (the framework)
- React (the library)
- Initial Request: The browser requests a page.
- Route Matching: Next.js app router matches the requested URL to a server component.
- Server Component Rendering: Next.js instructs React to render the server component.
- RSC Payload Generation: React renders the server component and its child server components, converting them into a special JSON format known as the RSC payload.
- Suspension Handling: If any server component suspends, React pauses rendering of that subtree and sends a placeholder value.
- Client Component Preparation: Client components are prepared with instructions for later in the lifecycle.
- HTML Generation: Next.js uses the RSC payload to generate HTML on the server.
- Streaming: The HTML is streamed to the browser for an immediate, non-interactive preview. Next.js also streams the RSC payload.
- Progressive Rendering: In the browser, Next.js processes the streamed React response.
- UI Rendering: React uses the RSC payload and client component instructions to progressively render the UI.
- Final UI State: Once all components are loaded, the final UI state is presented.
- Hydration: Client components undergo hydration, transforming the app from a static display into an interactive experience.
- Refetch Request: The browser requests a refetch of a specific UI (e.g., a full route).
- Component Matching: Next.js processes the request and matches it to the requested server component.
- Rendering: React renders the component tree (similar to initial loading, but without HTML generation).
- Streaming: Next.js progressively streams the response data back to the client.
- Client-side Rendering: Next.js triggers a render of the route using the new output.
- Reconciliation: React reconciles the new rendered output with existing components on screen.
- DOM Updates: React updates the DOM while preserving crucial UI states (e.g., focus, input values).
Next.js employs three server rendering strategies:
- Static Rendering
- Dynamic Rendering
- Streaming
Each strategy offers different benefits for performance and data freshness.