Skip to content

Commit

Permalink
fix: correctly return 404.astro in i18n (#12764)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Dec 19, 2024
1 parent 45005a5 commit 4353fc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export function createI18nMiddleware(
const _requestHasLocale = requestHasLocale(payload.locales);
const _redirectToFallback = redirectToFallback(payload);

const prefixAlways = (context: APIContext): Response | undefined => {
const prefixAlways = (context: APIContext, response: Response): Response | undefined => {
const url = context.url;
if (url.pathname === base + '/' || url.pathname === base) {
return _redirectToDefaultLocale(context);
}

// Astro can't know where the default locale is supposed to be, so it returns a 404.
else if (!_requestHasLocale(context)) {
return _noFoundForNonLocaleRoute(context);
return _noFoundForNonLocaleRoute(context, response);
}

return undefined;
Expand Down Expand Up @@ -124,15 +124,15 @@ export function createI18nMiddleware(
}

case 'pathname-prefix-always': {
const result = prefixAlways(context);
const result = prefixAlways(context, response);
if (result) {
return result;
}
break;
}
case 'domains-prefix-always': {
if (localeHasntDomain(i18n, currentLocale)) {
const result = prefixAlways(context);
const result = prefixAlways(context, response);
if (result) {
return result;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,22 @@ describe('[DEV] i18n routing', () => {
it("should NOT render the default locale if there isn't a fallback and the route is missing", async () => {
const response = await fixture.fetch('/it/start');
assert.equal(response.status, 404);
const html = await response.text();
assert.match(html, /Can't find the page you're looking for./);
});

it("should render a 404 because the route `fr` isn't included in the list of locales of the configuration", async () => {
const response = await fixture.fetch('/fr/start');
assert.equal(response.status, 404);
const html = await response.text();
assert.match(html, /Can't find the page you're looking for./);
});

it('should render the custom 404.astro when navigating non-existing routes ', async () => {
const response = await fixture.fetch('/does-not-exist');
assert.equal(response.status, 404);
const html = await response.text();
assert.match(html, /Can't find the page you're looking for./);
});
});

Expand Down

0 comments on commit 4353fc5

Please sign in to comment.