DELTAWEB
All posts
Web Development

Next.js hreflang: Setup for Headless and App Router Sites

Generate hreflang in Next.js App Router with metadata alternates, feed it from a headless CMS, keep sitemap output consistent. Setup checklist included.

Next.js hreflang: Setup for Headless and App Router Sites
10 minReading time
Arif KuruAuthor

Picture an exporter whose German and French product pages are ready in a headless CMS, yet Google Search Console keeps flagging "no return tag" warnings. The team typed hreflang tags by hand on each page, new products never got tags, and a few French pages were never linked at all. (This scenario is a hypothetical example.)

We covered the basics of the attribute in our complete hreflang guide. This article adds the layer that matters for Next.js App Router and headless CMS architecture: where tags are generated, where the translation relationship is read from, and how the sitemap stays in step with the page markup.

Key Takeaways

  • In Next.js App Router, hreflang is produced through the alternates.languages field of the page metadata and written into the initial HTML response.
  • Every language version must list all versions in its translation group, including itself. A one-way tag produces return-tag errors.
  • Keep the translation relationship in one source of truth. Page tags, sitemap entries and HTTP headers should all read from it.
  • Do not force visitors to a language based on browser settings. Each language needs a stable, directly reachable URL.
  • After publishing, check the tags in the raw HTML and the sitemap, then watch Search Console for warnings.

Why hreflang is simple on paper but harder on a headless stack

On a traditional CMS, language versions usually sit in one plugin-managed table, and the template that renders them lives in the same system. In a headless setup, content arrives from an API, the page template lives in a separate application, and language versions can change on different release cycles. The result is that hreflang data ends up spread across several places: the translation relationship in the CMS, URL rules in the frontend, and a separate list in the sitemap.

Google describes three ways to declare hreflang: link elements in the HTML head, HTTP response headers, and XML sitemaps. All three must describe the same translation group. HTML tags work per page, HTTP headers are useful for non-HTML files such as PDFs, and sitemaps suit large sites that need bulk management. Whichever method you choose, the underlying data should come from a single source.

Setting up the language structure in Next.js App Router

The App Router does not ship a built-in i18n configuration block the way the Pages Router did. Language structure is usually built with a dynamic [locale] segment, giving paths such as /en/products/..., /de/produkte/... or /tr/urunler/.... The root layout should set the html lang attribute from the active locale, and keeping that value consistent with your hreflang codes helps search engines read the page language correctly.

Dynamic segments and slug mapping

It is common for each language to have its own slug. If the German product URL is different from the English one, matching must be done on the translation group identifier, not on the locale code alone. Otherwise the link between /en/product-a and /de/produkt-a breaks, and search engines cannot see the two pages as versions of the same content. When you change a slug, redirect the old URL permanently to the new one.

Locale detection and redirects

Request-level locale routing is configured through a file whose name depends on the Next.js version. In Next.js 16 the middleware file is named proxy.ts; confirm this against the official documentation for the version you run. The behaviour matters more than the filename. Redirecting users automatically based on the Accept-Language header can stop a crawler from ever seeing some language versions. Offer a language suggestion instead of changing the URL silently, and make sure every language page can be opened directly.

Generating hreflang with metadata alternates

In the Next.js Metadata API, the alternates.languages field writes hreflang link elements into the head of the page. Its keys are language-region codes and its values are absolute URLs of the matching pages. If the translation relationship comes from a helper function, every page can produce the same shape of output. The example below illustrates the structure only; check the exact signatures and field names against the official docs for your Next.js version.

export async function generateMetadata({ params }) {
  const { locale, slug } = await params;
  const group = await getTranslationGroup(slug, locale);
  return {
    alternates: {
      canonical: group.urls[locale],
      languages: { ...group.urls, 'x-default': group.urls.en },
    },
  };
}

Three points deserve attention. First, every URL in languages must be absolute and must include the page's own entry. Second, the canonical should always point to the page's own language version; a canonical that points to another language stops that page from being indexed in its own right. Third, the x-default value names the default page, usually the English or main-market page. Our x-default guide walks through how to choose it.

Server-side output matters

Google reads hreflang from the HTML of the page. Tags added later by client-side JavaScript can be processed late, and in some cases not processed at all. For that reason the tags should be produced on the server and appear in the first HTML response. Injecting link elements from a client component can be a fallback, but it should not be the primary source.

The quickest check is to look at the raw page source rather than the rendered DOM in developer tools. If the hreflang elements are missing from the raw source, they are most likely produced by a client component, and the implementation should be reconsidered.

Mapping translation relationships in a headless CMS

The most reliable way to keep translation relationships in a headless CMS is to give every content entry a shared translation group identifier. That identifier collects the Turkish, German and English versions of one product into a single group. When the frontend renders a page, it fetches only the published members of that group and adds each one's URL to the hreflang list.

The same logic applies on Laravel; our Laravel article shows how it is structured there. The concept is identical across platforms.

FieldPurposeRule
translation_group_idLinks all language versions of one piece of contentSame value on every version
localeLanguage-region code (e.g. tr-TR, de-DE)Follows ISO codes, one per version
pathPermanent URL in that languageMust resolve to an absolute URL
statusPublishing stateOnly published versions enter the tag list

The most important rule in this table is that an unpublished language version must be left out of the hreflang list. A tag pointing to a draft URL stops the search engine from matching the pages, and return tags start to look inconsistent. When a version goes live, the tags on its sibling pages must be updated as well.

Keeping the sitemap in sync

In Next.js, the app/sitemap.ts file lets each entry carry alternates.languages. If the sitemap and the page tags are produced by separate code, they drift apart over time: one gets updated and the other is forgotten. Both should call the same translation group function.

  • The sitemap and the page tags should be fed from one helper function.
  • Every URL in the sitemap should return a reciprocal tag in its own output.
  • A page removed from the sitemap should also disappear from the tags of its siblings.
  • Adding a new language should not require separate edits in two places.

On large sites, watch the sitemap size limits and split into a sitemap index when needed. Each part must remain internally consistent, so check that the hreflang groups are not broken across files.

HTTP headers for PDFs and other files

Product catalogues and downloadable technical sheets have no HTML head. For these files, hreflang can be declared with an HTTP Link header. In Next.js, the header can be added through the headers configuration in next.config, or inside a route handler that serves the file. The header value should include the file's own URL and the URLs of its matching versions in other languages.

Before you do this, confirm that the files really exist as genuine translations. Creating a translation group for every catalogue PDF is a significant maintenance burden when the catalogue is large. Use the method only for files that are actually translated and cover the same scope as the page they accompany.

Multiple markets, same language

If English needs regional versions such as en-US, en-GB or en-AU, each one needs its own URL and must sit inside its own translation group. Creating a regional version is worth it only when the content is genuinely different. Opening regional pages that differ only in currency or price format often creates duplicate content problems.

Which URL structure serves which market depends on architecture choices such as subdirectory versus country-code domains. Our guides cover the general framework. Country-specific legal and tax details are outside the scope of this article.

Publishing flow and caching

On headless sites there can be a delay between a content change and the rendered output. When a language version goes live, its own tag and the tags of its sibling pages must update together. Otherwise one-way tags exist for a short period. Cache lifetimes can stretch that delay. Decide as a team how content is regenerated, whether through incremental static regeneration, on-demand refresh or a short cache lifetime, and attach the post-publish check to that flow.

A practical rule: do not publish a new language version before the pages it links to are live. That way no unmatched tags exist even briefly.

Common mistakes

  1. One-way tags: the Turkish page points to the German page, but the German page does not point back. Details are in our return tag errors guide.
  2. Wrong language codes: writing only a language code where a region is needed, or mixing letter cases.
  3. Canonical versus hreflang conflicts: every language page should canonicalise to its own URL.
  4. Relative URLs: without a domain, search engines may resolve the values incorrectly.
  5. Sitemap and page mismatch: one gets updated while the other is forgotten.
  6. Unpublished versions: draft URLs appear in the tag list.
  7. Stale tags in cache: an old HTML response stays at the CDN after publishing.

Many of these errors appear not at setup but as content grows. Treat checking as an ongoing process rather than a one-time audit. For a wider list of common international SEO errors, see our international SEO guide on hreflang errors.

Managing pages without equivalents

Not every page exists in every language. If a certification page written for the German market has no Turkish counterpart, a hreflang cluster for it would mislead search engines. In that case, publish the page only in the languages where it exists, and do not link to it from other languages outside its cluster. We cover this in detail in our hreflang clusters article.

Pre-launch checklist

CheckHow to verifyExpected result
Each language page carries its tags in raw HTMLView the page sourceAll language versions listed
Return tags are completeA two-way validation toolEvery match is reciprocal
x-default is definedHTML sourceOne default URL
Sitemap and page tags agreeCompare sitemap with page outputSame URL set
PDF headersHTTP response headersLink header present

Many tools can validate hreflang, and comparing them would be a separate article. What matters here is running the check automatically on every release. Adding it to the build step stops errors from reaching production when new content arrives.

Conclusion and next step

In a Next.js headless site, hreflang comes from one source of truth for the translation relationship, not from ad hoc markup on each page. Generate the page tags with metadata alternates, build the sitemap from the same data, add HTTP headers where files need them, and verify the output after every release. As a next step, list your current language versions in a table and mark any page that is missing a version in its translation group. If you want help setting this up on your own site, the DeltaWeb team is available.

FAQ

Frequently Asked Questions

  • How do you add hreflang in Next.js App Router?

    Use the alternates.languages field in the Metadata API. Each page should list every language version in its translation group, including itself. The tags must be written into the server-rendered HTML response.

  • Does Next.js need an i18n configuration for hreflang?

    The App Router has no built-in i18n block like the Pages Router. Most projects build language structure with a [locale] segment and generate hreflang from it. Routing behaviour changes between versions, so check the official documentation for yours.

  • Will hreflang work if it is added with JavaScript?

    It may work, but it is not reliable. Google can process client-injected tags late or miss them entirely. The most dependable approach is to generate the tags on the server in the first HTML response.

  • Do you need both sitemap and page hreflang?

    Not necessarily, but if you use both they must come from the same source. Two inconsistent lists lead to return-tag errors and Search Console warnings.

  • When should x-default be used?

    It names the fallback page shown when the visitor language matches none of your versions. Usually that is the main language or English page. See the x-default guide for how to choose it.

  • What data does a headless CMS need for hreflang?

    Each entry needs a shared translation group identifier, a language-region code, a permanent URL and a publishing status. Unpublished versions must be removed from the tag list.

  • How do you give hreflang to PDF files?

    Use an HTTP Link header. The header includes the file's own URL and the URLs of its matching language versions. Use it only for files that are genuine translations.

  • How do you verify hreflang after setup?

    Check that the tags appear in the raw HTML, that every match is reciprocal, and that the sitemap and page tags list the same URL set. Adding these checks to the release process reduces errors.

Related Articles

No comments yet

Leave a Comment