Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/witty-jobs-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes localized error pages such as `src/pages/pt/404.astro` returning a `200` status when rendered through the `pages()` handler from `astro/fetch` or `astro/hono`
2 changes: 2 additions & 0 deletions packages/astro/src/core/pages/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../app/origin-check.js';
import { getCookiesFromResponse } from '../cookies/response.js';
import { renderErrorFromState } from '../errors/handler.js';
import { getDefaultStatusCode } from '../routing/helpers.js';

// Shared empty-slots object so we don't allocate `{}` on every render for
// requests that don't come from the container API. Safe to share because
Expand Down Expand Up @@ -121,6 +122,7 @@ export async function handlePagesWithErrorFallback(state: FetchState): Promise<R
) {
return createCrossOriginForbiddenResponse(ctx.request);
}
state.status = getDefaultStatusCode(state.manifest, state.routeData, state.pathname);
Comment thread
astro-factory[bot] marked this conversation as resolved.
Resolved
try {
return await handlePages(state, ctx);
} catch (err: any) {
Expand Down
28 changes: 28 additions & 0 deletions packages/astro/test/units/fetch/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,34 @@ describe('pages()', () => {
assert.match(text, /<h1>my custom 500<\/h1>/);
});

it('renders a localized 404 route with a 404 status', async () => {
const notFoundPage = createComponent((_result: any, _props: any, _slots: any) => {
return render`<h1>Não encontrado</h1>`;
});
const app = createTestApp(
[createPage(simplePage, { route: '/' }), createPage(notFoundPage, { route: '/pt/404' })],
{
i18n: {
defaultLocale: 'en',
locales: ['en', 'pt'],
strategy: 'manual',
fallbackType: 'rewrite',
fallback: {},
domains: {},
domainLookupTable: {},
},
},
);
const request = stampApp(new Request('http://example.com/pt/404'), app);
const state = new FetchState(request);

const response = await pages(state);

assert.equal(response.status, 404);
const text = await response.text();
assert.match(text, /<h1>Não encontrado<\/h1>/);
});

it('returns a marked 404 for the app post-check when the custom 404 route is prerendered', async () => {
const notFoundPage = createComponent((_result: any, _props: any, _slots: any) => {
return render`<h1>Not Found</h1>`;
Expand Down
Loading