fix i18n fallback rewrites failing in server mode#14017
Conversation
🦋 Changeset detectedLatest 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 |
| 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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 Performance ReportMerging #14017 will not alter performanceComparing Summary
|
ematipico
left a comment
There was a problem hiding this comment.
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, |
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Changes
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
i18n-routing-fallback-rewrite-hybrid)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.