From c76901065545f6a8d3de3e44d1c8ee5456a8a77a Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Wed, 6 Dec 2023 04:07:52 -0800 Subject: [PATCH] Fix issue where 404/500 status codes were logged as "[200]" (#9336) --- .changeset/modern-mice-shout.md | 5 +++ packages/astro/src/core/messages.ts | 2 +- .../src/vite-plugin-astro-server/route.ts | 35 +++++++++---------- 3 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 .changeset/modern-mice-shout.md diff --git a/.changeset/modern-mice-shout.md b/.changeset/modern-mice-shout.md new file mode 100644 index 0000000000..cc483b1937 --- /dev/null +++ b/.changeset/modern-mice-shout.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +dev: fix issue where 404 and 500 responses were logged as 200 diff --git a/packages/astro/src/core/messages.ts b/packages/astro/src/core/messages.ts index ebac2c51d6..b105e985cb 100644 --- a/packages/astro/src/core/messages.ts +++ b/packages/astro/src/core/messages.ts @@ -37,7 +37,7 @@ export function req({ method?: string; reqTime?: number; }): string { - const color = statusCode >= 400 ? red : statusCode >= 300 ? yellow : blue; + const color = statusCode >= 500 ? red : statusCode >= 300 ? yellow : blue; return ( color(`[${statusCode}]`) + ` ` + diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 92a7a8247a..3196b951a7 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -332,7 +332,7 @@ export async function handleRoute({ req({ url: pathname, method: incomingRequest.method, - statusCode: response.status, + statusCode: status ?? response.status, reqTime: timeEnd - timeStart, }) ); @@ -356,24 +356,23 @@ export async function handleRoute({ } if (route.type === 'endpoint') { await writeWebResponse(incomingResponse, response); - } else { - if ( - // We are in a recursion, and it's possible that this function is called itself with a status code - // By default, the status code passed via parameters is computed by the matched route. - // - // By default, we should give priority to the status code passed, although it's possible that - // the `Response` emitted by the user is a redirect. If so, then return the returned response. - response.status < 400 && - response.status >= 300 - ) { - await writeSSRResult(request, response, incomingResponse); - return; - } else if (status && response.status !== status && (status === 404 || status === 500)) { - // Response.status is read-only, so a clone is required to override - response = new Response(response.body, { ...response, status }); - } - await writeSSRResult(request, response, incomingResponse); + return; } + // We are in a recursion, and it's possible that this function is called itself with a status code + // By default, the status code passed via parameters is computed by the matched route. + // + // By default, we should give priority to the status code passed, although it's possible that + // the `Response` emitted by the user is a redirect. If so, then return the returned response. + if (response.status < 400 && response.status >= 300) { + await writeSSRResult(request, response, incomingResponse); + return; + } + // Apply the `status` override to the response object before responding. + // Response.status is read-only, so a clone is required to override. + if (status && response.status !== status && (status === 404 || status === 500)) { + response = new Response(response.body, { ...response, status }); + } + await writeSSRResult(request, response, incomingResponse); } interface GetScriptsAndStylesParams {