Overview
Level 3: Advanced · Module 8: Generative UI · 25 min read
Why this matters
In traditional Next.js applications, server-rendered components (RSC) are delivered on initial page load, while subsequent client updates are driven by client-side JSON fetching. The Vercel AI SDK and React Server Actions introduced a revolutionary capability: streaming live React Server Components directly across the wire during an ongoing server action invocation. A server action can execute an LLM call, decide to render an interactive server component (like a live database-backed balance sheet), and stream the serialized React Flight payload chunk-by-chunk to the client. This keeps massive libraries (charts, syntax highlighters, PDF generators) on the server, resulting in zero added client bundle size.
Learning objectives
- Understand the React Flight protocol and RSC payload streaming mechanics.
- Implement server-side component streaming using Vercel AI SDK Core (
createStreamableUI). - Interleave plain streaming text with rich React Server Components in a single response.
- Keep heavy dependencies server-side to maintain ultra-lean client bundle sizes.
- Answer technical interview questions comparing RSC streaming vs client-rendered JSON schemas.
Mechanics of RSC Payload Streaming
sequenceDiagram
autonumber
actor User as User in Browser
participant Client as React Client (useUIState)
participant Server as Next.js Server Action ("use server")
participant LLM as Model API
User->>Client: "What are my top 3 investments?"
Client->>Server: invokeAction(prompt)
Server->>LLM: Stream completion with tools
Server-->>Client: Stream chunk 1: "Analyzing your portfolio..." (Text)
Note over Server: Tool completes with stock data.<br>Server instantiates <PortfolioChart stocks={...} />
Server-->>Client: Stream chunk 2: React Flight Payload (Serialized VDOM Tree)
Note over Client: Client reconciles Flight stream!<br>Mounts live React component into UI tree without full reload!Implementation: Streaming UI with createStreamableUI
The @ai-sdk/rsc package enables creating dynamic, streamable UI nodes that transition through loading skeletons before settling into final interactive components:
// app/actions/chat.tsx
"use server";
import { createStreamableUI, createStreamableValue } from "@ai-sdk/rsc";
import OpenAI from "openai";
import { StockCardSkeleton, StockCard } from "@/components/stock-cards";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
export async function submitUserMessage(userPrompt: string) {
// 1. Create a streamable UI container for client mounting
const uiStream = createStreamableUI(
<div className="p-4 text-xs text-gray-500 animate-pulse">
Searching financial markets...
</div>
);
const textStream = createStreamableValue("");
// 2. Run asynchronous model inference
(async () => {
try {
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: userPrompt }],
tools: [
{
type: "function",
function: {
name: "get_stock_quote",
description: "Retrieves live financial stock quote.",
parameters: {
type: "object",
properties: { symbol: { type: "string" } },
required: ["symbol"],
},
},
},
],
});
const message = response.choices[0].message;
if (message.tool_calls && message.tool_calls.length > 0) {
const tool = message.tool_calls[0];
const { symbol } = JSON.parse(tool.function.arguments);
// Update UI to intermediate loading skeleton
uiStream.update(<StockCardSkeleton symbol={symbol} />);
// Fetch live market data server-side
const quote = await fetchMarketData(symbol);
// Replace skeleton with final Server Component!
uiStream.done(<StockCard quote={quote} />);
} else {
// Plain text response
uiStream.done(
<div className="p-4 text-sm bg-gray-100 rounded-xl">
{message.content}
</div>
);
}
} catch (err: any) {
uiStream.error(<div className="text-red-600 text-xs">Error: {err.message}</div>);
}
})();
// Return streamable UI node directly to the caller!
return {
id: crypto.randomUUID(),
display: uiStream.value,
};
}Rendering Streamable UI on the Client
On the client, managing streamable UI nodes requires zero complex parsing:
// components/StreamingChat.tsx
"use client";
import { useState } from "react";
import { submitUserMessage } from "@/app/actions/chat";
export function StreamingChat() {
const [messages, setMessages] = useState<Array<{ id: string; display: React.ReactNode }>>([]);
const [input, setInput] = useState("");
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!input.trim()) return;
const userText = input;
setInput("");
// 1. Optimistic user bubble
setMessages((prev) => [
...prev,
{
id: crypto.randomUUID(),
display: <div className="p-3 bg-blue-600 text-white rounded-lg ml-auto max-w-[80%]">{userText}</div>,
},
]);
// 2. Invoke Server Action and receive streaming UI handle
const response = await submitUserMessage(userText);
// 3. Mount streamable UI handle directly in React tree
setMessages((prev) => [...prev, response]);
};
return (
<div className="flex flex-col h-[500px]">
<div className="flex-1 overflow-y-auto space-y-4 p-4">
{messages.map((m) => (
<div key={m.id}>{m.display}</div>
))}
</div>
<form onSubmit={handleSubmit} className="p-3 border-t flex gap-2">
<input value={input} onChange={(e) => setInput(e.target.value)} className="flex-1 border p-2 rounded" />
<button type="submit" className="bg-blue-600 text-white px-4 py-2 rounded">Send</button>
</form>
</div>
);
}Zero-Bundle-Size Advantage: Heavy Server Components
Consider a rich interactive data visualization component using echarts or three.js (a 450KB JavaScript bundle).
- In Client-Driven Generative UI: The 450KB bundle must be downloaded by every user's browser in case the model decides to invoke a chart.
- In Server Component Streaming (RSC): The chart library executes entirely on the server. The server renders the SVG output and streams serialized Flight bytes (~4KB). Client bundle size reduction: 99%!
Interview Angle: How to Answer in System Design
- Question: "How does React Server Component (RSC) streaming enable Generative UI, and what are its trade-offs compared to client-side component registries?"
- Target Answer:
"RSC streaming leverages the React Flight protocol to stream serialized Virtual DOM nodes across an active Server Action.
Its primary superpower is Zero-Bundle-Size: heavy charting, formatting, or PDF libraries remain exclusively on the server, streaming lightweight rendered representations to the browser without bloating the client JavaScript bundle. It also allows direct database queries inside server component leaves.
The trade-off is ecosystem coupling: RSC streaming is tightly bound to Next.js App Router and cannot be ported to vanilla React, Vite, or mobile clients (React Native). For cross-platform or decoupled architectures, a schema-driven client component registry is preferred."
Quick Check
- What protocol enables Next.js Server Actions to stream serialized React component trees to the browser?
- What is the primary bundle size advantage of server-rendered Generative UI over client registries?
- What hook from Vercel AI SDK Core handles streamable UI states?