The Dinou Pattern
Combine Server Functions, Headless Client Updaters, and React 19 primitives (use() and <Suspense>) with your global store (such as Zustand) for surgical, zero-layout-shift reactivity — without route-wide reloads or lost input focus.
The Concept & Architecture
In full-stack React applications, mutations often trigger broad page re-renders or full route re-fetches. This can cause unnecessary re-renders of unrelated components, reset unsaved form inputs, or cause visible layout shifts.
The Dinou Pattern offers a modern alternative by leveraging the unique ability of Server Functions to return Client Components directly over the React Flight RPC protocol. After executing a mutation securely on the server (across Node.js, Bun, Deno, Cloudflare Workers, or Netlify), the server returns an invisible Headless State Updater component that:
- Mounts on the client (rendering
null, emitting 0 DOM nodes and 0 layout shifts). - Executes a client-side
useEffectupon mounting to notify a store. - Selectively invalidates only dependent queries, streaming the updated UI via React 19
<Suspense>anduse()without full-page reloads.
Flight RPC & Headless Updaters
Because Server Functions communicate using the React Flight protocol rather than plain JSON REST endpoints, the server can return serialized references to Client Components with custom props:
The Server Function owns the business logic, validates inputs, and updates the database inside a secure server runtime (Node.js, Bun, Deno, Cloudflare Workers, or Netlify).
Returning the headless component allows the server to orchestrate client state transitions without coupling to client network listeners.
5-Step Execution Lifecycle
The following flow illustrates how the mutation, stream, store update, and surgical re-render operate in unison:
addPatternTask()) and sets a mutation promise into a headless boundary.<PatternTaskUpdater /> Client Component.null), and its useEffect invalidates the key in the global store (e.g. useTaskStore.getState().invalidateTasks()).<Suspense> boundary re-fetches and streams the updated list, leaving form focus and scroll untouched.When to Use vs. <DinouCacheSlot>
Dinou provides two complementary surgical reactivity models without full-route reloads. Choose the one that matches who owns the state:
<DinouCacheSlot> + refreshSlot()Best for server-owned, cached RSC subtrees (shared dashboards, product stock badges, pricing cards) where you want ISR/tag caching (revalidateTag) and zero client state stores. Explore 3D Subtree Caching.
Best for client-first interactive apps & multi-widget coordination where a mutation on the server needs to orchestrate Zustand, Jotai, Redux, or useSyncExternalStore slices across independent client panels while preserving local input focus and scroll.
Step-by-Step Implementation
Here is the complete, modular implementation showing how Server Functions, Flight RPC, and Zustand orchestrate surgical reactivity without route revalidations:
1. The Global Store (Zustand)
A minimal store holding a reactive invalidation key. Any global state manager (such as Zustand, Jotai, or Redux) works identically:
// src/stores/task-store.ts
import { create } from "zustand";
interface TaskStore {
tasksKey: number;
invalidateTasks: () => void;
}
export const useTaskStore = create<TaskStore>((set) => ({
tasksKey: 0,
invalidateTasks: () => set((state) => ({ tasksKey: state.tasksKey + 1 })),
}));Why it matters: Calling invalidateTasks() increments tasksKey, signaling subscribing components to re-fetch their Suspense stream without triggering route-wide re-renders.
2. The Headless Client Updater
An invisible Client Component that renders null (0 DOM elements), but invokes the store's invalidation action when mounted:
// src/components/pattern-updater.tsx
"use client";
import { useEffect } from "react";
import { useTaskStore } from "../stores/task-store";
export default function PatternTaskUpdater({ id }: { id?: string }) {
useEffect(() => {
// Atomically signal the store outside the render cycle
useTaskStore.getState().invalidateTasks();
}, [id]);
return null; // Headless component: emits zero DOM nodes and 0 layout shifts
}3. The Server Function Mutation
Performs the backend mutation on the server (Node.js, Bun, Deno, Cloudflare Workers, or Netlify) and returns the Headless Updater component over Flight RPC:
// src/server-functions/pattern-demo.tsx
"use server";
import type { ReactNode } from "react";
import PatternTaskUpdater from "../components/pattern-updater";
import { tasksDb } from "./db";
export async function addPatternTask(text: string): Promise<ReactNode> {
const trimmed = text.trim();
const id = Math.random().toString(36).substring(2, 9);
if (trimmed) {
tasksDb.unshift({
id,
text: trimmed,
createdAt: new Date().toLocaleTimeString(),
});
}
// The server orchestrates the client by returning the Headless Updater!
return <PatternTaskUpdater key={id} id={id} />;
}4. Server Data Query Function
Fetches and renders the updated list directly on the server:
// src/server-functions/pattern-demo.tsx (continued)
"use server";
import type { ReactNode } from "react";
import PatternTasksList from "../components/pattern-tasks-list";
import { tasksDb } from "./db";
export async function fetchPatternTasks(): Promise<ReactNode> {
return <PatternTasksList tasks={[...tasksDb]} />;
}Server or Client JSX: Because fetchPatternTasks streams over React Flight, it can return either a Client Component (such as <PatternTasksList /> when rows require local event handlers) or a pure Server Component (shipping 0 KB of list-rendering JavaScript to the browser).
5. Display Component (Client Component)
Renders the list of tasks received from the server query:
// src/components/pattern-tasks-list.tsx
"use client";
export interface PatternTaskItem {
id: string;
text: string;
createdAt: string;
}
export default function PatternTasksList({ tasks }: { tasks: PatternTaskItem[] }) {
if (!tasks || tasks.length === 0) {
return <p className="text-muted-foreground text-sm">No tasks yet.</p>;
}
return (
<ul className="space-y-2">
{tasks.map((task) => (
<li key={task.id} className="p-2.5 rounded-lg border bg-card flex justify-between">
<span>{task.text}</span>
<span className="text-xs text-muted-foreground">{task.createdAt}</span>
</li>
))}
</ul>
);
}6. Putting It All Together in the Page
The view coordinates mutation promises and queries with React 19's native use() hook and <Suspense>. Switch tabs below to explore each component in the unified architecture:
"use client";
import { useState, useEffect, Suspense, use, type ReactNode } from "react";
import { addPatternTask, fetchPatternTasks } from "./server-functions/pattern-demo";
import { useTaskStore } from "./stores/task-store";
function TasksStream({ promise }: { promise: Promise<ReactNode> }) {
// Native React 19: unroll the Flight stream promise
const content = use(promise);
return <>{content}</>;
}
function MutationStream({ promise }: { promise: Promise<ReactNode> }) {
const content = use(promise);
return <>{content}</>;
}
export default function Page() {
const tasksKey = useTaskStore((state) => state.tasksKey);
const [text, setText] = useState("");
const [isMutating, setIsMutating] = useState(false);
const [mutationError, setMutationError] = useState<string | null>(null);
const [mutationKey, setMutationKey] = useState(0);
const [mutationPromise, setMutationPromise] = useState<Promise<ReactNode> | null>(null);
const [tasksPromise, setTasksPromise] = useState<Promise<ReactNode>>(() => fetchPatternTasks());
// Invalidate and re-fetch only when the store increments tasksKey
useEffect(() => {
if (tasksKey > 0) {
setTasksPromise(fetchPatternTasks());
}
}, [tasksKey]);
const handleAddTask = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const trimmed = text.trim();
if (!trimmed || isMutating) return;
setIsMutating(true);
setMutationError(null);
setMutationKey((k) => k + 1);
const p = addPatternTask(trimmed);
// Pass a handled promise to use() so a rejected mutation resolves to null
// instead of crashing the React tree or mounting the Headless Updater
setMutationPromise(p.catch(() => null));
p.then(() => {
setText(""); // Only clear user input when the mutation succeeds
})
.catch((err: Error) => {
setMutationError(err.message || "Failed to add task");
})
.finally(() => {
setIsMutating(false);
});
};
return (
<div className="space-y-6">
{/* 1. Mutation Form */}
<form onSubmit={handleAddTask} className="flex gap-2">
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="New task..."
disabled={isMutating}
className="input"
/>
<button type="submit" disabled={isMutating || !text.trim()} className="btn-primary">
{isMutating ? "Adding..." : "Add Task"}
</button>
</form>
{mutationError && (
<p role="alert" className="text-sm text-destructive">
{mutationError}
</p>
)}
{/* 2. Headless Mutation Mount (renders null, updates store) */}
{mutationPromise && (
<Suspense key={`mutation-${mutationKey}`} fallback={null}>
<MutationStream promise={mutationPromise} />
</Suspense>
)}
{/* 3. Surgical Reactive Tasks List */}
<Suspense
key={`pattern-tasks-${tasksKey}`}
fallback={<div>Streaming updated list from server...</div>}
>
<TasksStream promise={tasksPromise} />
</Suspense>
</div>
);
}import { create } from "zustand";
interface TaskStore {
tasksKey: number;
invalidateTasks: () => void;
}
export const useTaskStore = create<TaskStore>((set) => ({
tasksKey: 0,
invalidateTasks: () => set((state) => ({ tasksKey: state.tasksKey + 1 })),
}));"use client";
import { useEffect } from "react";
import { useTaskStore } from "../stores/task-store";
export default function PatternTaskUpdater({ id }: { id?: string }) {
useEffect(() => {
// Atomically signal the store outside the render cycle
useTaskStore.getState().invalidateTasks();
}, [id]);
return null; // Headless component: emits zero DOM nodes and 0 layout shifts
}"use server";
import type { ReactNode } from "react";
import PatternTaskUpdater from "../components/pattern-updater";
import PatternTasksList from "../components/pattern-tasks-list";
import { tasksDb } from "./db";
export async function addPatternTask(text: string): Promise<ReactNode> {
const trimmed = text.trim();
const id = Math.random().toString(36).substring(2, 9);
if (trimmed) {
tasksDb.unshift({
id,
text: trimmed,
createdAt: new Date().toLocaleTimeString(),
});
}
// The server orchestrates the client by returning the Headless Updater!
return <PatternTaskUpdater key={id} id={id} />;
}
export async function fetchPatternTasks(): Promise<ReactNode> {
return <PatternTasksList tasks={[...tasksDb]} />;
}"use client";
export interface PatternTaskItem {
id: string;
text: string;
createdAt: string;
}
export default function PatternTasksList({ tasks }: { tasks: PatternTaskItem[] }) {
if (!tasks || tasks.length === 0) {
return <p className="text-muted-foreground text-sm">No tasks yet.</p>;
}
return (
<ul className="space-y-2">
{tasks.map((task) => (
<li key={task.id} className="p-2.5 rounded-lg border bg-card flex justify-between">
<span>{task.text}</span>
<span className="text-xs text-muted-foreground">{task.createdAt}</span>
</li>
))}
</ul>
);
}How It Works
- User submits the form, initiating
addPatternTask()without full-page reloads. - The mutation promise is passed to a headless
<Suspense>boundary. - Server mutates the database and returns
<PatternTaskUpdater />over Flight RPC. PatternTaskUpdatermounts, executing itsuseEffectto calluseTaskStore.getState().invalidateTasks().- The store notifies the page, calling
fetchPatternTasks()to obtain the fresh stream. - Only the tasks list
<Suspense>boundary updates smoothly, keeping inputs, state, and scroll position completely intact.
If the Server Function throws on the server, the client promise rejects with that error message and the Headless Updater never mounts. Passing p.catch(() => null) to setMutationPromise ensures use(promise) inside <MutationStream> resolves cleanly to null without requiring an outer <ErrorBoundary>, while your handler catches the original promise p and preserves the user's form input:
try {
const p = addPatternTask(trimmed);
setMutationPromise(p.catch(() => null)); // Prevents use(promise) render crash on failure
await p; // If the server throws, execution jumps directly to catch
setText(""); // Only clear input after success
} catch (error) {
// Headless updater never mounted; store was NOT modified!
const message = error instanceof Error ? error.message : "Failed to add task";
setMutationError(message);
}Security Tip: Throwing new Error("...") inside a Server Function responds with HTTP 500 and forwards error.message to the client. Only throw controlled, user-safe validation messages — catch unexpected database or ORM exceptions on the server so internal connection strings or query details are never exposed.
When triggering multiple rapid mutations, incrementing setMutationKey((k) => k + 1) on the <Suspense key=...> boundary forces React to cleanly unmount the previous updater and mount the new one fresh.
Keep Headless Updaters purely functional with zero DOM output (return null). They exist solely as the reactive bridge between the Server Function RPC response and the client-side store.
Initializing useState(() => fetchPatternTasks()) inside a pure "use client" page renders the <Suspense fallback> shell during SSR and fetches the initial Flight stream on browser mount. If you want the initial list streamed directly inside the first SSR HTML response with zero mount roundtrip, render the interactive client shell from a parent Server Component and pass initialTasksPromise as a prop (see Data Fetching Strategies), keeping tasksKey > 0 for surgical post-mutation refreshes.
Zero-Dependency Option: Native useSyncExternalStore
If you prefer not to include any third-party state libraries in your client bundle, you can achieve the exact same surgical reactivity using React 19's native useSyncExternalStore hook:
// src/stores/native-store.ts
import { useSyncExternalStore } from "react";
let tasksKey = 0;
const listeners = new Set<() => void>();
export const nativeTaskStore = {
getSnapshot: () => tasksKey,
invalidateTasks: () => {
tasksKey += 1;
listeners.forEach((listener) => listener());
},
subscribe: (listener: () => void) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
};
export function useNativeTasksKey(): number {
return useSyncExternalStore(
nativeTaskStore.subscribe,
nativeTaskStore.getSnapshot,
nativeTaskStore.getSnapshot // Server snapshot for SSR hydration safety
);
}In your Headless Updater, simply invoke nativeTaskStore.invalidateTasks() instead of useTaskStore.getState().invalidateTasks(). The lifecycle, Flight RPC streaming, and selective re-fetching behavior remain 100% identical.
SSR Concurrency Safety: Although tasksKey is declared at module scope, it is never mutated during rendering or on the server — invalidateTasks() only executes inside PatternTaskUpdater's browser useEffect, so every SSR request deterministically snapshots 0 with zero cross-request leakage or hydration mismatch.
Comparison & Key Advantages
Comparing the Dinou Pattern against cached 3D Server Slots (<DinouCacheSlot>) and traditional client-side data fetching (REST / React Query):
| Feature | The Dinou Pattern | <DinouCacheSlot> + refreshSlot() | REST / React Query (SPA) |
|---|---|---|---|
| Reactivity Scope | Surgical (store-subscribed boundaries) | Surgical (targeted cached RSC slot) | Surgical (query key invalidation) |
| Form / Input Focus | 100% Preserved (zero blur or reset) | Preserved (in-place React reconciliation) | Preserved |
| Bundle Overhead | ~1 KB (Zustand) or 0 KB (Native) | 0 KB extra client store | ~12–15 KB (client query library) |
| Backend Architecture | Universal Server Functions ("use server") | Async RSC + ISR / Tag Cache | Requires dedicated HTTP API endpoints (/api/...) |
| Data Transport | React Flight RPC (typed component streaming) | React Flight Slot Stream | JSON serialization + manual client fetch logic |
| Boilerplate | Zero API routes, controllers, or fetch hooks | Zero stores, zero API routes | Manual endpoints, fetch hooks, error states, and types |
| DOM Impact | 0 DOM nodes emitted by headless updater | In-place slot subtree swap | N/A (data only) |
useSyncExternalStore alongside React 19's Flight RPC and <Suspense>.See how The Dinou Pattern fits alongside Server Components, getProps, 3D Cache Slots, and PPR in the comprehensive Data Fetching guide.