Next.js canonical tags
Set canonical URLs in Next.js App Router metadata, handle dynamic routes and locales, and verify the canonical Google receives.
In the Next.js App Router, set canonical URLs with metadata.alternates.canonical. Define metadataBase in the root layout so relative URL values resolve to the production host, then provide a page-specific canonical for dynamic routes. Inspect the deployed HTML for one absolute canonical that matches redirects, internal links, and sitemap URLs. Google treats canonicals as signals and can choose another URL.
Where does your site stand?
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
- Free audit in 60 seconds
- Fixes drafted for your review
- ChatGPT, Perplexity, Gemini and AI Overviews tracked
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
- Re-check after big site changes
- Keep this page for reference
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
- Follow the steps below
- Top-traffic pages first
- Check again in a month
3 parts, 3 minutes.
Set a canonical base in the root layout
metadataBase as the base URL for metadata fields that need an absolute URL. It is typically defined in the root app/layout.js, where it applies to URL-based metadata in that route segment and below. Replace the example host with the production canonical host; a preview domain in this value can publish the wrong preference across many routes. Next.js shallowly merges metadata from parent segments, so do not set alternates.canonical in the root layout: a route that defines no alternates of its own would inherit that value and declare itself a duplicate of it.- 01An absolute canonical URL ignores
metadataBase. - 02A relative URL-based metadata field without a configured
metadataBasecan cause a build error. - 03Next.js normalizes duplicate slashes between the base and relative values. It treats a leading slash in a metadata path as relative to the base rather than replacing its path.
- 04Google recommends absolute URLs in
rel="canonical"and says relative paths, although supported, can cause problems later.metadataBasemakes the rendered tag absolute.
import type { Metadata } from "next"
export const metadata: Metadata = {
metadataBase: new URL("https://www.example.com"),
}
export default function RootLayout({ children }) {
return <html lang="en"><body>{children}</body></html>
}Generate canonical URLs for dynamic routes
params are asynchronous; Next.js 14 and earlier use synchronous params, so follow the docs for your installed version.Keep the canonical URL consistent with the URL the page actually serves after redirects. Generate it from trusted route data or a fixed base, not an incoming Host header that a request can change.
import type { Metadata } from "next"
type Props = { params: Promise<{ slug: string }> }
export async function generateMetadata({ params }: Props): Promise<Metadata> {
const { slug } = await params
const item = await getPublishedItem(slug)
return {
alternates: {
canonical: `/guides/${item.slug}`,
},
}
}Every fact, linked and dated.
nextjs.org
google.com
- 01generateMetadata | Next.js Documentationnextjs.org · checked 2026-10-24
- 02generateMetadata | Next.js 14 Documentationnextjs.org · checked 2026-10-24
- 03generateMetadata | Next.js 15 Documentationnextjs.org · checked 2026-10-24
- 04How to specify a canonical with rel=canonical and other methods | Google Search Centraldevelopers.google.com · checked 2026-10-24
- 05What is canonicalization | Google Search Centraldevelopers.google.com · checked 2026-10-24
- 06Create and submit a sitemap | Google Search Centraldevelopers.google.com · checked 2026-10-24
Asked a lot.
3 questions people ask next, answered from the sources on this page.
01How do I add a canonical URL in Next.js?
In the App Router, export metadata with alternates.canonical. Set metadataBase in the root layout if you want relative canonical paths to resolve to the production host.
02Why is Next.js generating a wrong canonical host?
Check metadataBase in the root layout and confirm it is set to the preferred production host rather than a preview or request-derived domain.
03Does a canonical tag force Google to use that URL?
No. Google treats canonical annotations as signals and may choose another URL based on redirects, internal links, sitemap entries, and page content.
Tools point. Agencies advise. This one ships.
Run the free audit, then approve the fixes. Every change can be undone.
- 46 fixes shipped without a ticket Brightpipe
- 0→7 ChatGPT citations in six weeks Paylane