Skip to article content
FrontendXLesson 13 / 20

Loading States and Streaming

A version-qualified path through the Next.js App Router: server components, client boundaries, data fetching, caching layers, streaming, error handling, proxy, images, performance, testing and deployment, with the Next.js 15 and 16 behavior changes called out where they matter.

intermediate · about 45 minutes · 6 exercises

  • Predict
  • Explain
  • Implement
  • Debug
  • Test
  • Interview

What you will be able to do

#
  • Use loading.tsx and Suspense to build shell-first pages with granular fallbacks.
  • Explain what streams, when, and what the user perceives during navigation.
  • Design fallbacks that avoid layout shift and read as intentional UI.

Overview

#

loading.tsx is a Suspense boundary

A loading.tsx file wraps its segment in a Suspense boundary: the parent layout renders immediately with the loading fallback in place of the segment, and the segment's content streams in when ready. Nested segments each get their own boundary, so navigating deep shows the nearest fallback rather than a blank page.

Granular streaming with Suspense

Inside a page, wrap slow sections in explicit Suspense boundaries so each streams independently:

snippet.tsx
export default function Page() {
  return (
    <main>
      <Header />  {/* ships immediately */}
      <Suspense fallback={<SkeletonRows />}>
        <SlowTable />   {/* streams when its data resolves */}
      </Suspense>
      <Suspense fallback={<SkeletonChart />}>
        <SlowChart />
      </Suspense>
    </main>
  );
}

Siblings stream in parallel: the table does not wait for the chart. The shell (everything outside boundaries) ships with the first byte, which is what makes the page feel instant even when data is slow.

What the user perceives

Navigation to a dynamic route shows the prefetched shell with fallbacks, then content replaces them as streams resolve. Design fallbacks at the final size (skeletons matching real layout) to avoid layout shift, and keep them meaningful: a labeled skeleton beats a spinner for orientation. Under Cache Components this pattern is formalized as the instant shell: the prerendered shell plus partial prefetching ships first, and request-time sections stream.

Passing promises down

A server component can start a read and pass the unresolved promise to a child inside a boundary, or to a client component that unwraps it with use(). This overlaps server work with streaming instead of serializing await-then-render steps. The anti-pattern is one giant await at the top of the page: it serializes everything behind the slowest read and delays the shell.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    app/dashboard has loading.tsx. Inside the page, SlowTable (600 ms) and SlowChart (1200 ms) are each wrapped in their own Suspense. Describe what the user sees at t=0, t=600 and t=1200 during a hard navigation.

    Choose the best answer
  2. Exercise 2Explain

    Explain the difference between loading.tsx and a Suspense boundary inside a page, and when you need both.

    Reveal the reference answer
    reference.text
    loading.tsx is a segment-level boundary: it covers everything the segment renders and activates on navigation into that segment, giving one consistent fallback for the whole area. A Suspense boundary inside a page is section-level: it splits one page into parts that stream independently, so a slow widget does not hold the rest hostage. You need both when a segment has a generally slow or dynamic character (loading.tsx for navigation feel) and specific sections with very different latencies (inner boundaries for granularity). The rule of thumb: loading.tsx for the route, Suspense for the sections.

    Authored expected output: Segment-level versus section-level boundaries plus the both-needed case.

    The two mechanisms compose; neither replaces the other.

  3. Exercise 3Implement

    Build a reports page: the header renders immediately, a summary section and a detail table stream independently, and each fallback is a fixed-height skeleton to avoid layout shift.

    starter.tsx
    // app/reports/page.tsx
    // TODO: header + two Suspense boundaries with sized skeletons

    Reveal the reference answer
    reference.tsx
    import { Suspense } from 'react';
    
    function SummarySkeleton() {
      return <div style={{ height: 120 }} aria-hidden="true" />;
    }
    function TableSkeleton() {
      return <div style={{ height: 400 }} aria-hidden="true" />;
    }
    
    export default function ReportsPage() {
      return (
        <main>
          <h1>Reports</h1>
          <Suspense fallback={<SummarySkeleton />}>
            <Summary />
          </Suspense>
          <Suspense fallback={<TableSkeleton />}>
            <DetailTable />
          </Suspense>
        </main>
      );
    }
    
    // Summary and DetailTable are async server components that fetch their own data.

    Authored expected output: Header first, then each section streams in without shifting the layout.

    Fixed-height skeletons reserve the final space; aria-hidden keeps them out of the accessibility tree.

  4. Exercise 4Debug

    This page shows nothing until all data is ready despite having Suspense boundaries. Find the bug. export default async function Page() { const summary = await getSummary(); const rows = await getRows(); return ( <main> <Suspense fallback={<Skeleton />}><SummaryView data={summary} /></Suspense> <Suspense fallback={<Skeleton />}><RowsView data={rows} /></Suspense> </main> ); }

    starter.tsx
    export default async function Page() {
      const summary = await getSummary();
      const rows = await getRows();
      return (
        <main>
          <Suspense fallback={<Skeleton />}><SummaryView data={summary} /></Suspense>
          <Suspense fallback={<Skeleton />}><RowsView data={rows} /></Suspense>
        </main>
      );
    }

    Reveal the reference answer
    reference.tsx
    The awaits happen in the page before any boundary renders, so the whole page suspends until both reads finish and the fallbacks never show. Move the data fetching into the async child components (or start promises and pass them down unwrapped) so each boundary suspends independently: function Summary() { const data = use(getSummaryPromise()); ... } or async function Summary() { const data = await getSummary(); ... }.

    Authored expected output: The shell renders immediately and each section streams when its own data resolves.

    Boundaries only help when the suspension happens inside them, not before them.

  5. Exercise 5Test

    Write a pure function streamTimeline(sections: { name: string; ms: number }[]) that returns the times at which the shell and each section become visible: shell at 0, each section at its own ms, sorted. Assert on [{a,600},{b,1200}] giving [ { at: 0, name: 'shell' }, { at: 600, name: 'a' }, { at: 1200, name: 'b' } ] and on an empty array giving only the shell entry.

    starter.ts
    export function streamTimeline(
      sections: { name: string; ms: number }[],
    ): { at: number; name: string }[] {
      // TODO
    }

    Reveal the reference answer
    reference.ts
    export function streamTimeline(
      sections: { name: string; ms: number }[],
    ): { at: number; name: string }[] {
      const events = sections.map((s) => ({ at: s.ms, name: s.name }));
      events.sort((x, y) => x.at - y.at);
      return [{ at: 0, name: 'shell' }, ...events];
    }
    
    // Independence is the point: each section's time is its own, not the max.

    Authored expected output: Both assertions pass.

    The timeline model makes the independence property visible.

  6. Exercise 6Interview

    An interviewer asks: how do you make a data-heavy page feel instant even when its slowest API takes two seconds? Cover the shell, boundaries, prefetch interaction, and one metric you would watch.

    Reveal the reference answer
    reference.text
    Ship a shell that does not depend on the slow API: header, layout, and any fast data render immediately, with the slow section isolated in its own Suspense boundary showing a same-size skeleton. Keep the page's loading.tsx so navigations show the fallback immediately, and rely on Link prefetch delivering the shell before the click for linked routes. Inside the slow section, parallelize independent reads and dedupe shared ones with React cache(). The metric: First Contentful Paint or Time to First Byte for the shell plus Cumulative Layout Shift for skeleton sizing; a two-second API should never move FCP, only the section's own resolution time. If the section data is cacheable, add use cache or revalidate so repeat views skip the wait entirely.

    Authored expected output: Shell-first design, boundary isolation, prefetch interaction, FCP and CLS as watched metrics.

    Perceived performance questions test whether you design for the user's timeline, not the server's.

Common misconceptions

#
  • loading.tsx replaces the whole page. It replaces its own segment; parent layout and siblings render normally.
  • Streaming requires special server config. Suspense boundaries are ordinary React; the framework streams their output automatically.
  • Fallbacks are decorative. They define perceived performance and layout stability; mismatched skeleton sizes cause shift.
  • One top-level await is fine because data is cached. Even cached, a single await serializes the shell behind that read.

Follow-up practice

#
  • nextjs-error-handling
  • nextjs-client-boundaries

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.