completefrontend

Predict the outputHard

A tea opens in a modal from the list. What shows after you click Cart, press Back, and reload?

The tea list at /teas opens each tea in a modal: a parallel route slot, @modal, holds an intercepting route, (.)teas/[slug], and its default.tsx shows nothing. Each tea also has its own full page, app/teas/[slug]/page.tsx. The nav at the top links to the list and the cart. Cache Components is on, as create-next-app sets it up. What does the page show after each step, and at which address?

lib/teas.ts
export type Tea = { slug: string; name: string; about: string };
 
export const teas: Tea[] = [
  { slug: "green", name: "Green tea", about: "Light and fresh." },
  { slug: "mint", name: "Mint tea", about: "Cool and sweet." },
];
 
export function getTea(slug: string) {
  return teas.find((tea) => tea.slug === slug);
}
app/layout.tsx
import Link from "next/link";
 
export const metadata = { title: "Leaf and Kettle" };
 
export default function RootLayout({ children, modal }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>
        <nav>
          <Link href="/teas">Teas</Link> <Link href="/cart">Cart</Link>
        </nav>
        {children}
        {modal}
      </body>
    </html>
  );
}
app/page.tsx
export default function Home() {
  return <h1>Leaf and Kettle</h1>;
}
app/teas/page.tsx
import Link from "next/link";
import { teas } from "@/lib/teas";
 
export default function TeasPage() {
  return (
    <main>
      <h1>Our teas</h1>
      <ul>
        {teas.map((tea) => (
          <li key={tea.slug}>
            <Link href={`/teas/${tea.slug}`}>{tea.name}</Link>
          </li>
        ))}
      </ul>
    </main>
  );
}
app/teas/[slug]/page.tsx
import { notFound } from "next/navigation";
import { getTea, teas } from "@/lib/teas";
 
export function generateStaticParams() {
  return teas.map((tea) => ({ slug: tea.slug }));
}
 
export default async function TeaPage({ params }: PageProps<"/teas/[slug]">) {
  const { slug } = await params;
  const tea = getTea(slug);
  if (!tea) notFound();
 
  return (
    <main>
      <h1>{tea.name}</h1>
      <p>{tea.about}</p>
    </main>
  );
}
app/cart/page.tsx
export default function CartPage() {
  return (
    <main>
      <h1>Your cart</h1>
      <p>Your cart is empty.</p>
    </main>
  );
}
app/@modal/default.tsx
export default function NoModal() {
  return null;
}
app/@modal/(.)teas/[slug]/page.tsx
import { notFound } from "next/navigation";
import { getTea, teas } from "@/lib/teas";
import Modal from "@/app/ui/modal";
 
export function generateStaticParams() {
  return teas.map((tea) => ({ slug: tea.slug }));
}
 
export default async function TeaModal({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const tea = getTea(slug);
  if (!tea) notFound();
 
  return (
    <Modal title={tea.name}>
      <p>{tea.about}</p>
    </Modal>
  );
}
app/ui/modal.tsx
"use client";
 
import { useRouter } from "next/navigation";
 
export default function Modal({ title, children }: { title: string; children: React.ReactNode }) {
  const router = useRouter();
 
  return (
    <div role="dialog" aria-label={title}>
      <h2>{title}</h2>
      {children}
      <button type="button" onClick={() => router.back()}>
        Close
      </button>
    </div>
  );
}

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 and next start, and open /teas in a browser.
  2. You click Mint tea in the list.
  3. You click Cart in the nav.
  4. You press the browser's Back button.
  5. You load /teas/mint again with a full page load, as a reload does.

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…