0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

fix(i18n): don't consider URLs that start with the name of the defaut locale (#10016)

This commit is contained in:
Emanuele Stoppa 2024-02-07 14:42:51 +00:00 committed by GitHub
parent fa9218e836
commit 037e4f12dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 85 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes a bug where routes with a name that start with the name of the `i18n.defaultLocale` were incorrectly returning a 404 response.

View file

@ -65,7 +65,13 @@ export function createI18nMiddleware(
};
const prefixOtherLocales = (url: URL, response: Response): Response | undefined => {
const pathnameContainsDefaultLocale = url.pathname.includes(`/${i18n.defaultLocale}`);
let pathnameContainsDefaultLocale = false;
for (const segment of url.pathname.split('/')) {
if (normalizeTheLocale(segment) === normalizeTheLocale(i18n.defaultLocale)) {
pathnameContainsDefaultLocale = true;
break;
}
}
if (pathnameContainsDefaultLocale) {
const newLocation = url.pathname.replace(`/${i18n.defaultLocale}`, '');
response.headers.set('Location', newLocation);

View file

@ -0,0 +1,9 @@
<html>
<head>
<title>Astro</title>
</head>
<body>
Endurance
</body>
</html>

View file

@ -59,6 +59,30 @@ describe('astro:i18n virtual module', () => {
});
});
describe('[DEV] i18n routing', () => {
describe('should render a page that stars with a locale but it is a page', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('renders the page', async () => {
const response = await fixture.fetch('/endurance');
expect(response.status).to.equal(200);
expect(await response.text()).includes('Endurance');
});
});
describe('i18n routing', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
@ -1005,6 +1029,23 @@ describe('[SSG] i18n routing', () => {
});
});
describe('should render a page that stars with a locale but it is a page', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing/',
});
await fixture.build();
});
it('renders the page', async () => {
const html = await fixture.readFile('/endurance/index.html');
expect(html).includes('Endurance');
});
});
describe('current locale', () => {
describe('with [prefix-other-locales]', () => {
/** @type {import('./test-utils').Fixture} */
@ -1068,6 +1109,29 @@ describe('[SSG] i18n routing', () => {
});
describe('[SSR] i18n routing', () => {
let app;
describe('should render a page that stars with a locale but it is a page', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing/',
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('renders the page', async () => {
let request = new Request('http://example.com/endurance');
let response = await app.render(request);
expect(response.status).to.equal(200);
expect(await response.text()).includes('Endurance');
});
});
describe('default', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;