Skip to content

Checkpoint: A Glossary

The Course
•
20 min study

Close the earlier lessons and build this from the brief. The terms come from getTerms() and getTerm(slug) in lib/terms.js.

The brief

  • / lists every term as a link to its own page, /terms/<slug>.
  • Above the list: All · C · L · P · R · S. Each letter links to /?letter=S, which shows only the terms starting with that letter.
  • /terms/state shows the word as a heading, its meaning, and a link More terms starting with S to /?letter=S.
  • An unknown term, such as /terms/zzz, shows No such term with a link back to the glossary.
  • The header's Glossary link is marked as the current page, with aria-current="page", on / only.

Acceptance checks: follow S, then State, then More terms starting with S. Try /terms/zzz.

Your workspace Next.js · 4 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Glossary</header> {children} </body> </html> ); } import { getTerms } from "@/lib/terms.js"; // Build the glossary here. See the brief. export default async function HomePage() { const terms = await getTerms(); return ( <main> <h1>Glossary</h1> <p className="muted">{terms.length} terms</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; } nav a[aria-current="page"] { font-weight: 700; text-decoration: none; color: #1c2430; } // Pretend database of glossary terms. const terms = [ { slug: "component", word: "Component", meaning: "A function that returns part of the page." }, { slug: "layout", word: "Layout", meaning: "A component that wraps the pages under its folder." }, { slug: "props", word: "Props", meaning: "The inputs a component receives." }, { slug: "route", word: "Route", meaning: "A page's address, made by a folder." }, { slug: "server-component", word: "Server component", meaning: "A component that runs on the server." }, { slug: "state", word: "State", meaning: "What a component remembers between renders." }, ]; export async function getTerms() { await new Promise((resolve) => setTimeout(resolve, 100)); return terms; } export async function getTerm(slug) { await new Promise((resolve) => setTimeout(resolve, 100)); return terms.find((term) => term.slug === slug) ?? null; }
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: files

app/terms/[slug]/page.jsx, app/terms/[slug]/not-found.jsx, and a client NavLinks.jsx for the header.

Hint 2: the letter

term.word.startsWith(letter) and term.word[0] give you everything you need.

Show one solution
One solution Next.js · 7 files
import NavLinks from "./NavLinks.jsx"; import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <NavLinks /> </header> {children} </body> </html> ); } import Link from "next/link"; import { getTerms } from "@/lib/terms.js"; const letters = ["C", "L", "P", "R", "S"]; export default async function HomePage({ searchParams }) { const { letter } = await searchParams; const terms = await getTerms(); const shown = letter ? terms.filter((term) => term.word.startsWith(letter)) : terms; return ( <main> <h1>Glossary</h1> <p> <Link href="/">All</Link> {letters.map((l) => ( <span key={l}> {" · "} <Link href={`/?letter=${l}`}>{l}</Link> </span> ))} </p> <ul> {shown.map((term) => ( <li key={term.slug}> <Link href={`/terms/${term.slug}`}>{term.word}</Link> </li> ))} </ul> </main> ); } "use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; export default function NavLinks() { const pathname = usePathname(); return ( <nav> <Link href="/" aria-current={pathname === "/" ? "page" : undefined}> Glossary </Link> </nav> ); } 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; } nav a[aria-current="page"] { font-weight: 700; text-decoration: none; color: #1c2430; } import Link from "next/link"; export default function TermNotFound() { return ( <main> <h1>No such term</h1> <p> Back to the <Link href="/">glossary</Link>. </p> </main> ); } import Link from "next/link"; import { notFound } from "next/navigation"; import { getTerm } from "@/lib/terms.js"; export default async function TermPage({ params }) { const { slug } = await params; const term = await getTerm(slug); if (!term) notFound(); return ( <main> <h1>{term.word}</h1> <p>{term.meaning}</p> <p> <Link href={`/?letter=${term.word[0]}`}>More terms starting with {term.word[0]}</Link> </p> </main> ); } // Pretend database of glossary terms. const terms = [ { slug: "component", word: "Component", meaning: "A function that returns part of the page." }, { slug: "layout", word: "Layout", meaning: "A component that wraps the pages under its folder." }, { slug: "props", word: "Props", meaning: "The inputs a component receives." }, { slug: "route", word: "Route", meaning: "A page's address, made by a folder." }, { slug: "server-component", word: "Server component", meaning: "A component that runs on the server." }, { slug: "state", word: "State", meaning: "What a component remembers between renders." }, ]; export async function getTerms() { await new Promise((resolve) => setTimeout(resolve, 100)); return terms; } export async function getTerm(slug) { await new Promise((resolve) => setTimeout(resolve, 100)); return terms.find((term) => term.slug === slug) ?? null; }
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. Which file serves /terms/state, and how does it find out state?
  2. Why is a filter better in the URL than in state?
  3. When would you use router.replace instead of router.push?

Was this article helpful?

Your response is saved on this device.