Performance: Lazy loading modules with routes
This affects the initial page load speed by deferring loading of feature modules until their routes are accessed.
const routes: Routes = [ { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) }, { path: 'dashboard', component: DashboardComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
import { FeatureModule } from './feature/feature.module'; const routes: Routes = [ { path: 'feature', loadChildren: () => FeatureModule }, { path: 'dashboard', component: DashboardComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager loading all modules | High (all components loaded) | Multiple (due to large DOM) | High (large initial paint) | [X] Bad |
| Lazy loading modules with routes | Low initially (only active route DOM) | Single or few (on route change) | Low initial paint cost | [OK] Good |
AdminModule in Angular routes?{ path: 'shop', loadChildren: () => import('./shop/shop.module').then(m => m.ShopModule) }/shop for the first time?{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }ProfileModule only when the user navigates to /profile. The module has child routes. Which is the best way to configure the main app routing to achieve this and ensure accessibility and performance?Pass the test and this pattern moves unknown