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

fix: better logging for rewrites (#11505)

* fix: better logging for rewrites

* fix: better logging for rewrites

* apply feedback
This commit is contained in:
Emanuele Stoppa 2024-07-19 12:45:10 +01:00 committed by GitHub
parent 4db78ae046
commit 8ff7658001
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View file

@ -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
```

View file

@ -36,16 +36,19 @@ export function req({
method, method,
statusCode, statusCode,
reqTime, reqTime,
isRewrite,
}: { }: {
url: string; url: string;
statusCode: number; statusCode: number;
method?: string; method?: string;
reqTime?: number; reqTime?: number;
isRewrite?: boolean;
}): string { }): string {
const color = statusCode >= 500 ? red : statusCode >= 300 ? yellow : blue; const color = statusCode >= 500 ? red : statusCode >= 300 ? yellow : blue;
return ( return (
color(`[${statusCode}]`) + color(`[${statusCode}]`) +
` ` + ` ` +
`${isRewrite ? color('(rewrite) ') : ''}` +
(method && method !== 'GET' ? color(method) + ' ' : '') + (method && method !== 'GET' ? color(method) + ' ' : '') +
url + url +
` ` + ` ` +
@ -240,6 +243,7 @@ export function formatConfigErrorMessage(err: ZodError) {
// a regex to match the first line of a stack trace // a regex to match the first line of a stack trace
const STACK_LINE_REGEXP = /^\s+at /g; const STACK_LINE_REGEXP = /^\s+at /g;
const IRRELEVANT_STACK_REGEXP = /node_modules|astro[/\\]dist/g; const IRRELEVANT_STACK_REGEXP = /node_modules|astro[/\\]dist/g;
function formatErrorStackTrace( function formatErrorStackTrace(
err: Error | ErrorWithMetadata, err: Error | ErrorWithMetadata,
showFullStacktrace: boolean showFullStacktrace: boolean
@ -364,6 +368,7 @@ export function printHelp({
function calculateTablePadding(rows: [string, string][]) { function calculateTablePadding(rows: [string, string][]) {
return rows.reduce((val, [first]) => Math.max(val, first.length), 0) + 2; return rows.reduce((val, [first]) => Math.max(val, first.length), 0) + 2;
} }
const tableEntries = Object.entries(tables); const tableEntries = Object.entries(tables);
const padding = Math.max(...tableEntries.map(([, rows]) => calculateTablePadding(rows))); const padding = Math.max(...tableEntries.map(([, rows]) => calculateTablePadding(rows)));
for (const [tableTitle, tableRows] of tableEntries) { for (const [tableTitle, tableRows] of tableEntries) {

View file

@ -130,7 +130,7 @@ type HandleRoute = {
manifestData: ManifestData; manifestData: ManifestData;
incomingRequest: http.IncomingMessage; incomingRequest: http.IncomingMessage;
incomingResponse: http.ServerResponse; incomingResponse: http.ServerResponse;
status?: 404 | 500; status?: 404 | 500 | 200;
pipeline: DevPipeline; pipeline: DevPipeline;
}; };
@ -232,7 +232,8 @@ export async function handleRoute({
req({ req({
url: pathname, url: pathname,
method: incomingRequest.method, method: incomingRequest.method,
statusCode: status ?? response.status, statusCode: isRewrite ? response.status : (status ?? response.status),
isRewrite,
reqTime: timeEnd - timeStart, reqTime: timeEnd - timeStart,
}) })
); );
@ -295,8 +296,9 @@ export async function handleRoute({
await writeSSRResult(request, response, incomingResponse); 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) return 404;
if (matchedRoute.route.route === '/404') return 404; if (matchedRoute.route.route === '/404') return 404;
if (matchedRoute.route.route === '/500') return 500; if (matchedRoute.route.route === '/500') return 500;
return 200;
} }