🎓 A1Learner
🔍
0 of 50 lessons completed

Next.js Streaming with Suspense

Suspense shows a fallback first, then the real content. The reader sees the shell while the slow part loads.

The slow list inside Suspense

The list waits 1,500 ms for its books. The fallback shows in the meantime.

import { Suspense } from "react";
import { getBooksSlow } from "@/lib/books";

async function SlowBookList() {
  const books = await getBooksSlow(1500);
  return (
    <ul>
      {books.map((book) => (
        <li key={book.id}>{book.title}</li>
      ))}
    </ul>
  );
}

export default function StreamingPage() {
  return (
    <main>
      <h1>My Bookshelf</h1>
      <Suspense fallback={<p>Loading the bookshelf...</p>}>
        <SlowBookList />
      </Suspense>
    </main>
  );
}

The first byte is not early here

The check times the streaming page on the dev server. The shell did not arrive early in this run.

curl -s -o /dev/null -w "first byte: %{time_starttransfer}s total: %{time_total}s\n" http://localhost:3113/streaming

Example

curl -s http://localhost:3111/streaming | grep -o 'Loading the bookshelf...\|Dune' | sort -u

Key points

  • Suspense: a wrapper that shows a fallback until its child is ready
  • Fallback: the placeholder shown while the real content loads
  • The shell is the page frame: the title and the fallback text.
  • The slow list loads after 1,500 ms. The fallback covers that wait.
  • The built page holds both parts: the fallback text and the book titles.

In this example, the check reads the streaming page and finds two strings in it. Loading the bookshelf... is the fallback, shown while the list loads. Dune is the first book in the list, shown when the list is ready. The page holds both parts, so the reader sees the fallback first and the list after. The early first byte was not cleanly measurable with curl here, so this lesson claims the two parts, not a timed shell.

Note

Streaming helps the reader most when the slow part is really slow. A fast page needs no fallback.

Try it Yourself

Edit the code, then press Run.

Running is simulated · TypeScript Playground

Exercise

What does Suspense show while its child loads?