From 8ff7658001c2c7bedf6adcddf7a9341196f2d376 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 19 Jul 2024 12:45:10 +0100 Subject: [PATCH] fix: better logging for rewrites (#11505) * fix: better logging for rewrites * fix: better logging for rewrites * apply feedback --- .changeset/blue-jars-hang.md | 12 ++++++++++++ packages/astro/src/core/messages.ts | 5 +++++ packages/astro/src/vite-plugin-astro-server/route.ts | 8 +++++--- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 .changeset/blue-jars-hang.md diff --git a/.changeset/blue-jars-hang.md b/.changeset/blue-jars-hang.md new file mode 100644 index 0000000000..2a7fd5b208 --- /dev/null +++ b/.changeset/blue-jars-hang.md @@ -0,0 +1,12 @@ +--- +'astro': patch +--- + +Enhances the dev server logging when rewrites occur during the lifecycle or rendering. + +The dev server will log the status code **before** and **after** a rewrite: + +```shell +08:16:48 [404] (rewrite) /foo/about 200ms +08:22:13 [200] (rewrite) /about 23ms +``` diff --git a/packages/astro/src/core/messages.ts b/packages/astro/src/core/messages.ts index 25d66a5906..8203f8ab83 100644 --- a/packages/astro/src/core/messages.ts +++ b/packages/astro/src/core/messages.ts @@ -36,16 +36,19 @@ export function req({ method, statusCode, reqTime, + isRewrite, }: { url: string; statusCode: number; method?: string; reqTime?: number; + isRewrite?: boolean; }): string { const color = statusCode >= 500 ? red : statusCode >= 300 ? yellow : blue; return ( color(`[${statusCode}]`) + ` ` + + `${isRewrite ? color('(rewrite) ') : ''}` + (method && method !== 'GET' ? color(method) + ' ' : '') + url + ` ` + @@ -240,6 +243,7 @@ export function formatConfigErrorMessage(err: ZodError) { // a regex to match the first line of a stack trace const STACK_LINE_REGEXP = /^\s+at /g; const IRRELEVANT_STACK_REGEXP = /node_modules|astro[/\\]dist/g; + function formatErrorStackTrace( err: Error | ErrorWithMetadata, showFullStacktrace: boolean @@ -364,6 +368,7 @@ export function printHelp({ function calculateTablePadding(rows: [string, string][]) { return rows.reduce((val, [first]) => Math.max(val, first.length), 0) + 2; } + const tableEntries = Object.entries(tables); const padding = Math.max(...tableEntries.map(([, rows]) => calculateTablePadding(rows))); for (const [tableTitle, tableRows] of tableEntries) { diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 9f087b621c..18bd367789 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -130,7 +130,7 @@ type HandleRoute = { manifestData: ManifestData; incomingRequest: http.IncomingMessage; incomingResponse: http.ServerResponse; - status?: 404 | 500; + status?: 404 | 500 | 200; pipeline: DevPipeline; }; @@ -232,7 +232,8 @@ export async function handleRoute({ req({ url: pathname, method: incomingRequest.method, - statusCode: status ?? response.status, + statusCode: isRewrite ? response.status : (status ?? response.status), + isRewrite, reqTime: timeEnd - timeStart, }) ); @@ -295,8 +296,9 @@ export async function handleRoute({ await writeSSRResult(request, response, incomingResponse); } -function getStatus(matchedRoute?: MatchedRoute): 404 | 500 | undefined { +function getStatus(matchedRoute?: MatchedRoute): 404 | 500 | 200 { if (!matchedRoute) return 404; if (matchedRoute.route.route === '/404') return 404; if (matchedRoute.route.route === '/500') return 500; + return 200; }