A layout's basket and a page's stock both log when they render. What prints for a visit, a <Link> click, a refresh, and Back?
The teas layout shows the visitor's basket and each tea page shows its stock. Both are read for every request, after connection(), each inside its own <Suspense>, and each logs when it renders. generateStaticParams lists Green tea and Mint tea, and the page has a Refresh button that calls router.refresh(). In a browser you open Green tea, click the Mint tea link, press Refresh, then press Back. What does the terminal print at each step?
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
app/teas/layout.tsximport { Suspense } from "react";
import Link from "next/link";
import { connection } from "next/server";
async function Basket() {
await connection();
console.log("teas layout: Basket rendered");
return <p>Basket: 2 tins</p>;
}
export default function TeasLayout({ children }: LayoutProps<"/teas">) {
return (
<>
<nav>
<Link href="/teas/green">Green tea</Link> <Link href="/teas/mint">Mint tea</Link>
</nav>
<Suspense fallback={<p>Basket: …</p>}>
<Basket />
</Suspense>
{children}
</>
);
}
app/teas/[slug]/page.tsximport { Suspense } from "react";
import { notFound } from "next/navigation";
import { connection } from "next/server";
import RefreshButton from "./refresh-button";
const teas: Record<string, string> = { green: "Green tea", mint: "Mint tea" };
export function generateStaticParams() {
return [{ slug: "green" }, { slug: "mint" }];
}
async function Stock({ slug }: { slug: string }) {
await connection();
// The warehouse takes a moment to answer.
await new Promise((resolve) => setTimeout(resolve, 200));
console.log(`tea page: Stock rendered for ${slug}`);
return <p>{teas[slug]}: 12 tins in stock</p>;
}
export default async function TeaPage({ params }: PageProps<"/teas/[slug]">) {
const { slug } = await params;
if (!(slug in teas)) notFound();
return (
<main>
<h1>{teas[slug]}</h1>
<Suspense fallback={<p>Checking the stock…</p>}>
<Stock slug={slug} />
</Suspense>
<RefreshButton />
</main>
);
}
app/teas/[slug]/refresh-button.tsx"use client";
import { useRouter } from "next/navigation";
export default function RefreshButton() {
const router = useRouter();
return <button onClick={() => router.refresh()}>Refresh</button>;
}
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:
- You run
next build, thennext start. - You open
/teas/greenin a browser. - You click the Mint tea link.
- You press Refresh.
- You press the browser's Back button.
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…