UndieGravity LogoUndiegravity

Next.js SEO: Metadata, Sitemaps, and Structured Data

Set titles and descriptions with the Next.js Metadata API, generate a sitemap and robots file, add canonical URLs and JSON-LD, and avoid common SEO mistakes.

By UndieGravity, , 2 min read

Next.js has SEO tools built in, so you rarely need a plugin. The App Router gives you a typed Metadata API, file-based sitemaps and robots rules, and full control over the HTML. This guide covers the setup most sites need.

Titles and descriptions

Export a metadata object from a page, or a generateMetadata function when the values depend on data. Set a title template once in the root layout so every page gets your brand without repeating it.

// app/layout.tsx
export const metadata = {
  title: { default: "Acme: Free Templates", template: "%s | Acme" },
  description: "Free website templates with full source code.",
};

// app/blog/[slug]/page.tsx
export async function generateMetadata({ params }: PageProps<"/blog/[slug]">) {
  const post = await getPost((await params).slug);
  return { title: post.title, description: post.description };
}
  • Keep the full title under about 60 characters so it is not cut off in results.
  • Write descriptions of 150 to 160 characters that answer what the page offers.
  • Do not repeat the brand in page titles if the template already adds it.

Canonical URLs

Set metadataBase in the root layout and an alternates.canonical per page. This tells search engines which URL is the original when the same content is reachable with tracking parameters or trailing slashes.

Sitemap and robots

Add app/sitemap.ts and app/robots.ts. Both are regular functions, so the sitemap can list every product or post from your data source.

import type { MetadataRoute } from "next";

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const posts = await getAllPosts();
  return posts.map((post) => ({
    url: `https://example.com/blog/${post.slug}`,
    lastModified: post.date,
  }));
}

Open Graph images

Social apps show the openGraph.images value when someone shares your link. Use a 1200 by 630 image. You can also add an opengraph-image.tsx file to a route to generate images from code.

Structured data

JSON-LD helps search engines and AI assistants understand what a page is: an article, a product, an organization. Render it as a script tag, and escape < so data from a CMS cannot break out of the tag.

<script
  type="application/ld+json"
  dangerouslySetInnerHTML={{ __html: JSON.stringify(data).replace(/</g, "\\u003c") }}
/>

Mistakes that cost rankings

  • Every page sharing the same title and description.
  • Important text rendered only on the client after a fetch, so it is missing from the HTML.
  • Blocking /_next/ in robots, which stops search engines from loading your CSS and JavaScript.
  • Thin pages created only to target keywords, with nothing useful on them.

Keep reading

Next.js

Next.js 16: What Changed and How to Upgrade

Next.js 16 makes Turbopack the default, renames middleware to proxy, requires async request APIs, and changes caching. Here is what to update first.

, 2 min read