Alle Artikel
13. November 2026

Hreflang and canonicals for a bilingual Next.js site, 2026

Set hreflang in Next.js with generateMetadata: a self canonical per language, return links, x-default and a sitemap option, with code.

Two colleagues review a bilingual website on a shared laptop, checking hreflang settings together

The short answer

On a bilingual Next.js site, every page needs two things in its head: a self-referencing canonical in its own language, and a set of hreflang alternates that lists every language version, itself included. In the App Router, one alternates object in generateMetadata produces both. Below: the rules behind that object, the code, the sitemap alternative and a short pre-launch check. The rules below come from Google Search Central and the Next.js docs, as of November 2026.

What hreflang has to do

Hreflang tells Google that two URLs are the same page in different languages. Google's localized versions guide sets a few hard requirements.

Each language version must list itself as well as all other language versions. For an English and Spanish site, that means the English page carries an en entry and an es entry, and the Spanish page carries the same two entries. The set is identical on every version.

Return links are the rule teams break most. Google puts it this way: "If page X links to page Y, page Y must link back to page X." If the pages don't point at each other, the annotations may be ignored or not interpreted correctly. The usual cause is a page that lists a translation which doesn't list it back.

Full URLs and language codes

Alternate URLs must be fully qualified, with the scheme. Google's example: https://example.com/foo, not //example.com/foo or /foo. Use HTTPS everywhere. The canonical guide also says not to put the HTTP version of your pages in your sitemap or hreflang annotations.

Language codes use ISO 639-1, optionally followed by an ISO 3166-1 Alpha 2 region. You can't specify a country code by itself, and the value is case-insensitive. For a site with one Spanish version, plain es is enough. Add a region such as es-MX only when you really serve different pages to different Spanish-speaking markets.

x-default

x-default is the fallback for users whose language matches none of your versions. Google says it was designed for language selector pages and works best there. A bilingual site without a selector page commonly points it at the default-language URL. It is optional.

One canonical per language

Hreflang and canonical solve different problems. Canonical picks the preferred URL among duplicates. Hreflang connects translations. Mix them up and a Spanish page can drop out of the index.

Google's rule for sites that use both: "If you're using hreflang elements, make sure to specify a canonical page in the same language, or the best possible substitute language if a canonical page doesn't exist for the same language." So the Spanish page canonicalizes to itself. It never canonicalizes to the English page.

Three more points from the same guide:

  • Put a self-referential rel="canonical" on the canonical page itself.
  • rel="canonical" annotations with hreflang, lang, media and type attributes are not used for canonicalization. Keep hreflang in separate rel="alternate" links.
  • Don't send conflicting signals, for example one URL in the sitemap and a different one in rel="canonical".

Google also notes that it prefers URLs that are part of hreflang clusters when it picks canonicals. A complete, reciprocal set of alternates helps the pages you want indexed.

Set hreflang and canonicals with generateMetadata

The Next.js generateMetadata reference documents an alternates field with canonical and languages. It renders <link rel="canonical"> and one <link rel="alternate" hreflang="..."> per language.

Set metadataBase once in the root layout. The docs say a relative path in a URL-based metadata field without metadataBase causes a build error, and that an absolute URL overrides metadataBase. With it set, relative paths resolve to the full URLs Google requires.

// app/layout.tsx
export const metadata = {
  metadataBase: new URL('https://example.com'),
}

Then build the alternates per page. This sketch assumes routes under app/[lang]/blog/[slug]/page.tsx, with en and es as the locales.

// app/[lang]/blog/[slug]/page.tsx
import type { Metadata } from 'next'

export async function generateMetadata({
  params,
}: {
  params: Promise<{ lang: 'en' | 'es'; slug: string }>
}): Promise<Metadata> {
  const { lang, slug } = await params
  const path = `/blog/${slug}`

  return {
    alternates: {
      canonical: `/${lang}${path}`,
      languages: {
        en: `/en${path}`,
        es: `/es${path}`,
        'x-default': `/en${path}`,
      },
    },
  }
}

A few notes on this code:

  • The canonical is built from the current lang, so each language page canonicalizes to itself.
  • Every page returns the same languages map, which gives you the self-listing and return links Google asks for.
  • The x-default key is our suggestion. The docs example shows only locale keys, so confirm in the rendered HTML that it comes out as hreflang="x-default".
  • If the translated slug differs from the English slug, look up each language's own slug and build the map from that. Reusing one slug for both languages only works when the slugs match.
  • Metadata exports are Server Component only, and you can't export both metadata and generateMetadata from the same segment.

Titles, Open Graph and the rest of the metadata object follow the same pattern. Our guide to generateMetadata for titles, canonicals and Open Graph covers them.

The sitemap alternative

Google treats three methods as equivalent: HTML link elements, HTTP headers and sitemap entries. It says there is no benefit in using more than one at the same time. If you already emit alternates in the head, you don't need to repeat them in the sitemap. If you would rather keep the head light, declare them in the sitemap instead.

Node map diagram with three parts: HTML link elements, HTTP headers and sitemap entries
Node map: The sitemap alternative.

Next.js supports this in app/sitemap.ts. The alternates.languages option was added in v14.2.0 and renders xhtml:link entries.

// app/sitemap.ts
import type { MetadataRoute } from 'next'

export default function sitemap(): MetadataRoute.Sitemap {
  return [
    {
      url: 'https://example.com/en/blog/my-post',
      lastModified: new Date('2026-11-01'),
      alternates: {
        languages: {
          en: 'https://example.com/en/blog/my-post',
          es: 'https://example.com/es/blog/my-post',
        },
      },
    },
  ]
}

Note that the Next.js docs example lists only the other languages. Google's guideline says each <url> must list every version, including itself, so add the self entry yourself, as above. The docs don't say whether Next.js adds it for you. Check the generated /sitemap.xml. Each language version also needs its own <url> entry with the same set.

Check your work before and after launch

Run this on a few real URLs in both languages, using view-source or curl:

  1. The page has one canonical, it points to itself, and it uses HTTPS.
  2. The page lists an alternate for each language, including itself, with full URLs.
  3. The Spanish URL lists the English one, and the English URL lists the Spanish one.
  4. If you also use a sitemap, it agrees with the tags: the same URLs, with no conflicting canonical.
  5. No alternate returns a redirect or an error.

If you are moving an existing site to a new structure, fold these checks into your site migration SEO checklist. Redirects and canonicals interact, and a locale prefix change is a migration.

Conclusion

Hreflang is bookkeeping. Each language page names itself, names its translations and gets named back. Canonicals stay inside each language. In Next.js, one alternates object in generateMetadata handles both, and metadataBase keeps the URLs absolute. Once the setup works, add the check to your release routine, and revisit it whenever a page is renamed or a language is added, as part of your content refresh strategy. If you want help with a multilingual build, our web development service covers it.

Frequently asked questions

Do I need hreflang if my site has only English and Spanish?

Yes, if both languages cover the same pages at separate URLs. Each page lists itself and its translation so Google can pair them. Both sides need return links, or the annotations may be ignored.

Should the Spanish page's canonical point to the English page?

No. Google says that when you use hreflang, the canonical should be a page in the same language. Give each language page a self-referential canonical.

Do I need x-default?

No, it is optional. It is the fallback for users whose language matches none of your versions, and Google says it works best on a language selector page. A bilingual site often points it at the default-language page.

Should I use HTML tags or a sitemap for hreflang?

Pick one. Google treats HTML, HTTP headers and sitemap entries as equivalent and says there is no benefit in using all of them at once. In Next.js, alternates in generateMetadata covers the HTML route, and app/sitemap.ts covers the sitemap route.