Skip to content

feat: render hreflang alternates in page head for translated content - #1907

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
swissky:feat/hreflang-alternates
Jul 10, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
swissky:feat/hreflang-alternates

Conversation

@swissky

@swissky swissky commented Jul 10, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Implements render-path hreflang alternates for translated content, per the acceptance criteria in #1690. Multilingual SEO previously stopped at the sitemap — translation siblings were cross-linked there, but rendered pages carried no hreflang annotations in their <head>.

Closes #1690

What's added:

  • getHreflangAlternates(collection, entryId, { siteUrl }) — a public helper (exported from emdash and emdash/seo, with a *WithDb variant for callers that already hold a database handle) that resolves the alternate set for a content entry. Result resolution is request-cached.
  • Automatic emission in <EmDashHead> — when i18n is enabled and the page context carries a content reference, it emits one <link rel="alternate" hreflang="..."> per variant plus x-default. The links join the contribution pipeline at the base layer, so plugins can override individual hreflang entries (the existing dedup key is the hreflang value). Non-i18n sites skip this entirely — zero extra queries.

Semantics deliberately mirror the sitemap route so head and sitemap always agree:

  • One alternate per published translation sibling, including a self-referencing entry (per Google's recommendation); drafts are excluded.
  • x-default points at the default-locale variant, falling back to the first routable variant when the default locale has no translation.
  • Variants whose locale isn't in the configured i18n.locales are dropped — linking search engines to an unservable route is worse than no link.
  • Untranslated entries still get self + x-default when i18n is enabled (matching sitemap behaviour).
  • URLs are built from the collection's urlPattern and localized through the resolved Astro i18n routing config (prefixDefaultLocale, custom locale path mappings), via the same interpolateUrlPattern/localizePath helpers the sitemap uses.

Acceptance criteria mapping: content pages emit rel=alternate per translation (EmDashHead + helper) ✓ · x-default documented and implemented consistently with the sitemap ✓ · locale routing honours Astro and EmDash i18n config (localizePath) ✓ · missing/unroutable translations handled safely (dropped; empty result for missing entries) ✓ · tests cover translated and untranslated entries ✓.

Documentation: new "hreflang Alternates in the Page Head" section in the internationalization guide, covering the automatic EmDashHead path, the manual helper, and the x-default fallback rules.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Cursor + Fable 5, reviewed and tested by me

Screenshots / test output

New test suite tests/integration/seo/hreflang.test.ts (10 tests): translated pair, resolution from a non-default sibling, untranslated entry (self + x-default), unpublished siblings excluded, unroutable locales dropped, x-default fallback when the default locale is missing, i18n disabled, missing site URL, missing entry, and the default /{collection}/{slug} pattern. Full seo + page test set passes (7 files, 130 tests); lint and typecheck clean.

Multilingual SEO previously stopped at the sitemap: translation
siblings were cross-linked with xhtml alternates there, but rendered
pages carried no hreflang annotations in their <head>.

Add a getHreflangAlternates() helper that resolves the alternate set
for a content entry with the same semantics as the sitemap route: one
alternate per published translation sibling (including a
self-referencing entry, per Google's recommendation), x-default on the
default-locale variant (falling back to the first routable variant),
unroutable locales dropped, drafts excluded, and an empty result when
i18n is disabled (no queries run). URLs are built from the collection's
urlPattern and localized through the Astro i18n routing config, so head
and sitemap always agree.

EmDashHead emits the links automatically when the page context carries
a content reference, as base-layer contributions so plugins can
override individual hreflang entries. The helper is also exported from
"emdash" and "emdash/seo" for hand-rolled heads, with a *WithDb variant
for callers that already hold a database handle. Result resolution is
request-cached.

Fixes emdash-cms#1690
@changeset-bot

changeset-bot Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ddb4fb8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1907

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1907

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1907

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1907

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1907

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1907

emdash

npm i https://pkg.pr.new/emdash@1907

create-emdash

npm i https://pkg.pr.new/create-emdash@1907

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1907

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1907

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1907

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1907

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1907

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1907

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1907

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1907

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1907

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1907

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1907

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1907

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1907

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1907

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1907

commit: ddb4fb8

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is sound: this adds the missing render-path hreflang piece on top of the existing i18n/URL-resolution helpers, keeps the public API minimal, request-caches the lookup, and emits the links through the established PageMetadataContribution pipeline. The code is well-tested for the helper and follows the package's import/export conventions.

The one real concern is a semantic gap versus the sitemap. The PR states that head and sitemap should "always agree," but getHreflangAlternatesWithDb only filters on status === "published" while handleSitemapData also respects _emdash_collections.has_seo and _emdash_seo.seo_no_index. That means a page marked noindex (or a collection with SEO disabled) will still emit self-referencing hreflang alternates in <EmDashHead>, which contradicts the noindex directive and the sitemap. It's a concrete SEO-correctness bug, not just an implementation detail.

I also noted that the helper falls back to settings.url without verifying it is absolute; a relative value silently produces invalid hreflang URLs in manual usage.

Comment thread packages/core/src/seo/hreflang.ts Outdated
let variants = await repo.findTranslations(collection, group);
if (variants.length === 0) variants = [item];

const published = variants.filter((v) => v.status === "published");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[needs fixing] The published filter only checks status === "published", so it diverges from the sitemap semantics the PR claims to mirror. handleSitemapData excludes collections with has_seo = 0 and entries whose _emdash_seo.seo_no_index = 1. A noindex page rendered by <EmDashHead> would therefore still emit self-referencing rel="alternate" links, sending search engines a contradictory signal.

Add the same _emdash_seo check the sitemap uses (or share the sitemap query logic) so that head and sitemap cannot disagree on which variants are discoverable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ddb4fb8 — the alternate set now applies the same _emdash_seo filter as the sitemap: variants with seo_no_index = 1 are excluded via a batch lookup, and when the requested entry itself is noindex the helper returns an empty set (a page opting out of indexing shouldn't announce an hreflang set at all). Entries without an _emdash_seo row stay indexable by default, so has_seo = 0 collections behave consistently too. Two new tests cover the sibling-exclusion and self-noindex cases.

Comment thread packages/core/src/seo/hreflang.ts Outdated
const settings = await getSiteSettingsWithDb(db);
siteUrl = settings.url;
}
if (!siteUrl) return [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] This accepts any truthy siteUrl, including relative values from settings.url. A relative site URL yields invalid hreflang output like example.com/fr/blog/... and, in <EmDashHead>, the link is silently dropped by resolvePageMetadata's isSafeHref check. Validate that the resolved URL uses an absolute http(s):// scheme before stripping the trailing slash.

Suggested change
if (!siteUrl) return [];
if (!siteUrl || !/^https?:\/\//i.test(siteUrl)) return [];

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ddb4fb8 — the resolved site URL is now validated against ^https?:\/\//i before use; relative values (from settings or the option) yield an empty result instead of malformed alternates. Added a test for the relative-URL case.

Match the sitemap's _emdash_seo filter: variants flagged noindex are
excluded from alternate sets, and a noindex entry emits no hreflang set
at all, so head and sitemap can't disagree on which variants are
discoverable. Also reject relative site URLs — hreflang requires
fully-qualified URLs, and EmDashHead's isSafeHref would silently drop
the malformed links.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/needs-review No maintainer or bot review yet labels Jul 10, 2026
@swissky swissky added the bot:review Trigger an emdashbot code review on this PR label Jul 10, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ascorbic
ascorbic merged commit 15f4057 into emdash-cms:main Jul 10, 2026
53 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jul 10, 2026
@swissky
swissky deleted the feat/hreflang-alternates branch September 10, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core area/docs bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Render hreflang alternates in page head for translated content

2 participants