Loading UI and Streaming with Suspense in Next.js
Creating instant loading states in Next.js: loading.tsx conventions, React Suspense boundaries, and progressive SSR streaming.
Loading UI and Streaming with Suspense in Next.js
The special loading.tsx file automatically wraps a route segment inside a React Suspense boundary, streaming fallback loading skeletons to the browser immediately while asynchronous server data loads in the background.
1. Instant Route Loading Skeletons (loading.tsx)
Create a loading.tsx file inside any route folder:
// app/docs/loading.tsx
export default function DocsLoading() {
return (
<div className="space-y-4 p-8 animate-pulse">
<div className="h-8 bg-muted rounded w-1/3" />
<div className="h-4 bg-muted rounded w-full" />
<div className="h-4 bg-muted rounded w-4/5" />
<div className="h-64 bg-muted rounded w-full" />
</div>
);
}2. Granular In-Page Streaming with <Suspense>
Instead of blocking the entire page while waiting for one slow component, stream fast UI immediately and wrap the slow component in <Suspense>:
// app/dashboard/page.tsx
import { Suspense } from "react";
import { FastProfileHeader } from "@/components/Header";
import { SlowAnalyticsGraph } from "@/components/Graph";
export default function DashboardPage() {
return (
<main className="p-8">
{/* 1. Renders and streams to browser immediately: */}
<FastProfileHeader />
{/* 2. Streams in progressively as database resolves: */}
<Suspense fallback={<p>Loading chart data...</p>}>
<SlowAnalyticsGraph />
</Suspense>
</main>
);
}Related Topics
Data Fetching & Revalidation in Next.js
Data fetching in Next.js App Router: async/await in Server Components, extended fetch API, time-based revalidation, and on-demand cache tagging.
Error Handling Architecture in Next.js
Handling runtime errors in Next.js: error.tsx boundaries, global-error.tsx, recovering via reset(), digest hashes, and server error sanitization.