Skip to content

Commit

Permalink
fix(i18n): fallback SSR (#10755)
Browse files Browse the repository at this point in the history
* fix(i18n): fallback SSR

* Update .changeset/old-pugs-jog.md

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
  • Loading branch information
ematipico and florian-lefebvre committed Apr 11, 2024
1 parent 66bc104 commit c6d59b6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-pugs-jog.md
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a case where the i18n fallback failed to correctly redirect to the index page with SSR enabled
7 changes: 6 additions & 1 deletion packages/astro/src/i18n/index.ts
Expand Up @@ -343,6 +343,7 @@ export function redirectToFallback({
locales,
defaultLocale,
strategy,
base,
}: MiddlewarePayload) {
return function (context: APIContext, response: Response): Response {
if (response.status >= 300 && fallback) {
Expand Down Expand Up @@ -370,7 +371,11 @@ export function redirectToFallback({
// If a locale falls back to the default locale, we want to **remove** the locale because
// the default locale doesn't have a prefix
if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') {
newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
if (context.url.pathname.includes(`${base}`)) {
newPathname = context.url.pathname.replace(`/${urlLocale}`, ``);
} else {
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/`);
}
} else {
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
}
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Expand Up @@ -3,7 +3,6 @@ import type { SSRManifestI18n } from '../core/app/types.js';
import { ROUTE_TYPE_HEADER } from '../core/constants.js';
import {
type MiddlewarePayload,
getPathByLocale,
normalizeTheLocale,
notFound,
redirectToDefaultLocale,
Expand Down
33 changes: 33 additions & 0 deletions packages/astro/test/i18n-routing.test.js
Expand Up @@ -1871,3 +1871,36 @@ describe('i18n routing does not break assets and endpoints', () => {
});
});
});

describe('SSR fallback from missing locale index to default locale index', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let app;

before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing-prefix-other-locales/',
output: 'server',
adapter: testAdapter(),
i18n: {
defaultLocale: 'en',
locales: ['en', 'fr'],
routing: {
prefixDefaultLocale: false,
},
fallback: {
fr: 'en',
},
},
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});

it('should correctly redirect', async () => {
let request = new Request('http://example.com/fr');
let response = await app.render(request);
assert.equal(response.status, 302);
assert.equal(response.headers.get('location'), '/');
});
});

0 comments on commit c6d59b6

Please sign in to comment.