Next.js App Router: a canonical in the root layout canonicalises every page to the homepage

In the Next.js App Router, metadata merges shallowly and inner pages inherit the root layout's alternates.canonical. One canonical set in app/layout.tsx tells search engines every page is a copy of the homepage. Set a relative, self-referencing canonical per page, and fix OG, 404 robots and sitemap dates the same way.

By Nishant Gauraw, NG-NeXTPublished Updated 4 min readExpert

TL;DR#

In the Next.js App Router, a page that does not define alternates inherits the root layout's alternates, including canonical. Put canonical: 'https://example.com' in app/layout.tsx and every page now declares itself a duplicate of the homepage. Remove it from the layout, set a relative self-canonical ('/about') on each page against metadataBase, and apply the same per-page discipline to openGraph, the 404 page's robots, and sitemap lastmod.

Symptoms#

  • Every page's HTML contains the same <link rel="canonical" href="https://example.com"/>.
  • Search Console reports inner pages as "Alternate page with proper canonical tag" or "Duplicate, Google chose different canonical", and only the homepage gets indexed.
  • Link previews for any page show the homepage title and URL, because og:url and og:title are identical everywhere.
  • The 404 response contains two robots tags (noindex and index, follow) and two <title> elements.
  • The sitemap claims every URL changed at the time of the last deploy.

Environment#

  • Next.js 13.4 or newer with the App Router (verified on 15.5), React 18 or 19.
  • Metadata defined with the metadata export or generateMetadata, metadataBase set in the root layout.
  • Any host or container. The behaviour is in the framework, not the server.

Root cause#

The Metadata API resolves metadata from the root layout down to the page and merges it shallowly by top-level key. For each key (title, alternates, openGraph, robots, ...), the deepest segment that defines it wins, and it wins with its whole object. A key that a page does not define is inherited unchanged from the nearest parent that does.

So this root layout:

// app/layout.tsx: BROKEN
export const metadata: Metadata = {
  metadataBase: new URL('https://example.com'),
  title: { default: 'Example', template: '%s · Example' },
  alternates: { canonical: 'https://example.com' },
  openGraph: { url: 'https://example.com', title: 'Example', images: ['/og.png'] },
  robots: { index: true, follow: true }
};

combined with pages that only set title and description produces, on /about, a correct title but the homepage's canonical, og:url and og:title. Nothing warns you: the output is valid HTML, just wrong.

The same mechanism explains the 404 page. For not-found responses Next adds <meta name="robots" content="noindex"/> itself, and the layout's robots is still rendered next to it. Without a custom app/not-found.tsx, the built-in not-found component also renders its own <title>404: This page could not be found.</title> inside the body, while the layout's default title is rendered in the head, so the response has two titles.

Finally, the stock app/sitemap.ts example uses lastModified: new Date(), which is evaluated at build time for a static sitemap. Every deploy stamps every URL as changed.

Fix#

  1. Strip page-specific keys from the root layout. Keep metadataBase, the title template and site-wide defaults. Delete alternates.canonical, openGraph.url and robots:

    // app/layout.tsx
    export const metadata: Metadata = {
      metadataBase: new URL('https://example.com'),
      title: { default: 'Example: what we do', template: '%s · Example' },
      description: 'Site-wide fallback description.',
      openGraph: { siteName: 'Example', type: 'website', locale: 'en_US' }
    };
    
  2. Give every page complete, self-referencing metadata through one helper, so a page cannot forget a key:

    // lib/seo.ts
    import type { Metadata } from 'next';
    
    export function pageMetadata(path: string, title: string, description: string, index = true): Metadata {
      return {
        title,
        description,
        alternates: { canonical: path }, // relative, resolved against metadataBase
        openGraph: { title: `${title} · Example`, description, url: path, siteName: 'Example', type: 'website' },
        twitter: { card: 'summary_large_image', title: `${title} · Example`, description },
        robots: { index, follow: true }
      };
    }
    
    // app/about/page.tsx
    export const metadata = pageMetadata('/about', 'About us', 'Who we are and how we work.');
    

    For dynamic routes, call the same helper from generateMetadata with the resolved slug.

  3. Use file-based Open Graph images. An opengraph-image.tsx in a route segment is resolved separately from the openGraph object, so a page can override openGraph without losing its image, and each page (or post) can have its own generated card.

  4. Add a real app/not-found.tsx. Give it a metadata export with a title, and do not render a <title> element inside it. With robots gone from the layout, the only robots tag is the framework's noindex.

    // app/not-found.tsx
    import type { Metadata } from 'next';
    export const metadata: Metadata = { title: 'Page not found' };
    export default function NotFound() {
      return <main><h1>This page does not exist.</h1></main>;
    }
    
  5. Store real content dates for the sitemap. Keep a small registry of pages with the date their content last changed, bump it in the same commit as a content change, and drop noindex pages from the sitemap entirely:

    // lib/pages.ts
    export const PAGES = [
      { path: '/', lastModified: '2026-11-15', index: true },
      { path: '/about', lastModified: '2026-04-24', index: true },
      { path: '/legal/terms', lastModified: '2026-04-24', index: false }
    ];
    

    Generate sitemap.xml from PAGES.filter((p) => p.index) using lastModified as lastmod. If you split the sitemap into an index plus child sitemaps, route handlers with export const dynamic = 'force-static' keep each file static.

Verification#

Run against the production build (next build && next start, or the real container), not next dev:

BASE=http://127.0.0.1:3000
for p in / /about /contact /blog/some-post; do
  html=$(curl -s "$BASE$p")
  printf '%-20s %s | %s\n' "$p" \
    "$(grep -o '<link rel="canonical" href="[^"]*"' <<<"$html")" \
    "$(grep -o '<meta property="og:url" content="[^"]*"' <<<"$html")"
done

# 404: status 404, exactly one robots tag, exactly one title.
curl -s -o /tmp/404.html -w '%{http_code}\n' "$BASE/definitely-not-a-page"
grep -o '<meta name="robots"[^>]*>' /tmp/404.html | wc -l    # expect 1
grep -o '<title>' /tmp/404.html | wc -l                      # expect 1

# Sitemap dates must not all equal today.
curl -s "$BASE/sitemap.xml" | grep -o '<lastmod>[^<]*' | sort | uniq -c

Every canonical should equal its own page URL, every og:url should differ, and the 404 checks should both print 1.

Pitfalls#

  • Setting openGraph on a page drops the layout's OG images. It is the same shallow merge. Use a helper that always sets the full object, or file-based images.
  • Absolute canonicals built from an environment variable. A missing variable in one environment yields undefined/about or a staging hostname in production HTML. A relative canonical plus one metadataBase has a single point of failure that is easy to test.
  • Testing in next dev. Development mode renders metadata on demand and can differ in small ways from the static output. Verify the build you ship.
  • Hiding the main heading until hydration. An <h1> server-rendered at opacity: 0 by an animation library is not painted until JavaScript runs, which delays Largest Contentful Paint. Use a CSS entrance animation or render it visible.
  • Canonical pointing at a redirect. If /about/ redirects to /about, the canonical must be /about.

FAQ#

Is a relative canonical like '/about' safe in Next.js?#

Yes, as long as the root layout sets metadataBase. Next resolves relative metadata URLs against it and renders an absolute https:// URL in the HTML.

If a page sets openGraph.title, does it keep the layout's og:image?#

No. Metadata is merged per top-level key, so a page that defines openGraph replaces the whole parent openGraph object, images included. Either repeat the images in a shared helper or use a file-based opengraph-image, which is resolved separately.

Why does my 404 page have two robots meta tags?#

Next adds noindex to not-found responses itself. If the root layout also sets robots: { index: true }, that tag is rendered as well. Remove robots from the root layout and set it per page.

Does Google use lastmod in sitemaps?#

Google uses lastmod when it is consistently accurate and ignores it when it is not. A build-time new Date() on every URL is the classic way to make it inaccurate. Google ignores changefreq and priority.

Should the homepage canonical have a trailing slash?#

Pick one form and keep it consistent with what the server returns without a redirect. With metadataBase set, canonical: '/' renders the bare origin, which matches a site that serves the homepage at its origin.

References#

Written from production work at NG-NeXT.