Skip to content

Commit 1a026af

Browse files
authoredJan 14, 2025··
fix: 404 status in dev (#12980)
1 parent 1072c76 commit 1a026af

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed
 

‎.changeset/itchy-trains-allow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a case where setting the status of a page to `404` in development would show the default 404 page (or custom one if provided) instead of using the current page

‎packages/astro/src/vite-plugin-astro-server/route.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,13 @@ export async function handleRoute({
241241
);
242242
}
243243

244-
if (statusCode === 404 && response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no') {
244+
if (
245+
statusCode === 404 &&
246+
// If the body isn't null, that means the user sets the 404 status
247+
// but uses the current route to handle the 404
248+
response.body === null &&
249+
response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no'
250+
) {
245251
const fourOhFourRoute = await matchRoute('/404', manifestData, pipeline);
246252
if (fourOhFourRoute) {
247253
renderContext = await RenderContext.create({

‎packages/astro/test/astro-response.test.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// @ts-check
12
import assert from 'node:assert/strict';
23
import { after, before, describe, it } from 'node:test';
34
import { loadFixture } from './test-utils.js';
45

56
// Asset bundling
67
describe('Returning responses', () => {
8+
/** @type {Awaited<ReturnType<typeof loadFixture>>} */
79
let fixture;
810
/** @type {import('./test-utils').DevServer} */
911
let devServer;
@@ -21,7 +23,23 @@ describe('Returning responses', () => {
2123
});
2224

2325
it('Works from a page', async () => {
24-
let response = await fixture.fetch('/not-found');
26+
const response = await fixture.fetch('/not-found');
2527
assert.equal(response.status, 404);
2628
});
29+
30+
it('Returns the default 404 is body is null', async () => {
31+
const response = await fixture.fetch('/not-found');
32+
const html = await response.text();
33+
34+
assert.equal(response.status, 404);
35+
assert.equal(html.includes('<pre>Path: /not-found</pre>'), true);
36+
});
37+
38+
it('Returns the page is body is not null', async () => {
39+
const response = await fixture.fetch('/not-found-custom');
40+
const html = await response.text();
41+
42+
assert.equal(response.status, 404);
43+
assert.equal(html.includes('Custom 404'), true);
44+
});
2745
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Astro.response.status = 404
3+
---
4+
<div>Custom 404</div>

‎packages/astro/test/test-utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ process.env.ASTRO_TELEMETRY_DISABLED = true;
3434
* @property {typeof build} build
3535
* @property {(url: string) => string} resolveUrl
3636
* @property {(path: string) => Promise<boolean>} pathExists
37-
* @property {(url: string, opts: Parameters<typeof fetch>[1]) => Promise<Response>} fetch
37+
* @property {(url: string, opts?: Parameters<typeof fetch>[1]) => Promise<Response>} fetch
3838
* @property {(path: string) => Promise<string>} readFile
3939
* @property {(path: string, updater: (content: string) => string) => Promise<void>} editFile
4040
* @property {(path: string) => Promise<string[]>} readdir
4141
* @property {(pattern: string) => Promise<string[]>} glob
42-
* @property {typeof dev} startDevServer
42+
* @property {(inlineConfig?: Parameters<typeof dev>[0]) => ReturnType<typeof dev>} startDevServer
4343
* @property {typeof preview} preview
4444
* @property {() => Promise<void>} clean
4545
* @property {() => Promise<App>} loadTestAdapterApp

0 commit comments

Comments
 (0)
Please sign in to comment.