fix(seo): correct sitemap locale prefix and hreflang for pages#58
fix(seo): correct sitemap locale prefix and hreflang for pages#58JohnRDOrazio merged 1 commit intomainfrom
Conversation
The sitemap was emitting two bugs for non-EN page entries: 1. `<loc>` doubled the locale prefix because Polylang's `uri` field already includes `/it/...`, and `buildUrl` prepended the locale again — Italian entries became `/it/it/governance-2/...`. 2. `<xhtml:link rel="alternate">` reused that same locale-specific URI for every hreflang, so the English alternate (and all others) pointed at a non-existent IT URL. Change `getAllPages` to query `translations` and resolve to the EN canonical URI (no locale prefix) regardless of which locale is being fetched. Also expose each page's `availableLocales` so the sitemap only emits hreflang alternates for languages that actually have a translation. Posts and projects continue to use their own locale-specific slugs in URLs and emit hreflang alternates for all locales — that's a separate pre-existing issue (e.g. `/projects/ontokit-2` IT slug gets used for the EN alternate despite EN having a different slug) and is out of scope here. Will track separately if it warrants a fix before #55 lands. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe changes update the sitemap generation to use per-page language availability and English URIs instead of global locales and locale-prefixed URIs. The GraphQL query now fetches translation metadata, the API transforms the response into a new Changes
Sequence DiagramsequenceDiagram
participant GQL as GraphQL Query
participant API as getAllPages()
participant Sitemap as Sitemap Handler
GQL->>GQL: Fetch pages with translations metadata
GQL-->>API: Return pages + translations
API->>API: Transform per-page data:<br/>select enUri, collect locales
API-->>Sitemap: Return WPSitemapPage[]
Sitemap->>Sitemap: For each page entry:<br/>buildAlternateLinks(page.enUri,<br/>page.availableLocales)
Sitemap-->>Sitemap: Generate per-page hreflang links
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 9 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Closes #54.
Summary
Two bugs in
app/api/sitemap/[lang]/route.tsfor non-EN page entries:<loc>— Polylang'surialready includes the language directory (/it/...), andbuildUrlwas prepending the locale again, producinghttps://catholicdigitalcommons.org/it/it/governance-2/....<xhtml:link rel="alternate">reused that same locale-specific URI, so the English alternate (and all others) pointed at a non-existent IT URL.getAllPagesnow requeststranslations { language { code } uri }from WPGraphQL, resolves to the EN canonical URI (no locale prefix) regardless of which locale is being fetched, and exposes each page'savailableLocalesso the sitemap emits hreflang alternates only for languages that actually have a translation.Verification
Spun up the dev server and fetched both sitemaps. Spot-check from
it:Single locale prefix, every hreflang resolves correctly to the right per-locale URL on the same EN canonical path.
Files changed
lib/wordpress/queries.ts—GET_ALL_PAGESnow includestranslations { language { code } uri }.lib/wordpress/api.ts— newWPSitemapPageshape (enUri,modified,availableLocales);getAllPagesderives EN canonical URI and the locale availability list.app/api/sitemap/[lang]/route.ts— usespage.enUrifor the path, passespage.availableLocalesso hreflang only includes locales with translations.Out of scope (deliberately)
Posts and projects still use their own locale-specific slugs in
<loc>URLs (e.g./it/projects/ontokit-2,/it/projects/api-della-bibbia) and emit hreflang alternates for all locales using that same locale-specific slug. That's a separate pre-existing bug — the EN alternate for an IT project would point to/projects/api-della-bibbiaeven though EN's slug isbible-api. Fixing it requires querying CPT translations the same way pages now do, but the data flow is different enough (CPTs vs pages) that it warrants a separate PR if desired before #55 lands. Not made worse by this PR.The
-2collision-suffix problem on the IT project slug/projects/ontokit-2is the same systemic Polylang-free issue documented in discussion #56.Test plan
/api/sitemap/en—<loc>has no locale prefix, hreflang alternates use correct per-locale prefixes./api/sitemap/it—<loc>has single/it/prefix, no doubling.es/fr/pt/de.🤖 Generated with Claude Code
Summary by CodeRabbit