Canonical URLs & Duplicate Content Prevention
Canonical URLs & Duplicate Content Prevention
Added in v1.0.376
Overview
As of v1.0.376, every page in the application declares a canonical URL. This prevents search engines from indexing the same content under multiple origins — such as www vs non-www variants, or Vercel preview deployment URLs — and ensures all link equity is consolidated to the single production domain.
How it works
Root-level canonical (all pages)
A default canonical is set in src/app/layout.tsx via Next.js metadata.alternates.canonical. This automatically applies to every page that does not declare its own override:
// src/app/layout.tsx
export const metadata: Metadata = {
alternates: {
canonical: process.env.NEXT_PUBLIC_APP_URL,
},
};
The value is read from the NEXT_PUBLIC_APP_URL environment variable, which must be set to your production origin (e.g. https://www.yourdomain.com).
Per-page canonical overrides
Pages with distinct URL paths export their own metadata.alternates.canonical to produce a precise canonical tag:
// src/app/pricing/page.tsx
export const metadata: Metadata = {
alternates: {
canonical: `${process.env.NEXT_PUBLIC_APP_URL}/pricing`,
},
};
This ensures the rendered <link rel="canonical" href="..."> tag in each page's <head> points to the exact canonical path, not just the root.
Vercel preview deployment suppression
A X-Robots-Tag: noindex response header is added to Vercel preview deployments via vercel.json using environment-conditional header rules. This prevents preview URLs (e.g. https://my-app-git-feature-org.vercel.app) from being crawled or indexed by search engines while leaving production unaffected.
Environment variable
| Variable | Required | Description |
|---|---|---|
NEXT_PUBLIC_APP_URL | Yes | The canonical production origin. Example: https://www.yourdomain.com. Do not include a trailing slash. |
Note: If
NEXT_PUBLIC_APP_URLis not set, the canonical tag will render asundefinedand provide no benefit. Always set this variable in your production and staging environment configurations.
Deployment checklist
-
NEXT_PUBLIC_APP_URLis set in Vercel production environment variables -
NEXT_PUBLIC_APP_URLuses the exact preferred domain (including or excludingwww) that you want search engines to index - Vercel preview environments do not have
NEXT_PUBLIC_APP_URLset to the production domain - Any new top-level page routes include a
metadata.alternates.canonicalexport pointing to their full URL
Background
This work addresses SEO control SEO-05, which identified that no metadata.alternates.canonical was configured on any page. The risk was that Vercel preview deployment URLs could be indexed as duplicate content and that www vs non-www variants could split ranking signals. Canonical tags are the standard mechanism recommended by Google and other search engines to disambiguate the preferred URL for a piece of content.