Skip to content

Next.js Metadata and SEO: Title Templates, generateMetadata, Canonicals, robots.txt, Sitemaps

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Search engines and social apps read the <head> of your page before anything else. The <title> becomes the blue link in Google, the description often becomes the grey text under it, and Open Graph tags decide what a link looks like when someone shares it on WhatsApp or LinkedIn. In plain React you had to add these tags by hand. The Next.js App Router builds them from a metadata object or a generateMetadata function that you export from a layout or page, and it can also generate robots.txt and sitemap.xml from code. This lesson adds them to the Souq Guide app and shows the exact tags the server sent with Next.js 16.4.6.

Quick answer

In Next.js, export a metadata object from layout.tsx or page.tsx for fixed values, or an async generateMetadata function when the title depends on params or data. Set metadataBase once in the root layout, use title.template such as "%s | Site" with a default, and give each page alternates.canonical. Add app/robots.ts and app/sitemap.ts to generate /robots.txt and /sitemap.xml. Metadata works only in server components.

35 min read and practice · Content checked on 26 November 2026

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.

Example: A root layout with metadataBase and a title template
app/layout.tsx
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>
  );
}
Real output: tags in the <head> served for /shops (a page with no metadata export)
<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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.