0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -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,
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) {

View file

@ -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;
}