A tea page has loading.tsx and error.tsx, and one tea's stock check throws. What does the server answer, and what does the browser show?
Each tea's page asks the stock service how many boxes are left, which takes a moment and fails for oolong. The tea's folder has a loading.tsx and an error.tsx. Cache Components is on, as create-next-app sets it up. Say each response's status and what its HTML shows once it has arrived, then what the browser shows after each step.
lib/teas.tsimport { connection } from "next/server";
export type Tea = { slug: string; name: string };
export const teas: Tea[] = [
{ slug: "green", name: "Green tea" },
{ slug: "oolong", name: "Oolong tea" },
];
export function getTea(slug: string) {
return teas.find((tea) => tea.slug === slug);
}
/** The boxes left, from the stock service: it takes a moment for each request, and it's down for oolong. */
export async function getStock(slug: string): Promise<number> {
await connection();
await new Promise((resolve) => setTimeout(resolve, 1500));
if (slug === "oolong") throw new Error("The stock service did not answer.");
return 12;
}
app/layout.tsximport Link from "next/link";
export const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>
<nav>
<Link href="/">Home</Link> <Link href="/teas/green">Green tea</Link> <Link href="/teas/oolong">Oolong tea</Link>
</nav>
{children}
</body>
</html>
);
}
app/page.tsxexport default function Home() {
return <h1>Leaf and Kettle</h1>;
}
app/teas/[slug]/page.tsximport { notFound } from "next/navigation";
import { getStock, getTea } from "@/lib/teas";
export default async function TeaPage({ params }: PageProps<"/teas/[slug]">) {
const { slug } = await params;
const tea = getTea(slug);
if (!tea) notFound();
const stock = await getStock(tea.slug);
return (
<main>
<h1>{tea.name}</h1>
<p>In stock: {stock}</p>
</main>
);
}
app/teas/[slug]/loading.tsxexport default function Loading() {
return <p>Checking the stock…</p>;
}
app/teas/[slug]/error.tsx"use client";
export default function TeaError({ retry }: { error: unknown; retry: () => void }) {
return (
<main>
<h2>We couldn't check the stock.</h2>
<button type="button" onClick={() => retry()}>
Try again
</button>
</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:
- You run
next build, thennext start. - You ask the server for
/teas/green. - Then you ask it for
/teas/oolong. - You open
/in a browser. - You click the Green tea link.
- You wait until the stock shows.
- You open
/teas/oolongwith a full page load.
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…