In this lesson you will
- Set site wide metadata and a title template in the root layout
- Write generateMetadata for dynamic pages from their params
- Add canonical URLs and Open Graph tags that resolve to full links
- Generate robots.txt and sitemap.xml from code
Site wide metadata in the root layout
Export a metadata object typed as Metadata from next in app/layout.tsx. Three fields do most of the work. metadataBase is your real domain, so every relative URL in metadata, such as /shops/dubai, becomes a full link; canonical and Open Graph URLs must be absolute, and this saves you typing the domain everywhere. title.template wraps the titles of child pages, so a page that sets "Shops in Dubai" gets "Shops in Dubai | Souq Guide". title.default is used by pages that set no title, and the template needs it.
The recording shows the catch. /shops exports no metadata of its own, so it got the default title "Souq Guide" and the site description. If ten pages do that, Google sees ten pages with the same title and description, and has to guess which one to show. The template applies only to child segments, never to the layout that defines it, and Next.js always adds the charset and viewport tags, so you never write those yourself.
import type { Metadata } from "next";
import Link from "next/link";
export const metadata: Metadata = {
metadataBase: new URL("https://souq-guide.example"),
title: { template: "%s | Souq Guide", default: "Souq Guide" },
description: "Opening hours and locations of traditional markets in the UAE.",
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<nav>
<Link href="/">Home</Link> | <Link href="/shops">Shops</Link>
</nav>
<main>{children}</main>
</body>
</html>
);
}<title>Souq Guide</title>
<meta name="description" content="Opening hours and locations of traditional markets in the UAE."/>souq-guide.example is a reserved example domain; put your own domain in metadataBase.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Do I still need next-seo or react-helmet with the App Router?
No. The built in metadata API covers titles, descriptions, canonical and language alternates, Open Graph, Twitter cards, robots and icons. Those libraries were made for the Pages Router or plain React, where no such API existed.
How long should a page title and description be?
There is no fixed limit, but Google cuts what does not fit the result, roughly 50 to 60 characters for titles on desktop. Put the important words first, keep each title unique, and write descriptions of about one or two sentences that match the page.
Should I put the site name in every title?
Usually yes, after the page topic, which is what title.template does. For the home page set title.absolute if you want a title that skips the template.
Does Next.js submit my sitemap to Google?
No. It only serves /sitemap.xml. List it in robots.txt, as the example does, and submit it once in Google Search Console so you can see which URLs were indexed.
Can generateMetadata fetch data from a database?
Yes. It is an async server function, so it can await a query or fetch. Share the loader with the page through React cache or "use cache" so the data is loaded once, not once for the tags and again for the page.
Sources
Facts in this lesson were checked against these official pages on 26 November 2026.
- Next.js docs: Metadata and OG images
- Next.js docs: generateMetadata
- Next.js docs: sitemap.xml
- Google Search Central: robots.txt introduction
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.