Every tea page at /teas/[slug] should have its own title, description, and social card, so a search result or a shared link shows the tea and not the shop's defaults. The page in app/teas/[slug]/page.tsx already shows the tea. Add generateMetadata to it: Next.js calls it with the same params the page gets, a promise of { slug }, and writes what it returns into the page's <head>.
Load the tea with getTea(slug) from @/lib/teas, and call notFound() when there's none, as the page does. Then return:
title: the tea's name. The root layout's title template adds | Leaf and Kettle to it.
description: the tea's about text, when it's 155 characters or fewer. A longer text is cut after the last whole word that fits in 154 characters, and gets … (one character) after it, so it's never longer than 155.
alternates.canonical: the tea's path, like /teas/mint, never with a query string, so /teas/mint?ref=newsletter counts as the same page. The root layout sets metadataBase to https://leafandkettle.example, so a path is enough, here and below.
openGraph: the same title and description, url set to the canonical path, siteName: "Leaf and Kettle", type: "website", and images with the tea's card, { url: tea.image, width: 1200, height: 630, alt: tea.name }.
robots: { index: false, follow: true } for a retired tea, whose page stays up for its reviews but shouldn't come up in searches. For every other tea, leave the robots key out altogether: Next.js merges a page's metadata into its layouts' key by key, and a key that's there, even set to undefined, replaces the layout's.
await generateMetadata({ params: Promise.resolve({ slug: "mint" }) })
→ {
title: "Mint tea",
description: "Cool, sweet peppermint leaves from our own garden. Lovely after dinner, hot or iced.",
alternates: { canonical: "/teas/mint" },
openGraph: {
title: "Mint tea",
description: "Cool, sweet peppermint leaves from our own garden. Lovely after dinner, hot or iced.",
url: "/teas/mint",
siteName: "Leaf and Kettle",
type: "website",
images: [{ url: "/images/teas/mint-card.jpg", width: 1200, height: 630, alt: "Mint tea" }],
},
}
lib/teas.ts, read-only. Import it as @/lib/teas.
export type Tea = {
slug: string;
name: string;
about: string;
/** The tea's social card picture, 1200 by 630 pixels, in public/images/teas. */
image: string;
price: number;
/** A tea the shop no longer makes. Its page stays up for its reviews. */
retired: boolean;
};
const teas: Tea[] = [
{
slug: "green",
name: "Green tea",
about: "Our everyday green tea: young leaves from a family farm in Shizuoka, steamed within hours of picking. Grassy and bright, with a gentle sweetness that lasts into the second and third cups.",
image: "/images/teas/green-card.jpg",
price: 4,
retired: false,
},
{
slug: "mint",
name: "Mint tea",
about: "Cool, sweet peppermint leaves from our own garden. Lovely after dinner, hot or iced.",
image: "/images/teas/mint-card.jpg",
price: 3,
retired: false,
},
{
slug: "jasmine-pearls",
name: "Jasmine pearls",
about: "Green tea leaves, rolled into little pearls and scented with fresh jasmine blossoms over six nights. Each pearl opens slowly, so brew them again and again.",
image: "/images/teas/jasmine-pearls-card.jpg",
price: 8,
retired: false,
},
{
slug: "winter-blend",
name: "Winter blend",
about: "Black tea with orange peel, cinnamon, and clove. We stopped making it, but its reviews are still here.",
image: "/images/teas/winter-blend-card.jpg",
price: 5,
retired: true,
},
];
/** The tea with this slug, or undefined when the shop has never sold it. */
export async function getTea(slug: string): Promise<Tea | undefined> {
return teas.find((tea) => tea.slug === slug);
}
const meta = await generateMetadata({ params: params({ slug: "mint" }) });
expect(meta.title).toBe("Mint tea");
expect(meta.description).toBe("Cool, sweet peppermint leaves from our own garden. Lovely after dinner, hot or iced.");
const green = await generateMetadata({ params: params({ slug: "green" }) });
expect(green.description).toBe("Our everyday green tea: young leaves from a family farm in Shizuoka, steamed within hours of picking. Grassy and bright, with a gentle sweetness that…");
// Exactly 155 characters: it fits, so it stays whole.
const jasmine = await generateMetadata({ params: params({ slug: "jasmine-pearls" }) });
expect(jasmine.description).toBe("Green tea leaves, rolled into little pearls and scented with fresh jasmine blossoms over six nights. Each pearl opens slowly, so brew them again and again.");
const meta = await generateMetadata({ params: params({ slug: "green" }), searchParams: params({ ref: "newsletter" }) });
expect(meta.alternates?.canonical).toBe("/teas/green");
const meta = await generateMetadata({ params: params({ slug: "green" }) });
const card = meta.openGraph ?? {};
expect(card.title).toBe("Green tea");
expect(card.description).toBe("Our everyday green tea: young leaves from a family farm in Shizuoka, steamed within hours of picking. Grassy and bright, with a gentle sweetness that…");
expect(card.url).toBe("/teas/green");
expect(card.siteName).toBe("Leaf and Kettle");
expect(card.type).toBe("website");
expect([].concat(card.images ?? [])).toEqual([{ url: "/images/teas/green-card.jpg", width: 1200, height: 630, alt: "Green tea" }]);
const retired = await generateMetadata({ params: params({ slug: "winter-blend" }) });
expect(retired.robots).toEqual({ index: false, follow: true });
// A robots key, even an undefined one, would replace the layout's robots settings.
const green = await generateMetadata({ params: params({ slug: "green" }) });
expect(Object.keys(green)).not.toContain("robots");
expect(await accessErrorOf(() => generateMetadata({ params: params({ slug: "coffee" }) }))).toBe(404);
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).
I'm here to help with Titles, descriptions, and social cards for tea pages. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.