completefrontend

Write the codeMedium

Write a tea page that reads its tea from params and the number of cups from searchParams

Each tea's page at /teas/[slug] is also a brewing guide: /teas/mint?cups=3 says how much tea and water three cups take. Write app/teas/[slug]/page.tsx. Next.js gives the page params, a promise of { slug }, and searchParams, a promise of the query as a plain object.

Load the tea with getTea(slug) from @/lib/teas, and call notFound() when the shop doesn't sell it. Export generateStaticParams, returning { slug } for every tea in teas, in order. Show the tea's name in an <h1> and its about text in a paragraph.

Below them, show the brew for the cups in the URL, like For 3 cups: 9 g of tea and 750 ml of water at 100°C. Each cup takes the tea's grams and 250 ml of water, at the tea's temperature. cups must be a whole number from 1 to 6; anything else, or no cups at all, means one cup, which reads For 1 cup: …. When the URL has cups twice, the first one counts.

Cache Components is on. The build knows every slug, from generateStaticParams, but never the query, so the page itself mustn't wait for searchParams. Keep the heading and the paragraph in the static shell, and show the brew from a component inside <Suspense>, with <p>Working out your brew…</p> as its fallback.

Write this: app/teas/[slug]/page.tsx
import { Suspense } from "react";
import { notFound } from "next/navigation";
import { getTea, teas, type Tea } from "@/lib/teas";
 
export default async function TeaPage({ params, searchParams }: PageProps<"/teas/[slug]">) {
  const { slug } = await params;
  const { cups } = await searchParams;
 
  return (
    <main>
      <h1>{slug}</h1>
      <p>For {cups} cups</p>
    </main>
  );
}
lib/teas.ts (read-only)
export type Tea = { slug: string; name: string; about: string; grams: number; temperature: number };
 
export const teas: Tea[] = [
  { slug: "green", name: "Green tea", about: "Light and fresh.", grams: 2, temperature: 80 },
  { slug: "mint", name: "Mint tea", about: "Cool and sweet.", grams: 3, temperature: 100 },
  { slug: "oolong", name: "Oolong tea", about: "Rich and smooth.", grams: 4, temperature: 90 },
];
 
/** 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 tests your code must pass
  • shows the tea from params, with the brew for the cups in the URL
  • brews one cup without cups, or with a value that isn't a whole number from 1 to 6
  • takes the first cups when the URL has it twice
  • calls notFound() for a tea the shop doesn't sell
  • lists every tea for the build
  • renders its static shell without waiting for searchParams

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…