Loading UI and streaming

Intermediate · 3 min

How loading.tsx wraps a page in Suspense, why streaming improves perceived speed, and how to stream parts of a page with your own Suspense boundaries.

Before thisLinking and moving between pages

Codevolution · 3 min

Loading UI and streaming

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A `loading.tsx` file automatically wraps its segment's page in a `<Suspense>` boundary, so the fallback appears immediately while the page's data loads.
  • Streaming sends the page in pieces as each is ready, instead of making the user wait for the slowest data before seeing anything.
  • The loading fallback is prefetched with the layout, so navigation to a dynamic route responds at once and shared layouts stay interactive.
  • For finer control, put your own `<Suspense>` boundary around just the slow part, so the rest of the page is not held back.

The problem: the slowest query sets the pace

In a server-rendered page, the server builds HTML and sends it. If the page needs three things, a header that is instant, a product list that takes 100ms, and a recommendations panel that takes 2 seconds, then a naive server waits for all three and sends nothing until the last one finishes. The user sees a blank screen for two seconds because of one slow panel.

Streaming breaks that coupling. The server sends the HTML it already has and keeps the connection open. When the slow part is ready, it sends that too, and the browser slots it in. The user sees structure immediately and details as they arrive. Think of a restaurant bringing out the bread and drinks while the main course is still cooking, instead of holding everything until the whole meal is plated.

loading.tsx: streaming without writing Suspense

React’s tool for this is <Suspense>: wrap something that may be slow, give it a fallback, and React shows the fallback until it is ready. Next.js gives a shortcut for the whole page. Add a loading.tsx file to a segment:

// app/blog/loading.tsx
export default function Loading() {
  return <p>Loading posts…</p>;
}

Next.js automatically wraps that segment’s page (and everything nested under it) in a Suspense boundary using this component as the fallback. The layout renders around it right away. The fallback shows until the page finishes, then the page replaces it.

In the component hierarchy, loading sits inside layout and error and wraps not-found and page. Because the layout is outside the boundary, it is already on screen and stays interactive while the page streams in.

What it does to navigation

This is where it pays off. From linking and moving between pages: a dynamic route cannot be fully prefetched, but the loading fallback can, along with the shared layout. So when the user clicks a link to a dynamic route:

  • The click responds immediately, showing the prefetched fallback.
  • Navigation is interruptible: if the user clicks elsewhere before the page arrives, the router does not wait.
  • Shared layouts stay interactive.

Without a loading file, the click would appear to do nothing until the server answered. The docs recommend adding one to every dynamic route.

Finer control with Suspense

loading.tsx is all-or-nothing for the segment’s page. When only one component is slow, wrap just that:

import { Suspense } from "react";

export default function Page() {
  return (
    <main>
      <h1>Dashboard</h1>
      <Suspense fallback={<p>Loading recommendations…</p>}>
        <Recommendations />
      </Suspense>
    </main>
  );
}

<Recommendations /> is an async Server Component that awaits its data. The heading is sent immediately, and the recommendations stream in later. You can use both: a loading.tsx for the route and inner boundaries for specific panels. For React’s own account of the mechanism, see suspense for data fetching.

One gotcha: status codes

HTTP sends the status line before the body. Once streaming has started, the response is already committed to 200. So if a streamed component later calls notFound(), Next.js can no longer change the status to 404: the not-found UI renders in the page, the status stays 200, and Next.js adds a noindex meta tag so search engines do not index it. On a scratch app with a loading.tsx in app/blog, requesting an unknown post slug returned 200 with the not-found UI in the body. Before streaming starts, you get a real 404. Error handling and not found covers this.

A hint of what is coming

With Cache Components, which the Next.js 16 scaffold enables, the same idea is made explicit: anything that reads request-time data has to sit inside a Suspense boundary, and the build tells you when it does not. loading.tsx counts as that boundary for a whole route. The rendering group covers the full picture under partial prerendering.

What the video covers

Codevolution’s chapters introduce special files, create a loading state, add an artificial delay to see it, enhance the loading UI, and list its benefits. It is a clear demonstration of loading.tsx specifically. It does not cover manual Suspense boundaries or how streaming works under the hood; Stav’s visually explained video, linked under deeper, does both, and Codevolution’s Suspense SSR video adds the React side.

Where this fits

Loading states cover the wait; error states cover the failure. Next, error handling and not found.

Check yourself

  1. 01What does Next.js do when it finds a `loading.tsx` next to a `page.tsx`?

    It wraps the page (and anything nested below) in a `<Suspense>` boundary whose fallback is the loading component. The fallback renders first, and the real page replaces it when its data is ready.

  2. 02Why is a loading file especially useful on dynamic routes?

    Dynamic routes cannot be fully prefetched, so without a loading file the user waits for a server response with no feedback. With one, the layout and fallback are prefetched, navigation is immediate, and the content streams in.

  3. 03When would you use `<Suspense>` directly instead of `loading.tsx`?

    When only part of a page is slow. `loading.tsx` covers the whole segment's page, while a Suspense boundary around one component lets the rest of the page render immediately and only that component streams in.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark