Skip to content

fix i18n fallback rewrites failing in server mode#14017

Merged
ematipico merged 3 commits into
withastro:mainfrom
dmgawel:dmgawel/fallback-rewrite-fix
Jul 2, 2025
Merged

fix i18n fallback rewrites failing in server mode#14017
ematipico merged 3 commits into
withastro:mainfrom
dmgawel:dmgawel/fallback-rewrite-fix

Conversation

@dmgawel

@dmgawel dmgawel commented Jun 26, 2025

Copy link
Copy Markdown
Contributor

Changes

  • Fixes i18n fallback rewrites in server mode: i18n fallback rewrites (e.g., /es/about → /about) now work correctly when using SSR adapters with output: "server"
  • Updates route matching for server builds: Server builds place static files in a client/ subdirectory, which was causing route matching to fail. Now uses the correct output directory based on build mode
  • Allows i18n fallback rewrites in ForbiddenRewrite check: The safety check that prevents rewriting from SSR routes to static routes now correctly identifies and allows i18n fallback scenarios. This was required for dev mode, where the initial matched route for i18n fallbacks is 404 page (dynamic)

Before: /es/about-us with i18n fallback would throw ForbiddenRewrite: You tried to rewrite the on-demand route with the static route error in server mode

After: /es/about-us correctly falls back to /about-us and renders the English version when no Spanish version exists

Fixes #13964

Testing

  • Added new test fixture (i18n-routing-fallback-rewrite-hybrid)
  • Verified behavior in both static and server output modes

Docs

No docs changes needed - this fixes existing documented i18n fallback functionality that was broken in server mode. The user-facing behavior and API remain the same.

@changeset-bot

changeset-bot Bot commented Jun 26, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4fd2955

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

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

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label Jun 26, 2025
if (this.pipeline.serverLike && !this.routeData.prerender && routeData.prerender) {
// Allow i18n fallback rewrites - if the target route has fallback routes, this is likely an i18n scenario
const isI18nFallback = routeData.fallbackRoutes && routeData.fallbackRoutes.length > 0;
if (this.pipeline.serverLike && !this.routeData.prerender && routeData.prerender && !isI18nFallback) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In dev mode, the initially matched route for i18n fallback pages is 404 page, which is not prerendered. It's then rewritten to the correct route. This caused the ForbiddenRewrite check to kick in, because we are in a serverLike env, original route is not server rendered, and target route is static. I'm not entirely sure why it works like that in dev mode, but I've decided to add another check for fallbackRoutes, and allow rewrites to pages with fallback pages.

buildFormat: AstroConfig['build']['format'];
base: AstroConfig['base'];
outDir: AstroConfig['outDir'] | string;
outDir: AstroConfig['outDir'] | AstroConfig['build']['client'] | string;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

During build mode, in the server output, statically generated pages are prefixed with client/ (or whatever is configured in build.client). This was the main culprit of i18n fallback rewrites not working in server mode — they simply didn't match in the findRouteToRewrite function.

My fix uses buildClient from manifest in server output for the findRouteToRewrite function, so the route match correctly and doesn't return 404.

After the route is matched as expected, ForbiddenRewrite during build phase no longer kick in.

@codspeed-hq

codspeed-hq Bot commented Jun 26, 2025

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #14017 will not alter performance

Comparing dmgawel:dmgawel/fallback-rewrite-fix (4fd2955) with main (508e6bf)

Summary

✅ 6 untouched benchmarks

@ematipico ematipico 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.

Thank you for fixing this! Just added a small correction to the changeset

buildFormat: this.manifest.buildFormat,
base: this.manifest.base,
outDir: this.manifest.outDir,
outDir: this.serverLike ? this.manifest.buildClientDir : this.manifest.outDir,

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.

Great catch!

Comment thread .changeset/rare-spies-join.md Outdated
@ematipico ematipico merged commit 8d238bc into withastro:main Jul 2, 2025
17 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jul 2, 2025
openscript pushed a commit to openscript/astro that referenced this pull request Sep 12, 2025
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding an SSR adapter breaks i18n fallback rewrites for static pages

2 participants