Menu

Streaming with Suspense

Lesson 4: Streaming with Suspense

What is streaming?

Streaming sends a page in pieces. The fast parts appear immediately, and slow parts arrive when ready, replacing a fallback such as a skeleton.

Without streaming With streaming
Page waits for the slowest query Fast content shows at once
Blank screen during the wait Skeletons show where data is loading
One slow API delays everything Each section loads independently

Example

The course page shows details quickly, but reviews come from a slow service:

// app/courses/[slug]/page.tsx
import { Suspense } from 'react';
import { Reviews } from './Reviews';

export default async function CoursePage({ params }: PageProps<'/courses/[slug]'>) {
  const { slug } = await params;
  const course = await getCourse(slug); // Fast

  return (
    <article>
      <h1>{course.title}</h1>
      <p>{course.summary}</p>

      <Suspense fallback={<p>Loading reviews…</p>}>
        <Reviews courseId={course.id} /> {/* Slow */}
      </Suspense>
    </article>
  );
}
// app/courses/[slug]/Reviews.tsx
export async function Reviews({ courseId }: { courseId: string }) {
  const reviews = await getReviews(courseId); // Takes 2 seconds
  return (
    <ul>
      {reviews.map((r) => (
        <li key={r.id}>{r.rating}★ – {r.text}</li>
      ))}
    </ul>
  );
}

How it works

  1. Next.js renders the page and finds the Suspense boundary.
  2. It sends the title, summary, and the fallback straight away.
  3. When getReviews finishes, the server streams the reviews HTML into the same response.
  4. React swaps the fallback for the real content without a reload.
Tool Scope When to use
loading.tsx Whole route segment A simple loading screen for the entire page
<Suspense> Any component Granular loading for specific slow sections

Streaming data to Client Components: start a fetch on the server without awaiting it, pass the Promise as a prop, and read it in the client with use() inside a Suspense boundary.

'use client';
import { use } from 'react';

export function Stats({ statsPromise }: { statsPromise: Promise<{ learners: number }> }) {
  const stats = use(statsPromise);
  return <p>{stats.learners.toLocaleString('en-IN')} learners enrolled</p>;
}

Common mistakes

Mistake Effect Fix
Awaiting slow data at the top of the page The whole page waits Move it into a child component inside <Suspense>
One Suspense boundary around everything Same as no streaming Wrap each independent slow section separately
Fallbacks with a different size from the content Layout jumps (poor CLS) Make skeletons match the final layout
Putting <Suspense> inside the slow component Boundary does not catch it The boundary must be a parent of the component that waits

Key takeaways

  • Streaming lets fast content appear first and slow content fill in.
  • Use loading.tsx for whole segments and <Suspense> for specific sections.
  • Suspense boundaries are also the foundation of Partial Prerendering (Module 7).

Try it: Add a Reviews component with an artificial 2-second delay (await new Promise(r => setTimeout(r, 2000))) and wrap it in Suspense with a skeleton fallback.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details