Skip to the code

Brewing guides at any depth

Leaf and Kettle's brewing guides nest as deep as a topic needs: /guides/green, /guides/green/brewing, /guides/green/brewing/cold. Write one page for all of them, app/guides/[...slug]/page.tsx. The three dots make [...slug] a catch-all segment: it matches one or more segments after /guides, and Next.js passes them in params, a promise of { slug }, where slug is an array, ["green", "brewing"] for /guides/green/brewing. The bare /guides has a page of its own, so this one always gets at least one segment.

A guide's path is its segments joined with /, like green/brewing. Load it with getGuide(path) from @/lib/guides, and call notFound() from next/navigation when there's no guide at that path.

Show the guide's title in an <h1> and its body in a paragraph. Above them, put a breadcrumb trail: a <nav aria-label="Breadcrumb"> holding an <ol>, with an <li> for each step. First a Guides link to /guides, then a link to every guide above this one with its title as the text, and last the guide's own title as plain text, not a link. Make the links with next/link.

Also export generateStaticParams, which returns { slug } for every guide getGuides() gives, in order, with slug as the array of segments, so next build makes every guide's page ahead of time.

// /guides/green/brewing/cold has this trail:
<nav aria-label="Breadcrumb">
  <ol>
    <li><a href="/guides">Guides</a></li>
    <li><a href="/guides/green">Green tea</a></li>
    <li><a href="/guides/green/brewing">Brewing green tea</a></li>
    <li>Cold brewing green tea</li>
  </ol>
</nav>
 
// /guides/green/steeping → 404

The app's other files

lib/guides.ts, read-only. Import it as @/lib/guides.

export type Guide = { path: string; title: string; body: string };
 
const guides: Guide[] = [
  { path: "green", title: "Green tea", body: "Delicate leaves that like water below boiling." },
  { path: "green/brewing", title: "Brewing green tea", body: "Steep at 80°C for 2 minutes." },
  { path: "green/brewing/cold", title: "Cold brewing green tea", body: "Steep in cold water in the fridge for 6 hours." },
  { path: "oolong", title: "Oolong tea", body: "Rich, floral, and good for many short steeps." },
  { path: "oolong/brewing", title: "Brewing oolong tea", body: "Steep at 90°C for 1 minute, then a little longer each time." },
];
 
/** Every guide, in the shop's order. */
export async function getGuides(): Promise<Guide[]> {
  return guides;
}
 
/** The guide at this path, like "green/brewing", or undefined when there's none. */
export async function getGuide(path: string): Promise<Guide | undefined> {
  return guides.find((guide) => guide.path === path);
}

What the tests check

  1. shows the guide at the URL's path
    await renderServer(<Page params={params({ slug: ["green", "brewing"] })} />);
    expect(screen.getByRole("heading", { level: 1, name: "Brewing green tea" })).toBeInTheDocument();
    expect(screen.getByText("Steep at 80°C for 2 minutes.")).toBeInTheDocument();
    
  2. works for one segment and for three
    await renderServer(<Page params={params({ slug: ["oolong"] })} />);
    expect(screen.getByRole("heading", { level: 1, name: "Oolong tea" })).toBeInTheDocument();
    cleanup();
    await renderServer(<Page params={params({ slug: ["green", "brewing", "cold"] })} />);
    expect(screen.getByRole("heading", { level: 1, name: "Cold brewing green tea" })).toBeInTheDocument();
    expect(screen.getByText("Steep in cold water in the fridge for 6 hours.")).toBeInTheDocument();
    
  3. calls notFound() when there's no guide at the path
    expect(await accessErrorOf(() => renderServer(<Page params={params({ slug: ["green", "steeping"] })} />))).toBe(404);
    expect(await accessErrorOf(() => renderServer(<Page params={params({ slug: ["brewing"] })} />))).toBe(404);
    
  4. the breadcrumb links every guide above this one, then shows this one as text
    await renderServer(<Page params={params({ slug: ["green", "brewing", "cold"] })} />);
    const trail = within(screen.getByRole("navigation", { name: "Breadcrumb" }));
    expect(trail.getAllByRole("link").map((link) => [link.textContent, link.getAttribute("href")])).toEqual([
      ["Guides", "/guides"],
      ["Green tea", "/guides/green"],
      ["Brewing green tea", "/guides/green/brewing"],
    ]);
    expect(trail.getByText("Cold brewing green tea").closest("a")).toBeNull();
    expect(trail.getAllByRole("listitem").length).toBe(4);
    
  5. a top-level guide's trail is Guides and the guide
    await renderServer(<Page params={params({ slug: ["oolong"] })} />);
    const trail = within(screen.getByRole("navigation", { name: "Breadcrumb" }));
    expect(trail.getAllByRole("link").map((link) => link.getAttribute("href"))).toEqual(["/guides"]);
    expect(trail.getAllByRole("listitem").map((item) => item.textContent)).toEqual(["Guides", "Oolong tea"]);
    
  6. generateStaticParams lists every guide as an array of segments
    expect(await generateStaticParams()).toEqual([
      { slug: ["green"] },
      { slug: ["green", "brewing"] },
      { slug: ["green", "brewing", "cold"] },
      { slug: ["oolong"] },
      { slug: ["oolong", "brewing"] },
    ]);
    

Learn more

app/guides/[...slug]/page.tsxSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).