Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"lint": "astro check",
"preview": "astro preview --node",
"astro": "astro",
"prepare": "husky"
"prepare": "husky",
"check:images": "tsx scripts/check-image-size.ts"
},
"lint-staged": {
"*.{ts,tsx,js,jsx,json,astro}": "prettier --write src/"
"*.{ts,tsx,js,jsx,json,astro}": "prettier --write src/",
"*.{jpg,jpeg,png,webp,avif}": "tsx scripts/check-image-size.ts"
Comment thread
JeanneGrenet marked this conversation as resolved.
},
"dependencies": {
"@astrojs/check": "0.9.8",
Expand Down
48 changes: 48 additions & 0 deletions scripts/check-image-size.ts

@houssembaltii houssembaltii Mar 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me 500 is too generic, in the project we have images for :

  • avatars
  • hero sections
  • images for podcast
  • images used in the news

i think 500 is great for avatars but for hero section or images used in the news/podcast i feel we can easily surpass the 500 KB threshold .
perhaps we can tolerate more in size depending on the image purpose

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { statSync } from "node:fs";

const DEFAULT_MAX_KB = 500;

const SIZE_LIMITS: { pattern: string; maxKB: number }[] = [
{ pattern: "src/content/people/", maxKB: 500 },
{ pattern: "src/content/partners/", maxKB: 500 },
{ pattern: "src/content/news/", maxKB: 1000 },
{ pattern: "src/content/podcasts/", maxKB: 1000 },
{ pattern: "src/content/events/", maxKB: 2000 },
{ pattern: "src/content/for-kids-events/", maxKB: 1000 },
{ pattern: "src/content/for-kids-workshops/", maxKB: 1000 },
{ pattern: "src/assets/images/", maxKB: 1000 },
];

function getMaxKB(filePath: string): number {
const match = SIZE_LIMITS.find((limit) => filePath.includes(limit.pattern));
return match?.maxKB ?? DEFAULT_MAX_KB;
}

const files = process.argv.slice(2);
const errors: string[] = [];

for (const file of files) {
const maxKB = getMaxKB(file);
const stats = statSync(file);
const sizeKB = Math.round(stats.size / 1024);
if (stats.size > maxKB * 1024) {
errors.push(` ${file} (${sizeKB} KB > ${maxKB} KB)`);
}
}

if (errors.length > 0) {
console.error(`\n❌ The following images exceed their size limit:\n`);
for (const error of errors) {
console.error(error);
}
console.error(
"\nSize limits:" +
`\n People, partners: 500 KB` +
`\n News, podcasts, hero: 1000 KB` +
`\n Events: 2000 KB` +
`\n Other images: ${DEFAULT_MAX_KB} KB` +
"\n\nPlease compress or resize these images before committing." +
"\nYou can use tools like https://squoosh.app\n",
);
process.exit(1);
}