completefrontend

Predict the outputHard

Four components fetch the same URL, one with cache: "no-store" and one with a timeout signal. How many requests reach the API?

The home page shows today's stock four ways, and each part fetches the stock list itself through getStock(). To count the requests, the stock API here is a Route Handler in the same app that logs each one it answers; in the real shop it's the supplier's service. You build the app, start it, and ask for / twice. What does the terminal print?

app/layout.tsx
export const metadata = { title: "Leaf and Kettle" };
 
export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}
app/api/stock/route.ts
import { connection } from "next/server";
 
let answered = 0;
 
// Stands in for the supplier's stock API, and logs every request it answers.
export async function GET() {
  await connection();
  answered += 1;
  console.log(`stock API: request ${answered}`);
  return Response.json([
    { slug: "green", name: "Green tea", left: 12 },
    { slug: "mint", name: "Mint tea", left: 0 },
    { slug: "oolong", name: "Oolong tea", left: 3 },
  ]);
}
lib/stock.ts
import { headers } from "next/headers";
 
export type Stock = { slug: string; name: string; left: number };
 
// The stock API is on this same server here, so its address comes from the request's Host header.
export async function getStock(init?: RequestInit): Promise<Stock[]> {
  const host = (await headers()).get("host");
  const response = await fetch(`http://${host}/api/stock`, init);
  return response.json();
}
app/page.tsx
import { Suspense } from "react";
import { getStock } from "@/lib/stock";
 
async function InStock() {
  const teas = await getStock();
  return <p>{teas.filter((tea) => tea.left > 0).length} teas in stock</p>;
}
 
async function StockList() {
  const teas = await getStock();
  return (
    <ul>
      {teas.map((tea) => (
        <li key={tea.slug}>
          {tea.name}: {tea.left} left
        </li>
      ))}
    </ul>
  );
}
 
async function SoldOut() {
  const teas = await getStock({ cache: "no-store" });
  const names = teas.filter((tea) => tea.left === 0).map((tea) => tea.name);
  return <p>Sold out: {names.join(", ")}</p>;
}
 
async function AlmostGone() {
  const teas = await getStock({ signal: AbortSignal.timeout(5000) });
  const names = teas.filter((tea) => tea.left > 0 && tea.left < 5).map((tea) => tea.name);
  return <p>Almost gone: {names.join(", ")}</p>;
}
 
export default function Home() {
  return (
    <main>
      <h1>Stock today</h1>
      <Suspense fallback={<p>Asking the supplier…</p>}>
        <InStock />
        <StockList />
        <SoldOut />
        <AlmostGone />
      </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 /.
  3. You ask for / again.

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…