Sub topic 3
Suspense and Streaming the RSC Payload
Assume you know React components, JSX, hooks, promises, and the difference between client-side and server-side rendering.
Suspense Streams Ready Parts
Suspense is a React boundary that displays fallback content while a child is waiting. With React Server Components, the server can send the ready parts of the RSC payload first, then send the delayed part when its data is available.
import { Suspense } from 'react';
import Reviews from './Reviews';
export default function Product() {
return (
<section>
<h1>Product</h1>
<Suspense fallback={<p>Loading reviews...</p>}>
<Reviews />
</Suspense>
</section>
);
}The browser can show the product heading immediately. Later payload data fills the reviews boundary. Streaming reduces the time before useful content appears, but it does not make the data request itself faster.
RSC Is Not SSR
Server-side rendering, or SSR, produces HTML for the browser to display. RSC produces a serialized description of a React tree. A framework can use both:
- SSR sends initial HTML for fast display.
- The RSC payload tells React which server-rendered tree to use.
- Client Components are loaded as JavaScript and become interactive.
- Server Component code and server-only data stay on the server.
SSR answers, “What HTML should the browser receive?” RSC answers, “What React tree should the client render or update?”
Navigation Applies a New Payload
During a client-side navigation, a framework router can request an RSC payload for the next route instead of downloading a complete HTML document. React merges the new server tree into the existing tree. Client state, such as an open menu or an input value, can remain in place when its component is preserved.
Server Functions Reverse the Direction
A Server Function is an async function marked with "use server". The client submits data to it through a framework-provided reference. The function runs on the server, so every input must be treated as untrusted network input.
'use server';
export async function addPost(formData) {
const title = formData.get('title');
if (typeof title !== 'string' || title.trim() === '') {
throw new Error('Invalid title');
}
// Save the validated title to the database.
}Security: Inspect What the Payload Exposes
The payload is not a private channel. Props sent to a Client Component can be visible in the browser. Do not pass passwords, private tokens, or unnecessary database fields. Keep authorization checks on the server, including inside Server Functions.
Exercise
Why can a Suspense boundary improve perceived performance without speeding up a database query?
Show answer
It lets the server stream ready content before the delayed query finishes, so the user sees useful content sooner.