Skip to content

Titles and Metadata

The Course
•
15 min study

The page's title is what the browser tab, bookmarks and search results can show. Its description is a suggested summary; search engines can choose a different snippet. Give important pages a useful title and description.

Page titles Next.js · 7 files
import Link from "next/link"; import TabTitle from "./TabTitle.jsx"; import "./globals.css"; // The site's default title and description. %s is replaced by each page's title. export const metadata = { title: { template: "%s · Study Hub", default: "Study Hub" }, description: "Plan what you study, one day at a time.", }; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <nav> <Link href="/">Home</Link> <Link href="/about">About</Link> <Link href="/topics/routing">Routing</Link> </nav> </header> {children} <footer> <TabTitle /> </footer> </body> </html> ); } export default function HomePage() { return ( <main> <h1>Study Hub</h1> <p>Look at the page title as you move around.</p> </main> ); } "use client"; import { usePathname } from "next/navigation"; import { useEffect, useState } from "react"; // Shows the browser tab's title, which the lab's preview has no tab to show. export default function TabTitle() { const pathname = usePathname(); const [title, setTitle] = useState(""); useEffect(() => { const id = setTimeout(() => setTitle(document.title), 100); return () => clearTimeout(id); }, [pathname]); return <p className="muted">Tab title: {title}</p>; } 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; } export const metadata = { title: "About", }; export default function AboutPage() { return ( <main> <h1>About</h1> <p>A place to plan what you study.</p> </main> ); } import { notFound } from "next/navigation"; import { getTopic } from "@/lib/topics.js"; // A title that depends on the data: worked out for each topic. export async function generateMetadata({ params }) { const { slug } = await params; const topic = getTopic(slug); return { title: topic ? topic.title : "Not found" }; } export default async function TopicPage({ params }) { const { slug } = await params; const topic = getTopic(slug); if (!topic) notFound(); return ( <main> <h1>{topic.title}</h1> <p className="muted">Day {topic.day}</p> </main> ); } // A pretend database of topics, in the server's memory. const topics = [ { slug: "routing", title: "Routing", day: 1 }, { slug: "server-components", title: "Server components", day: 2 }, { slug: "dynamic-routes", title: "Dynamic routes", day: 3 }, ]; export function getTopics() { return topics; } export function getTopic(slug) { return topics.find((topic) => topic.slug === slug) ?? null; } export function addTopic(topic) { topics.push(topic); }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

The preview has no browser tab, so the footer shows the tab's title. Follow About and Routing.

metadata

JavaScript
export const metadata = {
  title: { template: "%s · Study Hub", default: "Study Hub" },
  description: "Plan what you study, one day at a time.",
};

A layout or page can export metadata, and Next.js writes the <title> and <meta> tags into <head> for you. In the root layout:

  • default is the title for pages that don't set their own;
  • template wraps pages' titles: %s is replaced by the page's.

So export const metadata = { title: "About" } in the About page gives About · Study Hub. Pages inherit what they don't set, such as the description.

metadata only works in server components.

generateMetadata

A topic's title depends on its data. A page can export an async generateMetadata instead:

JavaScript
export async function generateMetadata({ params }) {
  const { slug } = await params;
  const topic = getTopic(slug);
  return { title: topic ? topic.title : "Not found" };
}

It gets the same params as the page, so /topics/routing gets Routing · Study Hub.

Exercise

Descriptions

Give each topic page a description such as Day 2 of the course: Server components.

Your working space Next.js · 7 files
import Link from "next/link"; import TabTitle from "./TabTitle.jsx"; import "./globals.css"; // The site's default title and description. %s is replaced by each page's title. export const metadata = { title: { template: "%s · Study Hub", default: "Study Hub" }, description: "Plan what you study, one day at a time.", }; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <nav> <Link href="/">Home</Link> <Link href="/about">About</Link> <Link href="/topics/routing">Routing</Link> </nav> </header> {children} <footer> <TabTitle /> </footer> </body> </html> ); } export default function HomePage() { return ( <main> <h1>Study Hub</h1> <p>Look at the page title as you move around.</p> </main> ); } "use client"; import { usePathname } from "next/navigation"; import { useEffect, useState } from "react"; // Shows the browser tab's title, which the lab's preview has no tab to show. export default function TabTitle() { const pathname = usePathname(); const [title, setTitle] = useState(""); useEffect(() => { const id = setTimeout(() => setTitle(document.title), 100); return () => clearTimeout(id); }, [pathname]); return <p className="muted">Tab title: {title}</p>; } 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; } export const metadata = { title: "About", }; export default function AboutPage() { return ( <main> <h1>About</h1> <p>A place to plan what you study.</p> </main> ); } import { notFound } from "next/navigation"; import { getTopic } from "@/lib/topics.js"; // A title that depends on the data: worked out for each topic. export async function generateMetadata({ params }) { const { slug } = await params; const topic = getTopic(slug); return { title: topic ? topic.title : "Not found" }; } export default async function TopicPage({ params }) { const { slug } = await params; const topic = getTopic(slug); if (!topic) notFound(); return ( <main> <h1>{topic.title}</h1> <p className="muted">Day {topic.day}</p> </main> ); } // A pretend database of topics, in the server's memory. const topics = [ { slug: "routing", title: "Routing", day: 1 }, { slug: "server-components", title: "Server components", day: 2 }, { slug: "dynamic-routes", title: "Dynamic routes", day: 3 }, ]; export function getTopics() { return topics; } export function getTopic(slug) { return topics.find((topic) => topic.slug === slug) ?? null; } export function addTopic(topic) { topics.push(topic); }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

A description is computed from the same topic data as the title. Handle a missing topic before reading its fields.

Hint 2: a first step

Return description as well as title from generateMetadata, when the topic exists.

Show one solution
JavaScript
export async function generateMetadata({ params }) {
  const { slug } = await params;
  const topic = getTopic(slug);
  if (!topic) return { title: "Not found" };
  return { title: topic.title, description: `Day ${topic.day} of the course: ${topic.title}.` };
}

generateMetadata gets the same params as the page, so it can look up the same topic. The !topic check comes first: without it, an unknown slug would throw on topic.day instead of reaching the not-found page.

Was this article helpful?

Your response is saved on this device.