Sub topic 1
Why split the bundle
Prerequisites
This chapter assumes you can do the following:
- Build a standalone Angular component.
- Define routes with the Angular Router.
- Read TypeScript
importandexportstatements. - Run
ng buildwith the Angular CLI.
The cost of one big bundle
When you build an Angular app for production, the build tool (the bundler) combines your code and its dependencies into JavaScript files. If you route everything through static imports, the bundler puts it all into one main bundle. The browser must download, parse, and run that file before the user sees anything useful.
A bundle's cost has three parts:
- Download time. More bytes take longer on slow or mobile networks.
- Parse and compile time. The browser must process every byte, even code the user never runs.
- Startup work. More code means more to initialize before the first screen is interactive.
An admin area, a reports page, or a rarely used settings screen may be 40% of your code. Most visitors never open them. Shipping them to everyone makes the first load slower for no benefit.
What splitting does
Code splitting divides the output into several files, called chunks. The main chunk holds what the first screen needs. Other chunks hold code for features the user may reach later. The browser fetches a chunk only when the app asks for it.
In Angular, the natural split point is the route. A route is a clear boundary: the user either navigates there or does not. Lazy loading means the router downloads the code for a route the first time that route is activated.
How the bundler knows where to split
A static import is resolved at build time and always lands in the same file as the code that imports it:
import { AdminDashboardComponent } from './admin/admin-dashboard.component';A dynamic import() is an expression that returns a Promise. The bundler treats its argument as a boundary and emits the target file, with its own dependencies, as a separate chunk:
const loadAdmin = () =>
import('./admin/admin-dashboard.component').then(m => m.AdminDashboardComponent);Nothing is downloaded until loadAdmin() runs. Later chapters show how the router calls such functions through loadComponent and loadChildren. For now, remember the rule: one static import anywhere in the main chunk pulls the code back into the main chunk. A dynamic import only helps if nothing else imports that code statically.
The trade-off
Lazy loading is not free. It moves cost from startup to navigation.
| Aspect | Eager | Lazy |
|---|---|---|
| Initial load | Slower, larger bundle | Faster, smaller bundle |
| First navigation to a feature | Instant | Waits for a network request |
| Number of requests | Few | More |
| Failure mode | One failed download | A chunk can fail later, for example after the user goes offline |
The wait on first navigation can be reduced by preloading, which fetches lazy chunks in the background after startup. A later chapter covers it.
Deciding what to split
Good candidates for lazy loading:
- Features behind a login, such as admin areas.
- Pages most users rarely visit.
- Large pages with heavy dependencies, such as charting or editors.
Poor candidates:
- The landing page and anything needed for the first render.
- Tiny components. The extra request can cost more than the bytes saved.
A practical approach is to measure first. Run ng build and look at the size of the main bundle in the output. If the initial total is large and clearly contains features most users skip, split along those routes.
What comes next
The next chapters refresh how routes are configured, then show how loadComponent and loadChildren turn route entries into split points.
Exercise
Your app has a home page, a reports page with a large charting library, and a settings page that is a small form. Which routes are the best candidates for lazy loading, and why? What happens if home.component.ts also statically imports a helper from the reports folder?
Show answer
The reports route is the best candidate because the charting library adds many bytes that most visitors never need at startup. The settings page may be worth splitting only if it is rarely visited, but its small size means the saving is minor. The home page should stay eager because it is needed for the first render. If home.component.ts statically imports from the reports folder, the bundler must place that imported code in the main chunk, so the reports code (and anything it imports) is no longer fully lazy.
Exercise
Name two costs a browser pays for JavaScript it never uses, and one new cost that lazy loading introduces.
Show answer
The browser pays download time and parse/compile time for unused code. Lazy loading adds a network request, and therefore a possible wait or failure, the first time the user navigates to a lazy route.