Skip to content

Commit

Permalink
feat: log slow pages in red (#11507)
Browse files Browse the repository at this point in the history
* fix: log slow pages in red

* apply feedback

* chore: update based on feedback

* Update .changeset/spotty-rice-shake.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
3 people authored Jul 31, 2024
1 parent 2cf770d commit a62345f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/spotty-rice-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'astro': minor
---

Adds color-coding to the console output during the build to highlight slow pages.

Pages that take more than 500 milliseconds to render will have their build time logged in red. This change can help you discover pages of your site that are not performant and may need attention.
12 changes: 10 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs';
import os from 'node:os';
import { fileURLToPath } from 'node:url';
import { bgGreen, black, blue, bold, dim, green, magenta } from 'kleur/colors';
import { bgGreen, black, blue, bold, dim, green, magenta, red } from 'kleur/colors';
import PQueue from 'p-queue';
import type { OutputAsset, OutputChunk } from 'rollup';
import type {
Expand Down Expand Up @@ -192,6 +192,8 @@ export async function generatePages(options: StaticBuildOptions, internals: Buil
await runHookBuildGenerated({ config, logger });
}

const THRESHOLD_SLOW_RENDER_TIME_MS = 500;

async function generatePage(
pageData: PageBuildData,
ssrEntry: SinglePageBuiltModule,
Expand Down Expand Up @@ -244,7 +246,13 @@ async function generatePage(
const timeEnd = performance.now();
const timeChange = getTimeStat(prevTimeEnd, timeEnd);
const timeIncrease = `(+${timeChange})`;
logger.info('SKIP_FORMAT', ` ${dim(timeIncrease)}`);
let timeIncreaseLabel;
if (timeEnd - prevTimeEnd > THRESHOLD_SLOW_RENDER_TIME_MS) {
timeIncreaseLabel = red(timeIncrease);
} else {
timeIncreaseLabel = dim(timeIncrease);
}
logger.info('SKIP_FORMAT', ` ${timeIncreaseLabel}`);
prevTimeEnd = timeEnd;
}
}
Expand Down

0 comments on commit a62345f

Please sign in to comment.