Put Payload Limits in the Pipeline: Why Size Budgets Should Fail Builds
Photo by Matthew Fournier on Unsplash
Payload bloat rarely arrives as a dramatic commit. It shows up as a 40KB hero image, an extra font weight, a dependency that pulls in a date library, a JSON response with three nested objects nobody uses. Each change is small enough to pass review. Together they turn a fast page into a slow one. The place to stop that pattern is CI.
A budget is a product decision, not a lint rule
Performance budgets are often treated as nice-to-have dashboards. That is backwards. If you know a page should load on a mid-range phone over LTE, you have a payload limit. Put it where code meets main: the pipeline. A budget turns a vague preference into a check. It also forces trade-offs into the open. If a new feature needs 30KB more JavaScript, someone has to decide whether it is worth it. That is a healthy conversation. The alternative is a quarterly performance review where everyone agrees the site feels slower and nobody knows which of the last 400 commits did it.
What to budget
Budget the things users download or wait for. JavaScript and CSS after compression. Images by format and dimension. Font files total. The HTML shell. API response fixtures used by the frontend. For server-rendered apps, include critical CSS. For mobile, maybe separate budgets. Example starting points for a content site: main JS 150KB compressed, CSS 40KB, fonts 100KB total, largest image 200KB, API JSON 50KB. A dashboard app will need more. The numbers should come from your baseline and your performance goals, not from a random blog post. The point is to pick a number, measure against it, and adjust with evidence.
Absolute ceilings and delta checks
Use two checks. An absolute ceiling catches the image that is 4MB because someone exported it at 6000px. A delta check catches the slow creep: this PR adds 12KB to the main bundle. Warn on small deltas, fail on hard ceilings. Ratchet budgets down when you make progress. If you cut 50KB, lower the budget so it does not grow back. Keep exceptions explicit and temporary. A comment in a config file is not a strategy. An override that never expires is just a new budget with extra steps.
Make the checks fast
CI budgets fail when they are slow or noisy. Run checks in parallel with tests. Cache dependencies and build artifacts. Measure compressed sizes with the same tool versions every time, because Brotli and minifier output can vary. Use existing tools where they fit: size-limit for bundles, Lighthouse CI for page weight, custom scripts for fonts and images. For API responses, serialize a fixture in a test and check the byte size. Keep the failure message useful: file, previous size, new size, budget, and a command to inspect. A red X with no context just teaches people to bypass the check.
Do not let budgets become a gatekeeping exercise
The fastest way to kill a budget is to make it block unrelated work. Start in warn-only mode for a couple of weeks. Fix the baseline. Then turn on failures for regressions. Allow a documented override for emergencies, but make it visible and expiring. Review thresholds quarterly. A budget that has not changed in a year is probably either too loose or ignored. The people shipping features should understand why the check exists, not just learn to hate the bot that posts the same link every Tuesday.
Cover the boring stuff
Images are the most common offender. Check that uploads are resized and converted to modern formats before they hit the repo. Fonts should be subset and WOFF2 only. Docker images should have a size ceiling too, because deploy time is payload time. API responses should not include fields the client never reads. These checks are not glamorous, but they are where the bytes are. A 300KB image that should be 80KB is not a design decision. It is an export setting that nobody noticed.
A size budget in CI is a promise that the next feature will not quietly cost users a second of load time. It is not about shaming anyone. It is about making the cost visible before it ships. Put the number in the pipeline, keep the failure message human, and let the build do the nagging.