Skip to content

Checkpoint: A Study Dashboard

The Course
•
20 min study

Close the earlier lessons and build this from the brief. The data functions are in lib/dashboard.js.

The brief

Dashboard (/):

  • Hello, Ada as the heading, from getProfile(), shown as soon as the profile is loaded.
  • Under it, 4-day streak · 240 minutes this week, from getStreak() and getMinutesThisWeek(). These are slow, so show Loading progress… until both are ready, without holding up the heading.
  • The two slow queries run at the same time.

History (/history):

  • History as the heading, and a list such as Monday: Routing, from getHistory().
  • It takes over a second, so following History shows Loading history… immediately.

Acceptance checks: reload / and watch the heading appear before the progress line, which arrives about a second later (not two). Follow History.

Your workspace Next.js · 4 files
import Link from "next/link"; import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <nav> <Link href="/">Dashboard</Link> <Link href="/history">History</Link> </nav> </header> {children} </body> </html> ); } import { getProfile } from "@/lib/dashboard.js"; // Build the dashboard here. See the brief. export default async function DashboardPage() { const profile = await getProfile(); return ( <main> <h1>Hello, {profile.name}</h1> </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; } const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getProfile() { await wait(200); return { name: "Ada" }; } export async function getStreak() { await wait(1000); return 4; } export async function getMinutesThisWeek() { await wait(1000); return 240; } export async function getHistory() { await wait(1200); return [ { date: "Monday", topic: "Routing" }, { date: "Tuesday", topic: "Layouts" }, { date: "Wednesday", topic: "Server components" }, ]; }
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 progress line

An async Progress component that loads both values with Promise.all, inside a <Suspense> in the page.

Hint 2: History

app/history/page.jsx and app/history/loading.jsx.

Show one solution
One solution 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="/">Dashboard</Link> <Link href="/history">History</Link> </nav> </header> {children} </body> </html> ); } import { Suspense } from "react"; import { getMinutesThisWeek, getProfile, getStreak } from "@/lib/dashboard.js"; // Load fresh data on every request, rather than once when the site is built. export const dynamic = "force-dynamic"; async function Progress() { const [streak, minutes] = await Promise.all([getStreak(), getMinutesThisWeek()]); return ( <p> {streak}-day streak · {minutes} minutes this week </p> ); } export default async function DashboardPage() { const profile = await getProfile(); return ( <main> <h1>Hello, {profile.name}</h1> <Suspense fallback={<p className="muted">Loading progress…</p>}> <Progress /> </Suspense> </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; } export default function Loading() { return ( <main> <p className="muted">Loading history…</p> </main> ); } import { getHistory } from "@/lib/dashboard.js"; // Load fresh data on every request, rather than once when the site is built. export const dynamic = "force-dynamic"; export default async function HistoryPage() { const history = await getHistory(); return ( <main> <h1>History</h1> <ul> {history.map((entry) => ( <li key={entry.date}> {entry.date}: {entry.topic} </li> ))} </ul> </main> ); } const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); export async function getProfile() { await wait(200); return { name: "Ada" }; } export async function getStreak() { await wait(1000); return 4; } export async function getMinutesThisWeek() { await wait(1000); return 240; } export async function getHistory() { await wait(1200); return [ { date: "Monday", topic: "Routing" }, { date: "Tuesday", topic: "Layouts" }, { date: "Wednesday", topic: "Server components" }, ]; }
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.

Check yourself

  1. When should two queries run one after the other instead of together?
  2. What's the difference between loading.jsx and a <Suspense> in a page?
  3. Why does an error page write its own message instead of showing error.message?

Was this article helpful?

Your response is saved on this device.