Issue 926
feat: a JavaScript bundle budget check that fails a pull request by itself
Back to our planOpen
enhancementcitoolingpriority: medium
Every new client component or dependency can add JavaScript that every neighbor's phone downloads. This evaluates the bundle-size tools, preferring what Next.js now has built in, and sets up the one that passes so a PR that grows the bundle past its budget turns red on its own.
Problem
Next 16 removed the size and "First Load JS" columns from next build, so nothing in our build or CI shows how much JavaScript a route ships, let alone fails on growth.
Proposed fix
- Evaluate once, in this order, record on this issue (maintained? free for our use? runs in our setup? what data it sends out? fits native first?):
- Native:
next analyze(Next 16.1+, Turbopack).next analyze --outputwrites the analysis to.next/diagnostics/analyze,--snapshot <name>names it, and since 16.4next analyze export --snapshot <name> [--route <route>]streams it as JSON Lines without rebuilding. A small script that sums client JavaScript per route from that export and compares it with the budget may be all we need. Check that the export holds per-route client bytes, and thatnext analyzeruns on the CI runner within the job's time. - Well-established open source:
size-limit(MIT, last release weeks ago) with@size-limit/file: measures built files by glob, brotli by default, and fails when over the limit. Its suggested action,andresz1/size-limit-action, comments and fails the PR; its maintenance and data handling were not confirmed. It measures files, not routes, so check it can be pointed at what a route really loads. - HashiCorp
nextjs-bundle-analysis(MPL-2.0): the npm package's last release is from 2023, which fails the "actively maintained" package rule indocs/guidelines/security.md, and its docs say it comments on the PR, not that it fails it. Its support for the App Router and Turbopack builds was not confirmed. Expect it to be ruled out; record why.
- Native:
- Set it up, if one passes. A job
Bundle budgetin.github/workflows/ci.yml, run when the shared perf gate saysbundleis due and always onmain, always reporting a result. Budgets per route from the baseline file, with the headroom rule written once next to the budget file. The log ends with the summary line format the gate log issue counts. - If the native export plus a short script does the job, that script is the only custom code, it lives where
docs/architecture/says scripts go, and it starts with its test.
Acceptance criteria
- Not done: The evaluation is on this issue, five answers per candidate, with links, and what could not be confirmed
- Not done: The chosen check fails a test PR that adds a heavy client dependency, then passes once it's reverted
- Not done: Budgets read from the committed baseline; no number typed into the workflow
- Not done: Runs only when the gate flags
bundle, always onmain, and always reports a result - Not done: Versions pinned; package rules checked;
npm auditclean - Not done:
docs/project.mdlists the check
Files likely touched
.github/workflows/ci.yml, the budget file next to the baseline, a short script (only if the native route is chosen), package.json (only if a package is chosen), docs/project.md.
Must not touch
next.config.* beyond what the analyzer needs. The shared detector's rules.
Open questions
- Should a dependency added only for the admin count against the public budget? Default: no, budget public routes only, admin measured but not gated yet.