How Lazy Loading Works
Instead of bundling the entire application into a single JavaScript file, Angular’s build process creates separate bundles for each lazy-loaded module:src/app/app-routing.module.ts
The
import() syntax is a JavaScript dynamic import that returns a Promise. Angular uses this to load modules asynchronously when the route is activated.Loading Flow
Initial page load
Browser downloads:
main.js(AppModule, CoreModule, LayoutModule)polyfills.js(browser compatibility)runtime.js(webpack module loader)styles.css(global styles)
User navigates to /favorites
Browser downloads:
favorites-module.js(FavoritesModule + components)- Any dependencies not already loaded
Module instantiation
Angular:
- Instantiates the FavoritesModule
- Creates component instances
- Renders the favorites page
Build Output Example
When you build ScreenPulse, you’ll see separate chunk files:Performance Benefits
Without Lazy Loading
With Lazy Loading
Faster Initial Load
Users only download the code for the landing page, not the entire app
Reduced Bundle Size
Main bundle is 40-60% smaller with proper lazy loading
Better Caching
Browsers cache feature bundles independently - updates to one feature don’t invalidate all caches
Improved User Experience
Faster time-to-interactive means users can start using the app sooner
Code Splitting Strategy
ScreenPulse splits code at the feature level:What Goes in the Initial Bundle
AppModule
AppModule
- Root component (AppComponent)
- Router configuration
- Global providers (interceptors)
CoreModule
CoreModule
- Authentication services (though these use
providedIn: 'root') - Guards and interceptors (registered in AppModule)
LayoutModule
LayoutModule
- Navbar component
- Footer component
- Application shell that’s visible on every page
Third-party libraries
Third-party libraries
- Angular core packages
- RxJS
- Angular Material core
What’s Lazy Loaded
- SearchModule: Search page, search bar, carousel, results table
- FavoritesModule: Favorites page, favorites cards, sorting controls
- AuthModule: Login/register pages, auth forms
- Feature-specific Material modules: Only load Material components when needed
Preloading Strategies
Angular can preload lazy modules in the background after the initial render:src/app/app-routing.module.ts
Preloading Options
- NoPreloading (default)
- PreloadAllModules
- Custom Strategy
Load modules only when user navigates to the routeBest for: Apps with many large features that most users won’t visit
Optimizing Lazy Loading
Keep the initial bundle small
Only include essential services and components in CoreModule and LayoutModule
Avoid importing feature modules directly
Never import a lazy-loaded module in AppModule - only use
loadChildrenUse SharedModule wisely
Put truly shared components in SharedModule, but don’t bloat it with feature-specific code
Common Pitfalls
Testing Lazy Loading
Verify lazy loading in Chrome DevTools:
You should see files like
favorites-module.[hash].js downloaded only when navigating to /favorites.
Next Steps
Routing
Learn about route configuration and guards
Module Structure
Understand how modules are organized
Build Configuration
Optimize production builds
Performance
Additional performance optimization techniques