Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better logging for rewrites #11505

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/blue-jars-hang.md
Original file line number Diff line number Diff line change
@@ -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 → 200] /foo/about 200ms
08:22:13 [200 → rewrite → 404] /about 23ms
```
12 changes: 11 additions & 1 deletion packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,23 @@ export function req({
method,
statusCode,
reqTime,
formerStatusCode,
}: {
url: string;
statusCode: number;
method?: string;
reqTime?: number;
formerStatusCode?: number;
}): string {
const color = statusCode >= 500 ? red : statusCode >= 300 ? yellow : blue;
let statusCodeLabel;
if (formerStatusCode) {
statusCodeLabel = color(`[${formerStatusCode} → rewrite → ${statusCode}]`);
} else {
statusCodeLabel = color(`[${statusCode}]`);
}
return (
color(`[${statusCode}]`) +
statusCodeLabel +
` ` +
(method && method !== 'GET' ? color(method) + ' ' : '') +
url +
Expand Down Expand Up @@ -240,6 +248,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
Expand Down Expand Up @@ -364,6 +373,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) {
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type HandleRoute = {
manifestData: ManifestData;
incomingRequest: http.IncomingMessage;
incomingResponse: http.ServerResponse;
status?: 404 | 500;
status?: 404 | 500 | 200;
ematipico marked this conversation as resolved.
Show resolved Hide resolved
pipeline: DevPipeline;
};

Expand Down Expand Up @@ -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),
formerStatusCode: isRewrite ? status : undefined,
reqTime: timeEnd - timeStart,
})
);
Expand Down Expand Up @@ -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;
}
Loading