Skip to content

Commit

Permalink
fix(i18n): don't consider URLs that start with the name of the defaut…
Browse files Browse the repository at this point in the history
… locale (#10016)
  • Loading branch information
ematipico authored Feb 7, 2024
1 parent fa9218e commit 037e4f1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tough-socks-change.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 7 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Astro</title>
</head>
<body>
Endurance
</body>
</html>

64 changes: 64 additions & 0 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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} */
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 037e4f1

Please sign in to comment.