completefrontend

Predict the outputEasy

A "use cache" component logs when it renders. What does the terminal print for four visits to the same page?

The tea shop's brewing page reads the tea and the number of cups from the URL, inside <Suspense>. It shows the tea with TeaCard, a cached component that logs each time it renders, and the cups itself. You build the app, start it, and ask for the page four times. What does the terminal show, and what does each visit get?

app/layout.tsx
export 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.ts
export 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/teas/tea-card.tsx
import { cacheLife } from "next/cache";
import { getTea } from "@/lib/teas";
 
export async function TeaCard({ slug }: { slug: string }) {
  "use cache";
  cacheLife("hours");
  console.log(`TeaCard rendered for ${slug}`);
  const tea = await getTea(slug);
  if (!tea) return <p>We don't sell that tea.</p>;
  return (
    <article>
      <h2>{tea.name}</h2>
      <p>{tea.about}</p>
    </article>
  );
}
app/teas/page.tsx
import { Suspense } from "react";
import { TeaCard } from "./tea-card";
 
async function Pot({ searchParams }: Pick<PageProps<"/teas">, "searchParams">) {
  const { tea = "green", cups = "1" } = await searchParams;
  return (
    <>
      <TeaCard slug={String(tea)} />
      <p>Cups: {String(cups)}</p>
    </>
  );
}
 
export default function BrewPage({ searchParams }: PageProps<"/teas">) {
  return (
    <main>
      <h1>Brew a pot</h1>
      <Suspense fallback={<p>Warming the pot…</p>}>
        <Pot searchParams={searchParams} />
      </Suspense>
    </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 and run with next start. What happens:

  1. You run next build, then next start.
  2. You ask the server for /teas?tea=mint, twice.
  3. You ask it for /teas?tea=oolong.
  4. You ask it for /teas?tea=mint&cups=3.

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…