A cached component renders a price that reads cookies() inside its own <Suspense>. Does next build pass?
The tea shop's home page shows the tea of the week in TeaOfTheWeek, cached for days. Members get 10% off, so the price is a component of its own, MemberPrice, which reads the member cookie. A teammate says that's fine: MemberPrice isn't cached itself, and it sits inside its own <Suspense>. You run next build. What happens?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>
<header>Leaf and Kettle</header>
{children}
</body>
</html>
);
}
lib/teas.tsexport type Tea = { slug: string; name: string; about: string; price: number };
const teas: Tea[] = [
{ slug: "green", name: "Green tea", about: "Light and fresh.", price: 4 },
{ slug: "mint", name: "Mint tea", about: "Cool and sweet.", price: 3 },
{ slug: "oolong", name: "Oolong tea", about: "Rich and smooth.", price: 6 },
];
/** 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);
}
app/page.tsximport { Suspense } from "react";
import { cookies } from "next/headers";
import { cacheLife } from "next/cache";
import { getTea } from "@/lib/teas";
async function MemberPrice({ price }: { price: number }) {
const member = (await cookies()).has("member");
return <p>{member ? `Member price: $${(price * 0.9).toFixed(2)}` : `Price: $${price.toFixed(2)}`}</p>;
}
async function TeaOfTheWeek() {
"use cache";
cacheLife("days");
const tea = await getTea("oolong");
if (!tea) return null;
return (
<section>
<h2>Tea of the week: {tea.name}</h2>
<p>{tea.about}</p>
<Suspense fallback={<p>Price: …</p>}>
<MemberPrice price={tea.price} />
</Suspense>
</section>
);
}
export default function Home() {
return (
<main>
<h1>Fresh tea, packed by hand</h1>
<TeaOfTheWeek />
</main>
);
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build. What happens:
- You run
next build.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…