git clone https://github.com/Intense-Visions/harness-engineering
T=$(mktemp -d) && git clone --depth=1 https://github.com/Intense-Visions/harness-engineering "$T" && mkdir -p ~/.claude/skills && cp -r "$T/agents/skills/codex/next-streaming-suspense" ~/.claude/skills/intense-visions-harness-engineering-next-streaming-suspense-25506c && rm -rf "$T"
agents/skills/codex/next-streaming-suspense/SKILL.mdNext.js Streaming and Suspense
Stream server-rendered HTML progressively using Suspense boundaries and loading.tsx
When to Use
- Pages with slow data fetches where showing a skeleton immediately improves perceived performance
- Preventing a single slow component from blocking the entire page render
- Implementing loading UI without client-side state management
- Building shell-first UX (instant layout, streamed content)
- Integrating with React 18 concurrent features and Suspense-aware libraries
Instructions
- Create
alongsideloading.tsx
in any route segment — Next.js wraps the page in apage.tsx
boundary automatically.<Suspense> - Wrap individual slow components in
for finer-grained streaming control.<Suspense fallback={<Skeleton />}> - Start data fetches before
to allow parallel fetching — initiate promises, then await them after wrapping in Suspense.await - Use the
hook in Client Components to unwrap a Promise passed as a prop, suspending until it resolves.use() - Never
a fetch at the top of a layout that wraps a Suspense boundary — this negates streaming by blocking the boundary parent.await - Prioritize streaming for above-the-fold content — wrap below-the-fold slow sections in Suspense instead.
- Keep
as a Server Component — it can import static UI without any JS shipped to the client.loading.tsx
// app/dashboard/page.tsx — parallel fetch with streaming import { Suspense } from 'react'; import { MetricsSkeleton, ActivitySkeleton } from '@/components/skeletons'; import { Metrics } from './metrics'; import { Activity } from './activity'; export default function DashboardPage() { // Initiate fetches in parallel — do NOT await here const metricsPromise = fetchMetrics(); const activityPromise = fetchActivity(); return ( <div> <Suspense fallback={<MetricsSkeleton />}> <Metrics promise={metricsPromise} /> </Suspense> <Suspense fallback={<ActivitySkeleton />}> <Activity promise={activityPromise} /> </Suspense> </div> ); } // app/dashboard/metrics.tsx — async Server Component async function Metrics({ promise }: { promise: Promise<Metric[]> }) { const metrics = await promise; // suspends until resolved return <MetricsList items={metrics} />; }
Details
Next.js implements streaming via HTTP chunked transfer encoding. The server sends the initial HTML shell immediately, then streams additional HTML chunks as Suspense boundaries resolve. The browser progressively renders each chunk without waiting for the full response.
Suspense boundary placement: Each
<Suspense> boundary is an independent streaming unit. Content outside boundaries renders synchronously. Deeply nested boundaries allow granular streaming — the outermost boundary controls the shell, inner boundaries control content slots.
loading.tsx convention: Next.js automatically wraps
page.tsx in a Suspense boundary when loading.tsx exists in the same directory. The loading.tsx file is the fallback. This is equivalent to <Suspense fallback={<Loading />}><Page /></Suspense>.
Parallel vs sequential data fetching: Two
awaits in sequence cause a waterfall — each fetch waits for the previous. Starting both fetches before any await (or using Promise.all) enables parallelism. With Suspense, each async Server Component can independently suspend without blocking siblings.
Error handling with Suspense: Pair each
<Suspense> boundary with an <ErrorBoundary> (or co-locate error.tsx) to handle fetch failures gracefully within that stream segment.
Trade-offs: Streaming increases TTFB perception because the shell arrives fast, but Time to Fully Loaded may be similar. Streaming works best when content has natural priority ordering (fast shell, slower content).
Source
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.