Skip to content

Commit

Permalink
Merge branch 'main' into sarah11918-patch-4
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah11918 authored Jul 19, 2024
2 parents a98e65b + df89cfd commit 39919a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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) /foo/about 200ms
08:22:13 [200] (rewrite) /about 23ms
```
5 changes: 5 additions & 0 deletions packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
` ` +
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
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;
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,
isRewrite,
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;
}

0 comments on commit 39919a9

Please sign in to comment.