A dynamic route matches any slug, including ones with no data behind them.
/topics/nope matched [slug], and the page crashed. A missing topic isn't a
crash: it's a page that doesn't exist, and the reader should be told so.
notFound()Next.js · 6 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Topics</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
import { getTopics } from "@/lib/topics.js";
export default async function HomePage() {
const topics = await getTopics();
return (
<main>
<h1>Topics</h1>
<ul>
{topics.map((topic) => (
<li key={topic.slug}>
<Link href={`/topics/${topic.slug}`}>{topic.title}</Link>
</li>
))}
</ul>
</main>
);
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
nav a[aria-current="page"] {
font-weight: 700;
text-decoration: none;
color: #1c2430;
}
import Link from "next/link";
// Shown when a page under /topics/[slug] calls notFound().
export default function TopicNotFound() {
return (
<main>
<h1>No such topic</h1>
<p>
See <Link href="/">all topics</Link>.
</p>
</main>
);
}
import { notFound } from "next/navigation";
import { getTopic } from "@/lib/topics.js";
export default async function TopicPage({ params }) {
const { slug } = await params;
const topic = await getTopic(slug);
if (!topic) notFound();
return (
<main>
<h1>{topic.title}</h1>
<p>{topic.summary}</p>
<p className="muted">
Day {topic.day} · {topic.minutes} minutes
</p>
</main>
);
}
// Pretend database of topics, looked up by their slug: the part of the URL
// that names them.
const topics = [
{ slug: "routing", title: "Routing", day: 1, minutes: 20, summary: "Folders become pages." },
{ slug: "layouts", title: "Layouts", day: 1, minutes: 20, summary: "Shared parts that wrap pages." },
{ slug: "server-components", title: "Server components", day: 2, minutes: 20, summary: "Components that run on the server." },
{ slug: "client-components", title: "Client components", day: 2, minutes: 20, summary: "Components that respond in the browser." },
{ slug: "dynamic-routes", title: "Dynamic routes", day: 3, minutes: 25, summary: "One file for many pages." },
];
const wait = () => new Promise((resolve) => setTimeout(resolve, 100));
export async function getTopics() {
await wait();
return topics;
}
export async function getTopic(slug) {
await wait();
return topics.find((topic) => topic.slug === slug) ?? null;
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
notFound() stops the page and shows a not-found page instead. Nothing
after it runs, so the rest of the page can use topic safely. On a real site
the response also gets the HTTP status 404, so search engines and other
programs know the page doesn't exist. (Day 4's streaming adds one exception,
explained there.)
The nearest not-found file
app/topics/[slug]/not-found.jsx is shown for missing topics. Next.js uses the
nearestnot-found.jsx above the page that called notFound(). So:
/topics/nope shows No such topic, from the topic folder's file;
/nope matches no route at all, so it gets the root's not-found page, here
the built-in 404:
JSX
export default function TopicNotFound() { return ( <main> <h1>No such topic</h1> <p> See <Link href="/">all topics</Link>. </p> </main> );}
Each section of a site can explain its own missing pages, with links that make
sense there.
Exercise
Previous and next
At the bottom of each topic, link to the previous and next topics in the list:
← Routing and Server components → on the Layouts page. The first topic
has no previous link, and the last has no next.
Your working spaceNext.js · 6 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Topics</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
import { getTopics } from "@/lib/topics.js";
export default async function HomePage() {
const topics = await getTopics();
return (
<main>
<h1>Topics</h1>
<ul>
{topics.map((topic) => (
<li key={topic.slug}>
<Link href={`/topics/${topic.slug}`}>{topic.title}</Link>
</li>
))}
</ul>
</main>
);
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
nav a[aria-current="page"] {
font-weight: 700;
text-decoration: none;
color: #1c2430;
}
import Link from "next/link";
// Shown when a page under /topics/[slug] calls notFound().
export default function TopicNotFound() {
return (
<main>
<h1>No such topic</h1>
<p>
See <Link href="/">all topics</Link>.
</p>
</main>
);
}
import { notFound } from "next/navigation";
import { getTopic } from "@/lib/topics.js";
export default async function TopicPage({ params }) {
const { slug } = await params;
const topic = await getTopic(slug);
if (!topic) notFound();
return (
<main>
<h1>{topic.title}</h1>
<p>{topic.summary}</p>
<p className="muted">
Day {topic.day} · {topic.minutes} minutes
</p>
</main>
);
}
// Pretend database of topics, looked up by their slug: the part of the URL
// that names them.
const topics = [
{ slug: "routing", title: "Routing", day: 1, minutes: 20, summary: "Folders become pages." },
{ slug: "layouts", title: "Layouts", day: 1, minutes: 20, summary: "Shared parts that wrap pages." },
{ slug: "server-components", title: "Server components", day: 2, minutes: 20, summary: "Components that run on the server." },
{ slug: "client-components", title: "Client components", day: 2, minutes: 20, summary: "Components that respond in the browser." },
{ slug: "dynamic-routes", title: "Dynamic routes", day: 3, minutes: 25, summary: "One file for many pages." },
];
const wait = () => new Promise((resolve) => setTimeout(resolve, 100));
export async function getTopics() {
await wait();
return topics;
}
export async function getTopic(slug) {
await wait();
return topics.find((topic) => topic.slug === slug) ?? null;
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Load all topics with getTopics() as well, and find the current one's place
by matching its stable slug, with findIndex. A real database can return
equivalent records as different objects, so reference equality is a fragile
way to find the position.
Hint 2: a first step
topics[index - 1] is undefined for the first topic. Render each link only
when there's a topic for it: {previous && <Link …>}.
Show one solution
JavaScript
import Link from "next/link";import { getTopic, getTopics } from "@/lib/topics.js";
JavaScript
if (!topic) notFound();const topics = await getTopics();const index = topics.findIndex((item) => item.slug === topic.slug);const previous = topics[index - 1];const next = topics[index + 1];