Lazy loading ships the code anyway if a second file imports it

Quoted from Next.js blog: Ship less JavaScript with AI: However, if any other module in t…
Quote: Next.js blog, "Ship less JavaScript with AI"

Summary

A lazy-loaded component still loads upfront when another module in the same bundle imports it directly, which at Vercel left a 150 KB code editor on most v0 pages.

Check every component behind a lazy boundary for a second importer, using the next-bundle-optimizer skill in Next.js 16.4.

Next.js 16.4 shipped an agent skill that reads Turbopack’s module graph to find JavaScript loading on first render when it was meant to load later. The Next.js blog post names the failure it hunts: a lazy() boundary defers nothing if another module in the same bundle statically imports the same component.

A wrapper like lazy(() => import('./HeavyComponent')) defers that component only while it is the single module importing it. One plain static import of the same path anywhere else in the bundle puts the code back in the initial load, and the boundary around it does nothing. The call site still reads as correct, because the file that broke the deferral is a different file.

Vercel ran the skill against its own front ends and published what it found. On v0, Vercel’s tool for generating app code from prompts, an experimental code editor was loading eagerly on nearly every page, including for users who were not in the experiment. Cutting it saved 150 KB of compressed JavaScript on most pages. Across v0 and vercel.com together, Vercel puts the total saving at close to two megabytes of compressed JavaScript.

The Next.js post frames this as developer tooling, but the cost lands on Core Web Vitals. Initial compressed JavaScript is the part the browser has to download, parse and run before the page answers input, so it surfaces in INP, and in LCP when it competes with the render. A bundle total alone hides the problem, because the kilobytes sit in the build either way and only the import graph says whether they had to arrive before first paint. A case study on Next.js SEO issues on this site recommended dynamic imports to defer below-the-fold components for the same reason. A second static importer is the failure that makes that advice silently stop working.

Running next analyze now opens a route summary that ranks the largest client routes, and inside a route a table sorts every source by size and labels it as initial load, behind an async boundary, or both. A filter hides async-only modules and leaves the critical render path, which is where a component you believed was deferred shows itself. The analyzer saves a snapshot of each run, so diffing two of them measures what a change actually did.

Stuck lazy boundaries are not the only thing the agent looks for. It traces import chains and applies minimum cut, the smallest set of connections whose removal separates one group of modules from the rest, to propose where new boundaries would go, then checks the proposal against the source and how the components are used. It also reports logic that could move to React Server Components, duplicate copies of large dependencies, and code that tree shaking failed to drop.

What to do

  • Upgrade to Next.js 16.4 and confirm the app runs Turbopack, the default bundler since Next.js 16. The analyzer needs its module graph, so a webpack build gets nothing.
  • Install the skill with npx skills add vercel/next.js --skill next-bundle-optimizer and run /next-bundle-optimizer. It reports by default; /next-bundle-optimizer fix applies the changes, reruns the analysis and compares the snapshots.
  • Without an agent, run next analyze, open the largest client route, sort the table by size and filter to the critical render path. Anything you expected to be deferred that appears there has a second importer.
  • For custom tooling, next analyze export writes the module and chunk graphs as JSON.

Watch out for

Automatic fixes move where the code splits, which moves what loads when a user interacts with the page. Vercel had the agent open a pull request per change, verified each one on a preview deployment and then shipped it, and that order is worth copying rather than trusting the diff. The eager editor on v0 also shows that an experiment flag controls rendering rather than bundling. A flagged-off component still ships to every user unless its import sits behind a boundary that nothing else bypasses.