What is the loading.js file used for?

loading.js creates an instant loading UI for a route segment. Next.js wraps the segment's page (and nested children) in a React <Suspense> boundary with your loading component as the fallback, so users see feedback immediately while the content streams in.

How it works

  • app/dashboard/loading.tsx wraps app/dashboard/page.tsx in <Suspense fallback={<Loading />}>.
  • The fallback is part of the prerendered output, so it can be served instantly from a CDN and prefetched.
  • On navigation, the shared layout stays interactive while the new page streams.
  • It can be a Server Component; it receives no props.

Prefetching

For dynamic routes without Partial Prefetching, the default <Link> prefetch goes down to the nearest loading.tsx. That is why adding a loading file makes navigation to a dynamic page feel instant.

Limitations

  • It covers the page and below, not the layout.tsx in the same segment. If the layout awaits data, the loading UI cannot show until the layout finishes. Put that data in its own <Suspense> boundary.
  • A single loading file per segment is coarse. For finer control, use <Suspense> around specific slow components.
  • Status codes: once streaming starts the response is already 200, so a later notFound() adds a noindex tag instead of changing the status.

Good practice

Use skeletons that match the final layout to avoid layout shift. Use instant = true (route segment config, Cache Components only) or the Navigation Inspector in Next.js DevTools to check that navigations show a loading state immediately.

Code example

tsx
1// app/dashboard/loading.tsx
2export default function Loading() {
3 return (
4 <div className="space-y-3" aria-busy="true" aria-live="polite">
5 <div className="h-8 w-1/3 animate-pulse rounded bg-slate-200" />
6 <div className="h-32 animate-pulse rounded bg-slate-200" />
7 </div>
8 );
9}
10
11// app/dashboard/page.tsx - slow data streams behind the loading UI
12export default async function Dashboard() {
13 const stats = await fetch("https://api.example.com/stats").then((r) => r.json());
14 return <h1>{stats.users} users</h1>;
15}
16
17// What Next.js effectively renders:
18// <DashboardLayout>
19// <Suspense fallback={<Loading />}>
20// <Dashboard />
21// </Suspense>
22// </DashboardLayout>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app