Next.js SEO for marketing sites: metadata, sitemaps and schema
Set up Next.js SEO the right way: title templates, canonicals, sitemaps, robots.txt, share images and JSON-LD, plus the merge rules that trip teams up.
Next.js has SEO tooling built in. In the App Router you set titles, descriptions, canonicals and social tags with the Metadata API, and you generate sitemap.xml, robots.txt and share images from special files in the app folder. You don't need a separate SEO package for the basics. What you do need is a sensible setup, because a few defaults (shallow merging in particular) catch out a lot of marketing sites.
This guide covers the setup we'd recommend for a content-led Next.js site at the time of writing (November 2026), in the order you'd build it. Here is the whole setup at a glance:
| What | Where | Note |
|---|---|---|
| Title template, metadataBase | app/layout.tsx |
default required with a template |
| Page title, description, canonical | metadata or generateMetadata |
Shallow merge: spread shared objects |
| Sitemap | app/sitemap.ts |
Accurate lastmod; 50,000 URL limit |
| Robots | app/robots.ts |
Point to the sitemap |
| Share images | opengraph-image.png or .tsx |
8MB limit for Open Graph |
| Structured data | JSON-LD script in page | Escape < |
1. Set site-wide defaults in the root layout
Start in app/layout.tsx by exporting a metadata object with three things:
-
A title template.
title: { template: '%s | Brand', default: 'Brand' }adds your brand to every child page title. The docs note thatdefaultis required when you set a template, and the template only applies to child segments, not the layout it's defined in. -
metadataBase. Set it to your production URL. URL-based fields such as canonicals and Open Graph images can then use relative paths. Using a relative path withoutmetadataBasecauses a build error. -
A default description and Open Graph settings such as
siteName,localeandtype.
Use title.absolute on any page that should ignore the template, such as the home page.
2. Add page-level metadata, and watch the merge rules
Static pages can export a metadata object. Pages built from a CMS or database should export generateMetadata, which receives route params and can fetch the same data as the page. The docs say fetch requests are memoised across generateMetadata, layouts and pages, so you don't pay twice for the same request. Both exports only work in Server Components, and you can't export both from the same segment.
The common mistake is merging. Metadata from each segment is merged shallowly, so if a blog post sets openGraph: { title: '...' }, it replaces the whole openGraph object from the layout, including your default share image. The fix the docs suggest is to keep shared fields in a separate module and spread them in, for example openGraph: { ...sharedOpenGraph, title: post.title }.
3. Get canonicals and hreflang right
Set alternates.canonical on each page, ideally from the same slug you use for routing. Be careful about putting a canonical in a layout: fields a child segment doesn't set are inherited, so a canonical of '/' in the root layout can end up on every page that forgets to set its own. That tells search engines your inner pages are copies of the home page.
For multilingual sites, alternates.languages outputs hreflang links, for example { 'en-GB': '/en-gb', 'de-DE': '/de-de' }. Keep these consistent with the language alternates in your sitemap.
4. Generate the sitemap and robots.txt from code
An app/sitemap.ts file returns an array of URLs and Next.js serves it as /sitemap.xml. It is cached by default unless it uses request-time APIs, so it typically gets built once per deployment. A few points from Google's own documentation are worth knowing:
-
Google says it ignores
priorityandchangefreq, so don't spend time tuning them. -
Google uses
lastmodif it is consistently and verifiably accurate. Feed it the real "last significant update" date from your CMS, notnew Date()on every build. -
A single sitemap is limited to 50,000 URLs or 50MB uncompressed. Larger sites can split sitemaps with
generateSitemaps.
An app/robots.ts file returns rules per user agent plus your sitemap URL. Since Next.js 16.3 a rule can also carry non-standard directives through an other field. For staging environments, setting robots: { index: false } in metadata (driven by an environment variable) is a simple way to keep preview deployments out of search results.
5. Make share images automatic
Drop an opengraph-image.png (and optionally opengraph-image.alt.txt) into any route folder and Next.js adds the right tags, including width and height. The docs warn that the build fails if an Open Graph image is over 8MB or a twitter-image is over 5MB.
For blogs and case studies, an opengraph-image.tsx file can generate a branded image per page using ImageResponse from next/og, typically at 1200 by 630 pixels with the post title on it. Generated images are statically optimised by default, so they're created at build time rather than on every share.
6. Add structured data with JSON-LD
The Next.js docs recommend rendering JSON-LD as a plain <script type="application/ld+json"> tag in your page or layout, not with next/script. Because JSON.stringify doesn't sanitise strings, they suggest replacing < with in the output to avoid script injection from CMS content. Typical types for a marketing site are Organization, Article or BlogPosting, BreadcrumbList and FAQPage. Test with Google's Rich Results Test or the Schema Markup Validator, and consider the schema-dts package for TypeScript types.
7. Understand streaming metadata
Since Next.js 15.2, generateMetadata can stream: the page starts rendering before metadata resolves, and the tags are appended to the <body>. The Next.js team says it has verified that bots which execute JavaScript, such as Googlebot, read this correctly. For "HTML-limited" bots such as facebookexternalhit, Next.js detects the user agent and still blocks until metadata is ready, so tags land in the <head>. You can change that list with the htmlLimitedBots option, though the docs say the default suits most sites.
If your project uses Cache Components, metadata that reads cookies, headers or uncached data is deferred to request time, and Next.js raises an error if the rest of the page could be fully prerendered. Adding 'use cache' inside generateMetadata is the usual fix when metadata comes from a CMS rather than the request. Our Next.js 16.4 Cache Components guide covers that model in more detail.
Frequently asked questions
Do I still need an SEO package like next-seo?
Not for App Router projects in most cases. The built-in Metadata API and file conventions cover titles, canonicals, social tags, sitemaps and robots.
Is metadata in the HTML that crawlers see?
If the page can be prerendered and generateMetadata isn't dynamic, metadata is included in the initial HTML. Dynamic metadata may stream into the body for JavaScript-capable bots, and is still placed in the head for HTML-limited bots.
Where should the canonical tag live?
On each page, set from its own slug. Avoid a fixed canonical in a shared layout, because child pages inherit it if they don't override it.
Need help with Next.js SEO?
We build and audit Next.js marketing sites, including metadata, sitemaps and structured data, through our Next.js development service. For related reading, see how to get your business cited in AI search answers and React vs Next.js. If your site needs a technical SEO review, contact us.
Sources: generateMetadata (Next.js docs), sitemap.xml (Next.js docs), robots.txt (Next.js docs), opengraph-image (Next.js docs), JSON-LD guide (Next.js docs), Build and submit a sitemap (Google Search Central)
(over to you)
Planning a build like this?
Tell us what you're building and we'll come back with ideas and next steps.
Keep reading
All articles
React & Next.js
· 8 min read
Next.js multi-language website: i18n routing and RTL done right
Read article
React & Next.js
· 6 min read
Next.js 16.4: Cache Components by default and how to upgrade
Read article
React & Next.js
· 6 min read
Next.js security releases: October fixes and the 3 November patch
Read article