Skip to the code

Tea pages under their category

Leaf and Kettle's shop puts each tea under its category: /shop/green/sencha, /shop/black/assam. Write the tea page, app/shop/[category]/[slug]/page.tsx. Its route has two dynamic segments, so params is a promise of both: { category, slug }.

A tea's slug is unique across the whole shop, so load the tea by slug alone, with getTea from @/lib/shop. When there's no such tea, call notFound().

When the tea exists but the URL names another category, like /shop/black/sencha (an old link from before a tea moved, or a typo), don't show it there: call permanentRedirect from next/navigation with the tea's own URL, /shop/green/sencha. That answers with status 308, so browsers and search engines keep the new address. A category the shop doesn't have at all is handled the same way.

Show the tea's name in an <h1>, its price as $6.00, and a link (with next/link) back to its category: Back to Green teas, to /shop/green. The category's name comes from getCategory.

Also export generateStaticParams, returning { category, slug } for every tea in getTeas(), in order. One function fills both segments, so next build makes every tea's page under its own category.

/shop/green/matcha → Matcha, $9.00, Back to Green teas (/shop/green)
/shop/black/sencha → 308 to /shop/green/sencha
/shop/green/coffee → 404

The app's other files

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

export type Category = { slug: string; name: string };
export type Tea = { slug: string; name: string; category: string; price: number };
 
const categories: Category[] = [
  { slug: "green", name: "Green teas" },
  { slug: "black", name: "Black teas" },
  { slug: "herbal", name: "Herbal teas" },
];
 
const teas: Tea[] = [
  { slug: "sencha", name: "Sencha", category: "green", price: 6 },
  { slug: "matcha", name: "Matcha", category: "green", price: 9 },
  { slug: "assam", name: "Assam", category: "black", price: 4 },
  { slug: "earl-grey", name: "Earl Grey", category: "black", price: 5 },
  { slug: "mint", name: "Mint", category: "herbal", price: 3 },
];
 
/** Every tea, in the shop's order. A tea's slug is unique across the whole shop. */
export async function getTeas(): Promise<Tea[]> {
  return teas;
}
 
/** The tea with this slug, or undefined when the shop doesn't sell it. */
export async function getTea(slug: string): Promise<Tea | undefined> {
  return teas.find((tea) => tea.slug === slug);
}
 
/** The category with this slug, or undefined when the shop doesn't have it. */
export async function getCategory(slug: string): Promise<Category | undefined> {
  return categories.find((category) => category.slug === slug);
}

What the tests check

  1. shows the tea with its price and a link back to its category
    await renderServer(<Page params={params({ category: "green", slug: "matcha" })} />);
    expect(screen.getByRole("heading", { level: 1, name: "Matcha" })).toBeInTheDocument();
    expect(screen.getByText("$9.00")).toBeInTheDocument();
    expect(screen.getByRole("link", { name: "Back to Green teas" })).toHaveAttribute("href", "/shop/green");
    
  2. calls notFound() for a tea the shop doesn't sell
    expect(await accessErrorOf(() => renderServer(<Page params={params({ category: "green", slug: "coffee" })} />))).toBe(404);
    expect(await accessErrorOf(() => renderServer(<Page params={params({ category: "coffee", slug: "espresso" })} />))).toBe(404);
    
  3. sends a tea under the wrong category to its own URL, for good
    const where = await redirectOf(() => renderServer(<Page params={params({ category: "black", slug: "sencha" })} />));
    expect(where).toEqual({ url: "/shop/green/sencha", status: 308, type: "replace" });
    
  4. sends a tea under a category the shop doesn't have to its own URL too
    const where = await redirectOf(() => renderServer(<Page params={params({ category: "tisanes", slug: "mint" })} />));
    expect(where).toEqual({ url: "/shop/herbal/mint", status: 308, type: "replace" });
    
  5. generateStaticParams fills both segments for every tea
    expect(await generateStaticParams()).toEqual([
      { category: "green", slug: "sencha" },
      { category: "green", slug: "matcha" },
      { category: "black", slug: "assam" },
      { category: "black", slug: "earl-grey" },
      { category: "herbal", slug: "mint" },
    ]);
    
  6. every page it lists renders, without a redirect
    for (const each of await generateStaticParams()) {
      await renderServer(<Page params={params(each)} />);
      expect(screen.getByRole("link", { name: /^Back to / })).toHaveAttribute("href", "/shop/" + each.category);
      cleanup();
    }
    

Learn more

app/shop/[category]/[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).