0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix(i18n): fallback SSR (#10755)

* 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>
This commit is contained in:
Emanuele Stoppa 2024-04-11 14:51:10 +01:00 committed by GitHub
parent 66bc1041d4
commit c6d59b6fb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 44 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes a case where the i18n fallback failed to correctly redirect to the index page with SSR enabled

View file

@ -343,6 +343,7 @@ export function redirectToFallback({
locales, locales,
defaultLocale, defaultLocale,
strategy, strategy,
base,
}: MiddlewarePayload) { }: MiddlewarePayload) {
return function (context: APIContext, response: Response): Response { return function (context: APIContext, response: Response): Response {
if (response.status >= 300 && fallback) { if (response.status >= 300 && fallback) {
@ -370,7 +371,11 @@ export function redirectToFallback({
// If a locale falls back to the default locale, we want to **remove** the locale because // If a locale falls back to the default locale, we want to **remove** the locale because
// the default locale doesn't have a prefix // the default locale doesn't have a prefix
if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') { 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 { } else {
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`); newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
} }

View file

@ -3,7 +3,6 @@ import type { SSRManifestI18n } from '../core/app/types.js';
import { ROUTE_TYPE_HEADER } from '../core/constants.js'; import { ROUTE_TYPE_HEADER } from '../core/constants.js';
import { import {
type MiddlewarePayload, type MiddlewarePayload,
getPathByLocale,
normalizeTheLocale, normalizeTheLocale,
notFound, notFound,
redirectToDefaultLocale, redirectToDefaultLocale,

View file

@ -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'), '/');
});
});