Skip to content

Commit a23985b

Browse files
apatel369ematipico
andauthoredNov 21, 2024··
fix: return correct locale in root 404 and 500 page with i18n (#12365)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 28dd3ce commit a23985b

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed
 

‎.changeset/warm-poems-breathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an issue where `Astro.currentLocale` was not correctly returning the locale for 404 and 500 pages.

‎packages/astro/src/core/render-context.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { callMiddleware } from './middleware/callMiddleware.js';
3636
import { sequence } from './middleware/index.js';
3737
import { renderRedirect } from './redirects/render.js';
3838
import { type Pipeline, Slots, getParams, getProps } from './render/index.js';
39+
import { isRoute404or500 } from './routing/match.js';
3940
import { copyRequest, setOriginPathname } from './routing/rewrite.js';
4041

4142
export const apiContextRoutesSymbol = Symbol.for('context.routes');
@@ -541,11 +542,9 @@ export class RenderContext {
541542
}
542543

543544
let computedLocale;
544-
if (routeData.pathname) {
545-
computedLocale = computeCurrentLocale(routeData.pathname, locales, defaultLocale);
546-
} else {
547-
computedLocale = computeCurrentLocale(url.pathname, locales, defaultLocale);
548-
}
545+
const pathname =
546+
routeData.pathname && !isRoute404or500(routeData) ? routeData.pathname : url.pathname;
547+
computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
549548
this.#currentLocale = computedLocale ?? fallbackTo;
550549

551550
return this.#currentLocale;

‎packages/astro/src/core/routing/match.ts

+10
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ export function matchRoute(pathname: string, manifest: ManifestData): RouteData
1515
export function matchAllRoutes(pathname: string, manifest: ManifestData): RouteData[] {
1616
return manifest.routes.filter((route) => route.pattern.test(decodeURI(pathname)));
1717
}
18+
19+
/**
20+
* Determines if the given route matches a 404 or 500 error page.
21+
*
22+
* @param {RouteData} route - The route data to check.
23+
* @returns {boolean} `true` if the route matches a 404 or 500 error page, otherwise `false`.
24+
*/
25+
export function isRoute404or500(route: RouteData): boolean {
26+
return route.pattern.test('/404') || route.pattern.test('/500');
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
const currentLocale = Astro.currentLocale;
3+
---
4+
<html>
5+
<head>
6+
<title>404 - Not Found</title>
7+
</head>
8+
<body>
9+
<h1>404 - Not Found</h1>
10+
<p>Current Locale: {currentLocale ? currentLocale : "none"}</p>
11+
</body>
12+
</html>

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

+26
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ describe('[DEV] i18n routing', () => {
8282
assert.equal(response.status, 200);
8383
assert.equal((await response.text()).includes('Endurance'), true);
8484
});
85+
86+
it('should return the correct locale on 404 page for non existing default locale page', async () => {
87+
const response = await fixture.fetch('/es/nonexistent-page');
88+
assert.equal(response.status, 404);
89+
assert.equal((await response.text()).includes('Current Locale: es'), true);
90+
});
91+
92+
it('should return the correct locale on 404 page for non existing english locale page', async () => {
93+
const response = await fixture.fetch('/en/nonexistent-page');
94+
assert.equal(response.status, 404);
95+
assert.equal((await response.text()).includes('Current Locale: en'), true);
96+
});
8597
});
8698

8799
describe('i18n routing', () => {
@@ -1200,6 +1212,20 @@ describe('[SSR] i18n routing', () => {
12001212
assert.equal(response.status, 200);
12011213
assert.equal((await response.text()).includes('Endurance'), true);
12021214
});
1215+
1216+
it('should return the correct locale on 404 page for non existing default locale page', async () => {
1217+
let request = new Request('http://example.com/es/nonexistent-page');
1218+
let response = await app.render(request);
1219+
assert.equal(response.status, 404);
1220+
assert.equal((await response.text()).includes('Current Locale: es'), true);
1221+
});
1222+
1223+
it('should return the correct locale on 404 page for non existing english locale page', async () => {
1224+
let request = new Request('http://example.com/en/nonexistent-page');
1225+
let response = await app.render(request);
1226+
assert.equal(response.status, 404);
1227+
assert.equal((await response.text()).includes('Current Locale: en'), true);
1228+
});
12031229
});
12041230

12051231
describe('default', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.