Skip to content

Loading Pages

The Course
•
15 min study
A slow stats page Next.js · 6 files
import Link from "next/link"; import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <nav> <Link href="/">Home</Link> <Link href="/stats">Stats</Link> </nav> </header> {children} </body> </html> ); } export default function HomePage() { return ( <main> <h1>Home</h1> <p>Your stats take a moment to work out.</p> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } // Shown straight away while app/stats/page.jsx loads. export default function Loading() { return ( <main> <p className="muted">Working out your stats…</p> </main> ); } import { getStats } from "@/lib/stats.js"; // Load fresh data on every request, rather than once when the site is built. export const dynamic = "force-dynamic"; export default async function StatsPage() { const stats = await getStats(); return ( <main> <h1>Stats</h1> <p> {stats.sessions} sessions · {stats.minutes} minutes </p> </main> ); } const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getStats() { await wait(1500); return { sessions: 12, minutes: 340 }; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

Follow Stats. The stats take 1.5 seconds to work out.

loading.jsx

app/stats/loading.jsx is shown straight away when you go to /stats, inside the layout, and replaced by the page when it's ready. The reader sees the click land at once, and the navigation stays usable while they wait:

JSX
export default function Loading() {
  return (
    <main>
      <p className="muted">Working out your stats…</p>
    </main>
  );
}

Next.js puts the page inside a <Suspense> boundary for you, with the loading file as its fallback. It's the same idea you may know from React's <Suspense>, applied to a whole page.

Predict

Enter app/stats/loading.jsx under the lab, press Remove file, then press Run and follow Stats again. What's on screen during the 1.5 seconds?

Check your prediction

The Home page, unchanged, until the stats arrive. Nothing shows that the click did anything. That's why slow pages need a loading file. Press Restore starter files and Run to bring the file back.

Exercise

Keep the heading

When the stats arrive, the Stats heading appears and pushes the text down. Make the loading page show the heading too, so only the numbers change.

Your working space Next.js · 6 files
import Link from "next/link"; import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <nav> <Link href="/">Home</Link> <Link href="/stats">Stats</Link> </nav> </header> {children} </body> </html> ); } export default function HomePage() { return ( <main> <h1>Home</h1> <p>Your stats take a moment to work out.</p> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } // Shown straight away while app/stats/page.jsx loads. export default function Loading() { return ( <main> <p className="muted">Working out your stats…</p> </main> ); } import { getStats } from "@/lib/stats.js"; // Load fresh data on every request, rather than once when the site is built. export const dynamic = "force-dynamic"; export default async function StatsPage() { const stats = await getStats(); return ( <main> <h1>Stats</h1> <p> {stats.sessions} sessions · {stats.minutes} minutes </p> </main> ); } const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getStats() { await wait(1500); return { sessions: 12, minutes: 340 }; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

A useful loading state keeps the page structure stable while only the missing data changes.

Hint 2: a first step

Add <h1>Stats</h1> to the loading file, above the message.

Show one solution
JSX
export default function Loading() {
  return (
    <main>
      <h1>Stats</h1>
      <p className="muted">Working out your stats…</p>
    </main>
  );
}

Was this article helpful?

Your response is saved on this device.