Skip to content

fix: route localized URLs through Vercel rewrites#10

Merged
mqmalagris merged 1 commit into
mainfrom
dev
May 1, 2026
Merged

fix: route localized URLs through Vercel rewrites#10
mqmalagris merged 1 commit into
mainfrom
dev

Conversation

@mqmalagris
Copy link
Copy Markdown
Owner

@mqmalagris mqmalagris commented May 1, 2026

Summary

Fixes the production 404 on every `/pt-br`, `/es`, `/fr` URL — the root cause that was blocking the multilingual SEO benefit from PR #3.

Root cause

`@astrojs/vercel@8.2.11` doesn't translate Astro's `i18n.fallback: rewrite` config into Vercel routes. The build's `config.json` only listed routes for the physical pages (`/account`, `/docs`, etc.), so requests to localized prefixes fell through to the edge router's 404 before ever reaching the Astro server function.

Fix

  • `vercel.json` rewrites map `/pt-br`, `/es`, `/fr` (plus `/:path*`) to the matching English route with a `?_locale=` query parameter. The browser keeps the original URL (Vercel preserves it on rewrites); the function gets the English path plus the locale hint.
  • `Layout.astro` locale resolution now checks `?_locale` first, then `Astro.currentLocale` (covers local dev where Astro routes the prefix natively), then the URL prefix (defensive fallback).
  • Canonical and hreflang were already built from `sharedPath + locale prefix`, so they continue to point to the user-visible URL — the rewrite is invisible.

Test plan

  • `pnpm build` succeeds
  • Local dev: `/pt-br/` and `/pt-br/account` still render with `` (Astro's own routing, no rewrite needed)
  • After deploy:
    for u in / /pt-br/ /es/ /fr/ /pt-br/account /es/account; do
      echo "$(curl -sL -o /dev/null -w "%{http_code}" https://www.ephemask.com$u)  $u"
    done
    every line returns `200`
  • `curl https://www.ephemask.com/pt-br/account | grep 'html lang'` shows ``
  • View source on `/pt-br/account` → `canonical` and `og:url` point to `https://ephemask.com/pt-br/account\` (with the locale prefix), not the rewritten English path
  • Sitemap-listed URLs all return 200 (Search Console "URL inspection" no longer shows 404 for the localized variants)

Root cause: @astrojs/vercel 8.2.11 doesn't translate Astro's
fallback: { rewrite } config into Vercel routes. The build's
config.json only listed routes for the physical pages
(/account, /docs, etc.), so /pt-br/*, /es/*, /fr/* fell through
to the edge router's 404 before ever reaching the Astro server
function.

Fix:
- vercel.json: rewrites for /pt-br, /es, /fr (plus /:path*) that
  internally route to the matching English path with a ?_locale
  query parameter so the function still knows the locale.
- Layout.astro: locale resolution now checks ?_locale first, then
  Astro.currentLocale (covers local dev), then the URL prefix
  (defensive). Vercel preserves the original URL in the browser,
  so canonical and hreflang remain correct.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ephemask-web Ready Ready Preview, Comment May 1, 2026 5:05pm

@mqmalagris mqmalagris merged commit eae6a32 into main May 1, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant