Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-states-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes i18n fallback routing replacing the first substring match instead of the actual locale segment, which mangled paths like `/energy/en/about` into `/esergy/en/about`
25 changes: 9 additions & 16 deletions packages/astro/src/i18n/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,8 @@ export interface ComputeFallbackRouteOptions {
* to a fallback locale based on the i18n configuration.
*/
export function computeFallbackRoute(options: ComputeFallbackRouteOptions): FallbackRouteResult {
const {
pathname,
responseStatus,
fallback,
fallbackType,
locales,
defaultLocale,
strategy,
base,
} = options;
const { pathname, responseStatus, fallback, fallbackType, locales, defaultLocale, strategy } =
options;

// Only apply fallback for 404 status codes (page not found in this locale)
if (responseStatus !== 404) {
Expand Down Expand Up @@ -96,19 +88,20 @@ export function computeFallbackRoute(options: ComputeFallbackRouteOptions): Fall
// Get the path for the fallback locale (handles granular locales)
const pathFallbackLocale = getPathByLocale(fallbackLocale, locales);

// Replace the locale segment by index so that earlier segments whose text
// happens to contain the locale code as a substring are left alone (#17907).
const localeIndex = segments.indexOf(urlLocale);
let newPathname: string;

// If fallback is to the default locale and strategy is prefix-other-locales,
// remove the locale prefix (default locale has no prefix)
if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') {
if (pathname.includes(`${base}`)) {
newPathname = pathname.replace(`/${urlLocale}`, ``);
} else {
newPathname = pathname.replace(`/${urlLocale}`, `/`);
}
segments.splice(localeIndex, 1);
newPathname = segments.join('/') || '/';
} else {
// Replace the current locale with the fallback locale
newPathname = pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
segments[localeIndex] = pathFallbackLocale;
newPathname = segments.join('/');
}

return {
Expand Down
42 changes: 42 additions & 0 deletions packages/astro/test/units/i18n/fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,48 @@ describe('computeFallbackRoute', () => {
assert.equal((result as Extract<FallbackRouteResult, { type: 'redirect' }>).pathname, '/en');
});

it('replaces only the locale segment when an earlier segment contains the locale as a substring', () => {
const result: FallbackRouteResult = computeFallbackRoute(
makeFallbackOptions({
pathname: '/energy/en/about',
responseStatus: 404,
currentLocale: 'en',
locales: ['en', 'es'],
defaultLocale: 'es',
fallback: { en: 'es' },
fallbackType: 'rewrite',
strategy: 'pathname-prefix-always',
}),
);

assert.equal(result.type, 'rewrite');
assert.equal(
(result as Extract<FallbackRouteResult, { type: 'rewrite' }>).pathname,
'/energy/es/about',
);
});

it('removes only the locale segment for prefix-other-locales when an earlier segment contains the locale as a substring', () => {
const result: FallbackRouteResult = computeFallbackRoute(
makeFallbackOptions({
pathname: '/espresso/es/about',
responseStatus: 404,
currentLocale: 'es',
locales: ['en', 'es'],
defaultLocale: 'en',
fallback: { es: 'en' },
fallbackType: 'rewrite',
strategy: 'pathname-prefix-other-locales',
}),
);

assert.equal(result.type, 'rewrite');
assert.equal(
(result as Extract<FallbackRouteResult, { type: 'rewrite' }>).pathname,
'/espresso/about',
);
});

it('preserves trailing content after locale replacement', () => {
const result: FallbackRouteResult = computeFallbackRoute(
makeFallbackOptions({
Expand Down
Loading