Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 037e4f1

Browse files
authored
fix(i18n): don't consider URLs that start with the name of the defaut locale (#10016)
1 parent fa9218e commit 037e4f1

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

.changeset/tough-socks-change.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes a bug where routes with a name that start with the name of the `i18n.defaultLocale` were incorrectly returning a 404 response.

packages/astro/src/i18n/middleware.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export function createI18nMiddleware(
6565
};
6666

6767
const prefixOtherLocales = (url: URL, response: Response): Response | undefined => {
68-
const pathnameContainsDefaultLocale = url.pathname.includes(`/${i18n.defaultLocale}`);
68+
let pathnameContainsDefaultLocale = false;
69+
for (const segment of url.pathname.split('/')) {
70+
if (normalizeTheLocale(segment) === normalizeTheLocale(i18n.defaultLocale)) {
71+
pathnameContainsDefaultLocale = true;
72+
break;
73+
}
74+
}
6975
if (pathnameContainsDefaultLocale) {
7076
const newLocation = url.pathname.replace(`/${i18n.defaultLocale}`, '');
7177
response.headers.set('Location', newLocation);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Astro</title>
4+
</head>
5+
<body>
6+
Endurance
7+
</body>
8+
</html>
9+

packages/astro/test/i18n-routing.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ describe('astro:i18n virtual module', () => {
5959
});
6060
});
6161
describe('[DEV] i18n routing', () => {
62+
describe('should render a page that stars with a locale but it is a page', () => {
63+
/** @type {import('./test-utils').Fixture} */
64+
let fixture;
65+
/** @type {import('./test-utils').DevServer} */
66+
let devServer;
67+
68+
before(async () => {
69+
fixture = await loadFixture({
70+
root: './fixtures/i18n-routing/',
71+
});
72+
devServer = await fixture.startDevServer();
73+
});
74+
75+
after(async () => {
76+
await devServer.stop();
77+
});
78+
79+
it('renders the page', async () => {
80+
const response = await fixture.fetch('/endurance');
81+
expect(response.status).to.equal(200);
82+
expect(await response.text()).includes('Endurance');
83+
});
84+
});
85+
6286
describe('i18n routing', () => {
6387
/** @type {import('./test-utils').Fixture} */
6488
let fixture;
@@ -1005,6 +1029,23 @@ describe('[SSG] i18n routing', () => {
10051029
});
10061030
});
10071031

1032+
describe('should render a page that stars with a locale but it is a page', () => {
1033+
/** @type {import('./test-utils').Fixture} */
1034+
let fixture;
1035+
1036+
before(async () => {
1037+
fixture = await loadFixture({
1038+
root: './fixtures/i18n-routing/',
1039+
});
1040+
await fixture.build();
1041+
});
1042+
1043+
it('renders the page', async () => {
1044+
const html = await fixture.readFile('/endurance/index.html');
1045+
expect(html).includes('Endurance');
1046+
});
1047+
});
1048+
10081049
describe('current locale', () => {
10091050
describe('with [prefix-other-locales]', () => {
10101051
/** @type {import('./test-utils').Fixture} */
@@ -1068,6 +1109,29 @@ describe('[SSG] i18n routing', () => {
10681109
});
10691110
describe('[SSR] i18n routing', () => {
10701111
let app;
1112+
1113+
describe('should render a page that stars with a locale but it is a page', () => {
1114+
/** @type {import('./test-utils').Fixture} */
1115+
let fixture;
1116+
1117+
before(async () => {
1118+
fixture = await loadFixture({
1119+
root: './fixtures/i18n-routing/',
1120+
output: 'server',
1121+
adapter: testAdapter(),
1122+
});
1123+
await fixture.build();
1124+
app = await fixture.loadTestAdapterApp();
1125+
});
1126+
1127+
it('renders the page', async () => {
1128+
let request = new Request('http://example.com/endurance');
1129+
let response = await app.render(request);
1130+
expect(response.status).to.equal(200);
1131+
expect(await response.text()).includes('Endurance');
1132+
});
1133+
});
1134+
10711135
describe('default', () => {
10721136
/** @type {import('./test-utils').Fixture} */
10731137
let fixture;

0 commit comments

Comments
 (0)