completefrontend

Write the codeMedium

Prerender only each category's two best sellers with a generateStaticParams that gets its category from the layout

Leaf and Kettle's shop has a page for each tea under its category, like /shop/green/matcha. The category's layout already lists the categories for the build:

app/shop/[category]/layout.tsx
import { getCategories } from "@/lib/shop";
 
export async function generateStaticParams() {
  const categories = await getCategories();
  return categories.map((category) => ({ category: category.slug }));
}
 
export default function CategoryLayout({ children }: LayoutProps<"/shop/[category]">) {
  return <section>{children}</section>;
}

Write the tea page, app/shop/[category]/[slug]/page.tsx. The shop sells hundreds of teas, so the build makes only each category's two best sellers ahead of time; the rest are prerendered on their first visit. Export generateStaticParams: Next.js calls it once for each category the layout returned, with params holding that category, like { category: "green" }. Return { slug } for the category's two teas with the most sold, most first, or the one tea of a category that has only one. Leave the category out: the layout fills it in.

The page shows the tea's name in an <h1>, its price as $9.00, and its category's name as In Green teas. Call notFound() when the shop doesn't sell the tea, or when it belongs to another category than the URL's.

params is a promise of { category, slug }. Cache Components is on, so await it in a component inside <Suspense>, with <p>Loading the tea…</p> as its fallback, not in the page itself: then a tea the build wasn't given still gets the route's shell at once.

Write this: app/shop/[category]/[slug]/page.tsx
import { Suspense } from "react";
import { notFound } from "next/navigation";
import { getCategory, getTea, getTeasIn, teas } from "@/lib/shop";
 
export async function generateStaticParams() {
  return teas.map((tea) => ({ category: tea.category, slug: tea.slug }));
}
 
export default async function ShopTeaPage({ params }: { params: Promise<{ category: string; slug: string }> }) {
  const { category, slug } = await params;
  const tea = await getTea(slug);
  if (!tea) notFound();
 
  return (
    <main>
      <h1>{tea.name}</h1>
    </main>
  );
}
lib/shop.ts (read-only)
export type Category = { slug: string; name: string };
export type Tea = { slug: string; category: string; name: string; price: number; sold: number };
 
export const categories: Category[] = [
  { slug: "green", name: "Green teas" },
  { slug: "black", name: "Black teas" },
  { slug: "herbal", name: "Herbal teas" },
];
 
/** `sold` is the tins sold this year: the best sellers have the most. */
export const teas: Tea[] = [
  { slug: "sencha", category: "green", name: "Sencha", price: 6, sold: 340 },
  { slug: "matcha", category: "green", name: "Matcha", price: 9, sold: 520 },
  { slug: "gyokuro", category: "green", name: "Gyokuro", price: 14, sold: 75 },
  { slug: "assam", category: "black", name: "Assam", price: 5, sold: 410 },
  { slug: "earl-grey", category: "black", name: "Earl Grey", price: 6, sold: 610 },
  { slug: "lapsang", category: "black", name: "Lapsang souchong", price: 7, sold: 120 },
  { slug: "mint", category: "herbal", name: "Mint", price: 4, sold: 290 },
];
 
/** Every category, in the shop's order. */
export async function getCategories(): Promise<Category[]> {
  return categories.map((category) => ({ ...category }));
}
 
/** The category with this slug, or undefined. */
export async function getCategory(slug: string): Promise<Category | undefined> {
  const category = categories.find((each) => each.slug === slug);
  return category && { ...category };
}
 
/** The teas in a category, in the shop's order (not by sales). */
export async function getTeasIn(category: string): Promise<Tea[]> {
  return teas.filter((tea) => tea.category === category).map((tea) => ({ ...tea }));
}
 
/** The tea with this slug, or undefined when the shop doesn't sell it. */
export async function getTea(slug: string): Promise<Tea | undefined> {
  const tea = teas.find((each) => each.slug === slug);
  return tea && { ...tea };
}
The tests your code must pass
  • lists each category's two best sellers, most sold first, by slug only
  • lists the one tea of a category that has only one, so no category comes back empty
  • shows any tea of the category, best seller or not
  • calls notFound() for a tea the shop doesn't sell, or one under another category
  • renders every page it lists, under its own category
  • waits for params inside <Suspense>, so the route's shell needs no slug

Try it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…