0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

fix: 404 status in dev (#12980)

This commit is contained in:
Florian Lefebvre 2025-01-14 17:58:07 +01:00 committed by GitHub
parent 1072c76703
commit 1a026afb42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
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

View file

@ -241,7 +241,13 @@ export async function handleRoute({
);
}
if (statusCode === 404 && response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no') {
if (
statusCode === 404 &&
// If the body isn't null, that means the user sets the 404 status
// but uses the current route to handle the 404
response.body === null &&
response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no'
) {
const fourOhFourRoute = await matchRoute('/404', manifestData, pipeline);
if (fourOhFourRoute) {
renderContext = await RenderContext.create({

View file

@ -1,9 +1,11 @@
// @ts-check
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
// Asset bundling
describe('Returning responses', () => {
/** @type {Awaited<ReturnType<typeof loadFixture>>} */
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;
@ -21,7 +23,23 @@ describe('Returning responses', () => {
});
it('Works from a page', async () => {
let response = await fixture.fetch('/not-found');
const response = await fixture.fetch('/not-found');
assert.equal(response.status, 404);
});
it('Returns the default 404 is body is null', async () => {
const response = await fixture.fetch('/not-found');
const html = await response.text();
assert.equal(response.status, 404);
assert.equal(html.includes('<pre>Path: /not-found</pre>'), true);
});
it('Returns the page is body is not null', async () => {
const response = await fixture.fetch('/not-found-custom');
const html = await response.text();
assert.equal(response.status, 404);
assert.equal(html.includes('Custom 404'), true);
});
});

View file

@ -0,0 +1,4 @@
---
Astro.response.status = 404
---
<div>Custom 404</div>

View file

@ -34,12 +34,12 @@ process.env.ASTRO_TELEMETRY_DISABLED = true;
* @property {typeof build} build
* @property {(url: string) => string} resolveUrl
* @property {(path: string) => Promise<boolean>} pathExists
* @property {(url: string, opts: Parameters<typeof fetch>[1]) => Promise<Response>} fetch
* @property {(url: string, opts?: Parameters<typeof fetch>[1]) => Promise<Response>} fetch
* @property {(path: string) => Promise<string>} readFile
* @property {(path: string, updater: (content: string) => string) => Promise<void>} editFile
* @property {(path: string) => Promise<string[]>} readdir
* @property {(pattern: string) => Promise<string[]>} glob
* @property {typeof dev} startDevServer
* @property {(inlineConfig?: Parameters<typeof dev>[0]) => ReturnType<typeof dev>} startDevServer
* @property {typeof preview} preview
* @property {() => Promise<void>} clean
* @property {() => Promise<App>} loadTestAdapterApp