Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading