completefrontend

Write the codeEasy

Write a not-found.tsx for tea pages that suggests teas in stock

Leaf and Kettle's tea page, app/teas/[slug]/page.tsx, calls notFound() when the shop doesn't sell the tea in the URL. Write the screen Next.js shows then: app/teas/[slug]/not-found.tsx. Next.js gives a not-found file no props, so it can't know which tea was asked for, but it's a Server Component, so it can load data itself.

Show an <h1> with We don't sell that tea. Then, under an <h2> with Try one of these, a list of the first three teas that are in stock, in the shop's order, each a next/link Link to /teas/<slug> with the tea's name. When fewer are in stock, show those; when none are, leave out the heading and the list. End with a link, See all teas, to /teas.

Load the teas with getTeas() from @/lib/teas, which returns every tea the shop lists, with inStock on each.

Write this: app/teas/[slug]/not-found.tsx
import Link from "next/link";
import { getTeas } from "@/lib/teas";
 
export default function NotFound() {
  return (
    <main>
      <h1>We don't sell that tea.</h1>
    </main>
  );
}
lib/teas.ts (read-only)
export type Tea = { slug: string; name: string; inStock: boolean };
 
export const teas: Tea[] = [
  { slug: "green", name: "Green tea", inStock: true },
  { slug: "mint", name: "Mint tea", inStock: false },
  { slug: "oolong", name: "Oolong tea", inStock: true },
  { slug: "chai", name: "Masala chai", inStock: true },
  { slug: "rooibos", name: "Rooibos", inStock: true },
];
 
/** Every tea the shop lists, in the shop's order, in stock or not. */
export async function getTeas(): Promise<Tea[]> {
  return teas;
}
The tests your code must pass
  • says the shop doesn't sell it, with no props, as Next.js renders it
  • suggests the first three teas in stock, in the shop's order
  • suggests fewer when fewer are in stock, as getTeas() says
  • leaves out the suggestions when nothing is in stock
  • links with next/link

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…