Update: A reader pointed out that Next.js 16.4 shipped an official bundle analyzer with snapshot diffs and an AI optimizer skill. It’s the right tool for finding why a route is heavy, and honestly, a great base for a gate too. So the tool in this post is now bundle-gate 0.1.0, built right on top of
next analyze. What’s new: a failing check names the modules that grew, all limits live in one config as rules with globs, the GitHub Action compares with the merge base in one job, it skips with a warning instead of failing for nothing, and on a breach it uploads the analyzer report. It lives on GitHub and npm. More about it in The investigator below.
So, AI-assisted coding or vibe coding is a fact of life now, but I’d rather not tell you guys about yet another “AI slop”. However, it can lead to some interesting results, and it happened to me.
In one PR, I got some changes that looked good at first glance, my own review didn’t notice any issues, but one of my checks on the PR did. One page could swell up by 153 KiB, while the new code was only 7 KiB. So, in this article I’ll tell you guys how I implemented a size guard, and what issues I faced during development.
In the previous article, I made some significant improvements. Honestly, I make some speed improvements constantly, some smaller, some bigger, but it is a permanent process of improvement. Well, if you improve something, you really want to keep it improved. But sometimes I noticed some degradation in my dashboards.
After a couple of analyses, I found out that all the degradations were related to a growing bundle size. I decided to keep an eye on it and check how other people do it.

Usually people do it manually, or use nextjs-bundle-analysis, or something like that. It didn’t fit for me: it only reads Pages Router routes, and I needed App Router support. There wasn’t the right tool for me.
The workaround
Let’s start with the bad news: Next.js removed the sizes, the First Load JS stat and the manifests. According to their statements, it wasn’t showing actual sizes for webpack and Turbopack.
So, here is what I had: @next/bundle-analyzer, which worked only for webpack builds, and next experimental-analyze, which is only for human analysis. It was impossible to create any pipelines based on those libs.
Well, then Next.js 16.4 came out, and next analyze learned to export its data. Routes, chunks, modules, gzip sizes, all the data I needed, and this time it is official. BOOM, now you can build a gate on top of it!
I built a small CLI that solves this: it runs the analyzer, compares two snapshots and checks my limits. No rocket science, a tool that solves my issues.
I’d like to share with you guys how it works in a few words.
There are just two commands inside:
snapshotrunsnext analyzeitself (nonext buildneeded), reads its export and saves the client JS and CSS that every route loads up front, gzip, into a small file. Async chunks are listed, but never judged;diffcompares two snapshots, applies my limits, names the modules that grew, and fails with an error if a route is over its limit.
There is nothing to install, npx is enough:
npx bundle-gate snapshot --name base --out base.json.gz
npx bundle-gate snapshot --name head --out head.json.gz
npx bundle-gate diff base.json.gz head.json.gz
The limits are rules in one bundle-gate.config.json: a route name or a glob (*, **, ?) and a limit like +30 KiB, +10% or <= 400 KiB. The first rule that matches wins, all terms in one limit must hold, and a route without a rule gets +2%:
{
"rules": [
{ "match": ["/admin/**", "/_not-found"], "limit": "ignore" },
{ "match": "/blog/[slug]", "limit": "+20%, <= 300 KiB" }
]
}
If something goes wrong, like the analyzer changed its export format, the tool checks the schema_version and stops with an error. And if two snapshots can’t be compared fairly, like different Next.js versions, diff just skips with a warning and exit code 0. Same in the Action: if the base can’t be analyzed, it warns and passes. Honestly, a skip is better than a false failure.
The baseline problem
Well, my first version worked as simply as possible, look:
- Run the size check in every preview, and keep artifacts in GitHub;
- In a feature PR I run a comparison and create some report.
But it led to issues with what I was actually comparing.
Two broken parts:
- Artifacts for the latest main can be missing;
- A PR is not rebased onto the latest main every time.

So, this is the real world: a main run can fail before it uploads artifacts, and a branch is not always up to date with main.
Let’s imagine that main moved ahead by several commits, and one of those commits reduced the bundle size. Your branch will have a failed check, but you didn’t do anything wrong, you just didn’t have those commits in your branch.
It is especially hard to explain in the new AI world, where many people are new to coding with AI tools. They try to code something, make some prototypes, or implement something that they have in mind, and a failure that is not their fault only confuses them.
About the numbers: roughly half of the v1 failures were false positives. Branches in sync with main failed in 5.7% of checks, branches 20+ commits behind — in 43%.
Well, based on my experience, I decided to migrate to a merge-base flow. So, here is how it works now, all in one job:
- Take the PR merge commit from
actions/checkout: the PR’s latest commit is head, and the point where the branch left main is the base; - Check out both commits into two git worktrees and run
snapshotin each; - Run
diff-> write the result to the job summary -> fail the check with an error annotation if needed.
No cache, both sides run in the same job with the same environment. So the same PR gives the same numbers whenever it runs, and new commits in main don’t change anything. The only thing you need is fetch-depth: 0 in checkout, because the merge base needs the full history.

So, let’s move on to what I caught!
The catch
Well. What happened?
I had a PR with some obvious changes, and most of those changes were written by an agent. As part of a new feature, the agent extracted a new module and exported it through the barrel; it is one of the rules of the FSD architecture. So, 4 files got this import:
import { formatPrice } from '@/shared/lib/utils'
In another PR, some changes to the style pages were merged into main. Updating the PR with main led to this:

And honestly, it was just a coincidence. Neither of those PRs did anything wrong, both changes were valid separately. But together — BOOM.
Next step: I asked the agent to read that report and find the problem. And honestly, agents are so good at that part of debugging.
It parsed the bundles, found that the barrel pulled heavy code into the style pages, and fixed only 4 imports! Type-only imports stayed on the barrel, TypeScript erases them anyway. On review, I moved the module into its own slice, so the FSD rule is kept and the bundle stays light:
- import { formatPrice } from '@/shared/lib/utils'
+ import { formatPrice } from '@/shared/lib/price'
How bundle size looked on each step:

What was wrong initially? Source maps showed that the same modules were shared between entry points, and some of them ended up in the bundle twice (I guess that’s how resolving circular dependencies works).

So, without a bundle size guard, my application overall would have grown by 200 KiB. Of course, I would have noticed it from alerts, but it would have been in production for some time.
The wins
Now, I’ve just shown one side of that bundle size check. Let’s take a look at the second side.
My new approach to watching the bundle helps me not only gate the bad changes, it also helps me notice the good ones.
I have a good example for you guys: for some time my app had 2 different versions of the UI, it was impossible to separate them properly, and of course it led to some degradations.
After a huge amount of work and several experiments, I moved to the new one, and after that I decided to remove the old UI parts and check what changed. I deleted 220 files and 19,354 lines of code: /projects lost a quarter of its first-load JS (-26%), and 22 routes got lighter.

Well, now I have good proof of my work and of how refactoring can influence user experience. Not only “features” make user experience better.
The investigator
bundle-gate shows what happened, and next analyze shows why.
A pull request imports all of lodash on the about page. bundle-gate fails the check, and the output already tells you who did it:
bundle-gate Next 16.4.6 · eager client JS (gzip)
base -> head
✗ /about 167.3 KiB -> 191.79 KiB +24.49 KiB +14.64%
rule default (+2%): grew +24.49 KiB (+14.64%), limit +2%
+ lodash +24.38 KiB
+ app/about/page.js +104 B
1 failed · 1 changed · 0 new · 0 removed · 5 unchanged (5 hidden, --all shows them)
Want to dig deeper? On a breach, the Action uploads the artifact bundle-gate-analyzer-report with the official analyzer report for both base and head. Download it from the workflow run, unzip it, run npx serve <folder>, open /compare and pick the base and head snapshots.
The treemap shows the cause right away: lodash.js, added.

Fix it yourself, or let your AI agent do it with Vercel’s next-bundle-optimizer skill.
Turning it on is a few lines in a pull_request workflow:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 24
- uses: bra31k/bundle-gate@v0.1.0
with:
working-directory: apps/web
The package
After running into these problems during development, I built a gate on top of next analyze, and now I decided to open-source it.
Where it lives now:
Well, what is it? It is the CLI tool I wrote to solve these problems, and now I decided to share it. No install step, just npx bundle-gate. It works with App Router and Pages Router.
Of course, there are some nuances: it needs Next.js 16.4+ and Node.js 22.18+; the analyzer always builds with Turbopack, so if you ship a webpack build, the numbers describe a Turbopack build of the same code; the analyzer itself is still experimental; only eager client JS and CSS are judged (I’m pretty sure for now it is more important, but it may not be enough for somebody more nerdy than me!); the Action needs the PR merge commit, so no push events and no PRs with merge conflicts; it compares with the merge base, so it won’t catch a problem that shows up only together with newer commits in main; and a renamed route shows up as one removed and one new route.
So guys, thank you for your time! Please feel free to take a look at it, leave issues, test it! I hope it will be helpful!
How I measured: all numbers in the cases come from real builds of my app.