Sub topic 11
Checking the Build Output for Lazy Chunks
This chapter assumes you can define standalone routes, use loadComponent and loadChildren, and run ng build with the Angular CLI. Here you will confirm that your lazy routes produced separate chunks, and learn how to read the build report.
Why verify
A lazy route can look correct in code and still ship in the main bundle. One static import of a lazy component anywhere in the eager code is enough to do this. The build output is the quickest way to find out, because it lists every file the bundler produced and how large each one is.
Running the build
Run a production build:
ng buildRecent Angular CLI versions build for production by default. The CLI prints two groups of files: initial chunks and lazy chunks.
Initial chunk files | Names | Raw size | Estimated transfer size
main-4XQ2K7ZP.js | main | 212.40 kB | 58.31 kB
polyfills-FFHMD2TL.js | polyfills | 34.52 kB | 11.28 kB
styles-5INURTSO.css | styles | 0 bytes | 0 bytes
Lazy chunk files | Names | Raw size | Estimated transfer size
chunk-7ABC12DE.js | admin-routes | 48.10 kB | 11.90 kB
chunk-9KLM34NO.js | home-component| 6.20 kB | 2.10 kBThe exact file names, hashes and sizes will differ in your project. The structure is what matters.
- Initial chunk files are downloaded before the app starts. Smaller is better.
- Lazy chunk files are fetched later, when the router activates the route (or preloads it).
- Raw size is the file size on disk.
- Estimated transfer size is the approximate size after compression.
What to check
Compare the report with your route configuration.
- Each
loadComponentorloadChildrentarget should appear in the lazy chunk list. - The names should match the feature. A chunk named after
admin-routesis the one created byimport('./admin/admin.routes'). - The initial
mainfile should shrink compared with the build before you split the routes. - Shared code, such as a component used by both an eager route and a lazy route, is placed in the initial chunk or in a shared lazy chunk. This is normal.
Note that one dynamic import can produce more than one file when the bundler also extracts code shared with other chunks.
Confirming at runtime
The report shows what was built. The browser shows what is loaded.
- Serve the production build with any static server, for example
npx http-server dist/my-app/browser. The exact output folder depends on your project name and CLI version. - Open the browser developer tools and select the Network tab.
- Filter by JS and reload the page.
- Navigate to the lazy route and watch for a new
chunk-*.jsrequest.
If you use withPreloading(PreloadAllModules), the lazy chunks are requested shortly after startup. That is preloading, not a failure of the split. Without a preloading strategy, the request appears only when you navigate to the route.
Diagnosing a missing chunk
If a route you expected to be lazy does not appear under lazy chunk files, look for these causes.
| Symptom | Likely cause |
|---|---|
| Lazy component's code is inside `main` | A static `import` of that component or its module elsewhere in eager code, such as another routes file or a barrel file |
| No lazy chunks at all | Routes use `component:` instead of `loadComponent:` or `loadChildren:` |
| Service from a lazy area is in `main` | The service is `providedIn: 'root'` and an eager file injects it, or an eager file imports it |
| Chunk is larger than expected | The lazy area imports a large library that you also use elsewhere, or imports through a barrel file that pulls in unrelated code |
To find the offending import, search your eager code for the lazy file's path:
grep -rn "admin-dashboard.component" src/appEvery hit outside the lazy area, other than the dynamic import() itself, is a candidate. Remove or replace it so that only the dynamic import references the file.
Analyzing chunk contents
When a chunk is bigger than you expect, generate stats and inspect them:
ng build --stats-jsonThis writes a stats.json file next to the build output. You can load it into a bundle analyzer, such as esbuild-visualizer or the esbuild bundle analyzer web page, to see which modules each chunk contains. Check that a lazy feature's modules do not also appear in main.
Exercise
After you convert the admin route to loadChildren, ng build shows no admin entry under lazy chunk files, and main grew. What is the most likely cause, and how do you find it?
Show answer
Some eager file still imports admin code statically, for example import { AdminDashboardComponent } from './admin/admin-dashboard.component' in app.routes.ts or through a barrel file. The bundler then keeps that code in the main bundle. Search the eager code for the admin file paths (for example with grep), and remove every static import except the dynamic import() in the route. Rebuild and confirm the chunk appears under lazy chunk files.
Exercise
You see a lazy chunk requested in the Network tab immediately after page load, before you navigate anywhere. Does this mean the split failed?
Show answer
No. If the app uses a preloading strategy such as PreloadAllModules, the router fetches lazy chunks in the background after the initial navigation. The chunk is still separate from main, so the initial download stays small.