Leaf and Kettle wants its tea pages to show their price, stock, and stars right in Google's results. Search engines read those from structured data: a JSON-LD object about the page, in a <script type="application/ld+json">. The tea page, app/teas/[slug]/page.tsx, already shows the tea. Add that script to what it renders, with a schema.org Product.
It has "@context": "https://schema.org", "@type": "Product", and the tea's name, description (its about text), sku, and image, as a full URL on SITE_URL. brand is { "@type": "Brand", name }, with the tea's brand as the name.
offers is one { "@type": "Offer" } with url, the tea page's full URL; price, the price as text with two decimals, like "5.50"; priceCurrency: "USD"; and availability, "https://schema.org/InStock" when stock is above 0 and "https://schema.org/OutOfStock" when it isn't.
aggregateRating is { "@type": "AggregateRating", ratingValue, reviewCount }: the average of the reviews' rating, rounded to one decimal, as a number, and how many reviews there are. A tea with no reviews has no aggregateRating at all.
Tea houses write their own descriptions, and one has put a </script> in its. Escape every < in the JSON text as \u003c, so the script can't end early. JSON reads it back as the same <.
/teas/green →
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Product",
"name": "Green tea",
"description": "Light and grassy, from a family farm in Shizuoka.",
"image": "https://leafandkettle.example/images/teas/green.jpg",
"sku": "LK-GRN-100",
"brand": { "@type": "Brand", "name": "Leaf and Kettle" },
"offers": {
"@type": "Offer",
"url": "https://leafandkettle.example/teas/green",
"price": "4.00",
"priceCurrency": "USD",
"availability": "https://schema.org/InStock"
},
"aggregateRating": { "@type": "AggregateRating", "ratingValue": 4.3, "reviewCount": 3 }
}
</script>
lib/teas.ts, read-only. Import it as @/lib/teas.
/** The shop's address in production. Structured data uses full URLs. */
export const SITE_URL = "https://leafandkettle.example";
export type Tea = {
slug: string;
sku: string;
name: string;
/** Written by the tea house that makes the tea, in the partners' dashboard. */
about: string;
/** The tea house that makes it. */
brand: string;
/** A picture in public/images/teas. */
image: string;
/** In US dollars. */
price: number;
stock: number;
};
export type Review = { author: string; rating: number; text: string };
const teas: Tea[] = [
{
slug: "green",
sku: "LK-GRN-100",
name: "Green tea",
about: "Light and grassy, from a family farm in Shizuoka.",
brand: "Leaf and Kettle",
image: "/images/teas/green.jpg",
price: 4,
stock: 30,
},
{
slug: "oolong",
sku: "LK-OOL-100",
name: "Oolong tea",
about: "Rich and smooth, with a long, honeyed finish.",
brand: "Leaf and Kettle",
image: "/images/teas/oolong.jpg",
price: 5.5,
stock: 0,
},
{
slug: "lapsang",
sku: "SH-LAP-050",
name: "Lapsang souchong",
about: 'Pine-smoked and bold. Steep for <4 minutes.</script><script>alert("smoked")</script>',
brand: "Smoke House Teas",
image: "/images/teas/lapsang.jpg",
price: 7.25,
stock: 8,
},
];
const reviews: Record<string, Review[]> = {
green: [
{ author: "Ana", rating: 5, text: "My morning cup." },
{ author: "Kenji", rating: 4, text: "Fresh, like spring." },
{ author: "Lea", rating: 4, text: "Lovely, if a little light." },
],
lapsang: [
{ author: "Sam", rating: 5, text: "A campfire in a cup." },
{ author: "Ana", rating: 4, text: "Strong, but I like it." },
],
};
/** 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);
}
/** A tea's reviews, oldest first: none for a tea nobody has reviewed. */
export async function getReviews(slug: string): Promise<Review[]> {
return reviews[slug] ?? [];
}
// The page's JSON-LD: exactly one script of that type, whose text is JSON.
const jsonLd = () => {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
expect(scripts.length).toBe(1);
return JSON.parse(scripts[0].textContent);
};
await renderServer(<Page params={params({ slug: "green" })} />);
const product = jsonLd();
expect(product["@context"]).toBe("https://schema.org");
expect(product["@type"]).toBe("Product");
expect(product.name).toBe("Green tea");
expect(product.description).toBe("Light and grassy, from a family farm in Shizuoka.");
expect(product.sku).toBe("LK-GRN-100");
expect(product.brand).toEqual({ "@type": "Brand", name: "Leaf and Kettle" });
// The page's JSON-LD: exactly one script of that type, whose text is JSON.
const jsonLd = () => {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
expect(scripts.length).toBe(1);
return JSON.parse(scripts[0].textContent);
};
await renderServer(<Page params={params({ slug: "green" })} />);
const product = jsonLd();
expect(product.image).toBe("https://leafandkettle.example/images/teas/green.jpg");
expect(product.offers?.url).toBe("https://leafandkettle.example/teas/green");
// The page's JSON-LD: exactly one script of that type, whose text is JSON.
const jsonLd = () => {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
expect(scripts.length).toBe(1);
return JSON.parse(scripts[0].textContent);
};
await renderServer(<Page params={params({ slug: "green" })} />);
const offer = jsonLd().offers ?? {};
expect(offer["@type"]).toBe("Offer");
expect(offer.price).toBe("4.00");
expect(offer.priceCurrency).toBe("USD");
expect(offer.availability).toBe("https://schema.org/InStock");
// The page's JSON-LD: exactly one script of that type, whose text is JSON.
const jsonLd = () => {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
expect(scripts.length).toBe(1);
return JSON.parse(scripts[0].textContent);
};
await renderServer(<Page params={params({ slug: "oolong" })} />);
const offer = jsonLd().offers ?? {};
expect(offer.price).toBe("5.50");
expect(offer.availability).toBe("https://schema.org/OutOfStock");
// The page's JSON-LD: exactly one script of that type, whose text is JSON.
const jsonLd = () => {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
expect(scripts.length).toBe(1);
return JSON.parse(scripts[0].textContent);
};
await renderServer(<Page params={params({ slug: "green" })} />);
expect(jsonLd().aggregateRating).toEqual({ "@type": "AggregateRating", ratingValue: 4.3, reviewCount: 3 });
cleanup();
await renderServer(<Page params={params({ slug: "oolong" })} />);
expect(Object.keys(jsonLd())).not.toContain("aggregateRating");
// The page's JSON-LD: exactly one script of that type, whose text is JSON.
const jsonLd = () => {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
expect(scripts.length).toBe(1);
return JSON.parse(scripts[0].textContent);
};
await renderServer(<Page params={params({ slug: "lapsang" })} />);
const script = document.querySelector('script[type="application/ld+json"]');
expect(script).not.toBeNull();
// In the HTML the server sends, a "</script" here would end the script and run the next one.
expect(script.textContent).not.toContain("<");
const tea = await getTea("lapsang");
expect(jsonLd().description).toBe(tea.about);
expect(jsonLd().aggregateRating).toEqual({ "@type": "AggregateRating", ratingValue: 4.5, reviewCount: 2 });
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 Product structured data on the tea page. 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.