How to set up metadata and Open Graph in Next.js

Set metadataBase and a title template in the root layout, export metadata or generateMetadata per page, and set a canonical on every page. With code.

By Sohail Hussain21 November 20265 min read

To set up metadata in Next.js, export a metadata object from your root app/layout.tsx with metadataBase, a title template and site-wide Open Graph defaults, then export metadata from each static page and a generateMetadata function from each dynamic route. Give every page its own title, description and canonical URL. Next.js turns all of it into tags in the HTML head, and file-based images such as opengraph-image.tsx fill in the share image.

This guide uses the App Router in Next.js 16, current as of October 2026, and is the setup we use on every site in our website design and development work. If you are on the Pages Router, the equivalent is next/head, and App Router vs Pages Router covers the difference.

Step 1: What belongs in the root layout?

Everything that is the same on every page, plus the base URL:

tsx
// app/layout.tsx
import type { Metadata } from "next";

export const metadata: Metadata = {
  metadataBase: new URL("https://www.example.com"),
  title: {
    default: "Acme Engineering",
    template: "%s | Acme Engineering",
  },
  description: "Pumps and valves for water treatment plants, designed and built in Pune.",
  openGraph: {
    type: "website",
    siteName: "Acme Engineering",
    locale: "en_IN",
  },
  twitter: { card: "summary_large_image" },
};

metadataBase lets every other URL field be a relative path. Without it, a relative canonical or image URL fails the build.

title.template wraps the titles of child pages, so a page with title: "Services" renders as "Services | Acme Engineering". The template does not apply to the layout's own segment, which is why default is required: it is the title for the homepage and any page that sets none.

Do not put a canonical URL in the root layout. Metadata merges shallowly, and any page that does not set its own alternates inherits the layout's, so every page would declare the homepage as its canonical. This is one of the easiest ways to drop a whole site out of search.

Step 2: How do you add metadata to a static page?

Export a metadata object with a unique title, description and canonical:

tsx
// app/services/page.tsx
import type { Metadata } from "next";

export const metadata: Metadata = {
  title: "Services",
  description: "Design, manufacture and servicing of industrial pumps and valves, with on-site support across Maharashtra.",
  alternates: { canonical: "/services" },
};

You do not need to repeat the title in openGraph. When the layout defines openGraph and the page does not set openGraph.title or openGraph.description, Next.js fills them from the page's title and description, and the Twitter tags fall back to the Open Graph values.

Keep titles under about 60 characters and descriptions between 120 and 155, so Google shows them without truncation. The meta length checker shows where yours would cut off.

The Metadata type has dozens of fields. Most sites need only a handful:

FieldRenders asWorth setting?
title<title>Always, unique per page
descriptionmeta name="description"Always; Google may rewrite it, but often uses it
alternates.canonicallink rel="canonical"On every indexable page
openGraphog: tagsYes; controls previews on LinkedIn, WhatsApp and Slack
twitter.cardtwitter:cardOnce, in the root layout
robotsmeta name="robots"Only on pages to keep out of search
alternates.languageslink rel="alternate" hreflangMultilingual sites only
keywordsmeta name="keywords"No; Google does not use it

Skip the rest unless you have a specific reason. Every extra field is something to keep correct.

Step 3: How do you generate metadata for dynamic pages?

Export generateMetadata from the dynamic route. In Next.js 16, params is a promise, so await it:

tsx
// app/blog/[slug]/page.tsx
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { getPost } from "@/lib/posts";

export async function generateMetadata({ params }: PageProps<"/blog/[slug]">): Promise<Metadata> {
  const { slug } = await params;
  const post = await getPost(slug);
  if (!post) return {};
  return {
    title: post.title,
    description: post.description,
    alternates: { canonical: `/blog/${slug}` },
    openGraph: {
      type: "article",
      siteName: "Acme Engineering",
      publishedTime: post.date,
    },
  };
}

export default async function Page({ params }: PageProps<"/blog/[slug]">) {
  const { slug } = await params;
  const post = await getPost(slug);
  if (!post) notFound();
  return <article>{/* ... */}</article>;
}

PageProps is a global type helper that Next.js generates from your routes, so slug is typed for you. Both functions call getPost, so make it a cached function (with "use cache" or React's cache) or a fetch, which Next.js deduplicates. The page then makes one request, not two.

This blog does the same thing, and also returns robots: { index: false, follow: false } for posts still marked as drafts, so a draft that leaks into a preview is never indexed.

Step 4: Why did my Open Graph image or site name disappear?

Because the page set its own openGraph. The merge is shallow: the page's openGraph object replaces the layout's entirely, it does not add to it. In the example above, the post sets siteName again for exactly this reason.

The clean fix is a shared defaults object:

ts
// lib/metadata.ts
export const sharedOpenGraph = {
  siteName: "Acme Engineering",
  locale: "en_IN",
};

Spread it into every page that sets Open Graph fields: openGraph: { ...sharedOpenGraph, type: "article" }. The same trap applies to twitter. If the root layout sets twitter.title and twitter.description, every page that does not set its own twitter object shows the homepage title and description on X and in other apps that read Twitter cards. Set only card in the layout and let the rest fall back to the Open Graph values. For the image itself, prefer the file convention: an opengraph-image.tsx or opengraph-image.png in the route folder. File-based metadata takes priority over the metadata object, and Next.js adds the width, height, type and alt tags for you. Generating Open Graph images in Next.js shows how to draw one per page from its title.

To see what a full set of tags should look like, build one with our Open Graph and Twitter card generator and compare it with your page source.

Step 5: How do you stop pages from being indexed?

Set robots on the pages that should stay out of search: thank-you pages, internal search results, drafts and gated content.

ts
export const metadata: Metadata = {
  title: "Thank you",
  robots: { index: false, follow: true },
};

This renders <meta name="robots" content="noindex, follow">. Do not block these pages in robots.txt as well; if Google cannot crawl a page, it cannot see the noindex. Sitemaps and robots.txt in Next.js covers the robots file.

Step 6: How do you check the result?

View the page source, not the element inspector, and look for the <title>, meta name="description", link rel="canonical" and og: tags. For pages that are pre-rendered, all of them are in the head of the initial HTML. For pages rendered on request, Next.js can stream metadata after the first bytes for browsers and crawlers that run JavaScript, while keeping it in the head for crawlers that do not, such as social media preview bots. The generateMetadata reference explains the detection and how to change it.

Then paste a URL into LinkedIn's Post Inspector or Facebook's Sharing Debugger to see the preview a real share would produce.

Next.js for SEO lists the other checks to run after launch, including structured data, and why we build company websites with Next.js covers why we prefer this setup to a plugin.

Was this guide useful?

Questions

Frequently asked.

Can I set metadata in a client component?

No. The metadata object and generateMetadata only work in server components, because Next.js resolves them on the server to put the tags in the initial HTML. Keep page.tsx as a server component that exports the metadata, and move the interactive parts into a separate client component it renders.

Where do I set the theme colour and viewport?

In a separate viewport export, or generateViewport, from the same layout or page. The themeColor, colorScheme and viewport fields inside metadata were deprecated in Next.js 14. Next.js adds a sensible viewport meta tag by default, so most sites only need themeColor.

Sohail Hussain

Founder, MSH Technologies

Sohail has designed and built websites, web apps and AI products for over 12 years, for clients from Singapore-based industrial groups to early-stage SaaS startups. He writes about what actually worked.

Keep reading

Related guides.

All posts

Your next move

Like what you see? Tell us about it.

A process that takes too long, a website that undersells you, or an idea ready to take shape. Tell us where you are and we will suggest a useful next step.

India · Working with clients worldwide

Sohail, Founder, replies within 1 working day (Mon–Sat, IST). Free and no obligation.