Split asset budgets into transfer and storage and gate them in check:full
The new dragon illustrations pushed the cold home page to 1,754,467 bytes, over its 1.75 MB budget. The home dragon is re-encoded at quality 88 and the sidebar dragon resized to 268 px, twice its displayed width, at quality 92, with SSIM above 0.994: the home page is back to about 1.71 MB. The 25 MB cap measured the images stored in the repository, which are not downloaded on each visit. The profile now separates transfer budgets (home, Aniimo, CSS gzip at 100 kB pending the CSS split) from storage, which reports images per folder and caps each file at 1 MB. npm run test:assets applies them in check:full on the production build, so a breach blocks delivery. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f02c761a8
commit
26984bb6c0
8 changed files with 79 additions and 25 deletions
|
|
@ -1,7 +1,7 @@
|
|||
// Rôle : mesure les octets chargés par l'accueil et Aniimo sur le build servi par le serveur Node.
|
||||
// Rôle : mesure les octets chargés par l'accueil et Aniimo sur le build servi par le serveur Node, et le stock d'images du dépôt.
|
||||
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
||||
import { platform } from "node:os";
|
||||
import { basename, join } from "node:path";
|
||||
import { basename, join, relative, sep } from "node:path";
|
||||
import { gzipSync } from "node:zlib";
|
||||
import assert from "node:assert/strict";
|
||||
import { chromium } from "@playwright/test";
|
||||
|
|
@ -9,14 +9,21 @@ import { createStaticServer } from "../server.mjs";
|
|||
|
||||
const label = String(process.argv[2] || "current").replace(/[^a-z0-9_-]+/gi, "-");
|
||||
const viewport = { width: 1440, height: 900 };
|
||||
// Deux familles distinctes : ce qu'un visiteur télécharge (transfert par route, CSS), et ce que le dépôt livre sans que tout
|
||||
// soit téléchargé (stock d'images). Le stock grandit avec chaque collection Sokko TCG : seul le poids de chaque fichier est plafonné.
|
||||
const budgets = {
|
||||
publicImageBytes: 25_000_000,
|
||||
cssGzipBytes: 70_000,
|
||||
routeTransferBytes: {
|
||||
"home-cold": 1_750_000,
|
||||
"aniimo-after-home": 3_000_000
|
||||
transfer: {
|
||||
cssGzipBytes: 100_000,
|
||||
routeBytes: {
|
||||
"home-cold": 1_750_000,
|
||||
"aniimo-after-home": 3_000_000
|
||||
}
|
||||
},
|
||||
storage: {
|
||||
maxImageFileBytes: 1_000_000
|
||||
}
|
||||
};
|
||||
const IMAGE_ROOT = "website/public/static/img";
|
||||
|
||||
async function listFiles(root) {
|
||||
const entries = await readdir(root, { withFileTypes: true });
|
||||
|
|
@ -62,7 +69,15 @@ async function routeReport(page, { name, route, selector, includeDocument }) {
|
|||
return { name, route, ...metrics, imageElements: images };
|
||||
}
|
||||
|
||||
const imageFiles = await listFiles("website/public/static/img");
|
||||
const imageFiles = await Promise.all((await listFiles(IMAGE_ROOT)).map(async (file) => ({ file, bytes: (await stat(file)).size })));
|
||||
// Répartition par premier dossier : les images à la racine forment le groupe « (racine) ».
|
||||
const imageAreas = Object.entries(imageFiles.reduce((areas, { file, bytes }) => {
|
||||
const parts = relative(IMAGE_ROOT, file).split(sep);
|
||||
const area = parts.length > 1 ? parts[0] : "(racine)";
|
||||
areas[area] = { files: (areas[area]?.files || 0) + 1, bytes: (areas[area]?.bytes || 0) + bytes };
|
||||
return areas;
|
||||
}, {})).sort(([, left], [, right]) => right.bytes - left.bytes).map(([area, totals]) => ({ area, ...totals }));
|
||||
const oversizedImages = imageFiles.filter(({ bytes }) => bytes > budgets.storage.maxImageFileBytes).map(({ file, bytes }) => ({ file: relative(IMAGE_ROOT, file), bytes }));
|
||||
const cssFiles = (await listFiles("website/dist/assets")).filter((file) => file.endsWith(".css"));
|
||||
const server = createStaticServer();
|
||||
await new Promise((resolve, reject) => {
|
||||
|
|
@ -110,7 +125,9 @@ try {
|
|||
budgets,
|
||||
repository: {
|
||||
publicImageFiles: imageFiles.length,
|
||||
publicImageBytes: (await Promise.all(imageFiles.map((file) => stat(file)))).reduce((sum, entry) => sum + entry.size, 0),
|
||||
publicImageBytes: imageFiles.reduce((sum, { bytes }) => sum + bytes, 0),
|
||||
imageAreas,
|
||||
largestImages: [...imageFiles].sort((left, right) => right.bytes - left.bytes).slice(0, 5).map(({ file, bytes }) => ({ file: relative(IMAGE_ROOT, file), bytes })),
|
||||
css
|
||||
},
|
||||
routes
|
||||
|
|
@ -126,13 +143,14 @@ try {
|
|||
imageTransferBytes: route.images.transferBytes
|
||||
})));
|
||||
console.log(`CSS: ${css.map((entry) => `${entry.file} ${entry.bytes} bytes (${entry.gzipBytes} gzip)`).join(", ")}`);
|
||||
console.log(`Public images: ${report.repository.publicImageFiles} files, ${report.repository.publicImageBytes} bytes`);
|
||||
console.log(`Stored images (not a download): ${report.repository.publicImageFiles} files, ${report.repository.publicImageBytes} bytes`);
|
||||
console.table(imageAreas);
|
||||
console.log(`Report: ${output}`);
|
||||
assert.ok(report.repository.publicImageBytes <= budgets.publicImageBytes, `Public images exceed ${budgets.publicImageBytes} bytes`);
|
||||
assert.ok(css.every((entry) => entry.gzipBytes <= budgets.cssGzipBytes), `A CSS bundle exceeds ${budgets.cssGzipBytes} gzip bytes`);
|
||||
assert.ok(css.every((entry) => entry.gzipBytes <= budgets.transfer.cssGzipBytes), `A CSS bundle exceeds ${budgets.transfer.cssGzipBytes} gzip bytes`);
|
||||
for (const route of routes) {
|
||||
assert.ok(route.transferBytes <= budgets.routeTransferBytes[route.name], `${route.name} exceeds ${budgets.routeTransferBytes[route.name]} transfer bytes`);
|
||||
assert.ok(route.transferBytes <= budgets.transfer.routeBytes[route.name], `${route.name} exceeds ${budgets.transfer.routeBytes[route.name]} transfer bytes`);
|
||||
}
|
||||
assert.deepEqual(oversizedImages, [], `Images over ${budgets.storage.maxImageFileBytes} bytes: convert or resize them (docs/PERFORMANCE.md)`);
|
||||
} finally {
|
||||
await browser?.close();
|
||||
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue