Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NG Srilanka - Improving Startup Performance wit...

NG Srilanka - Improving Startup Performance with Angular Lazy Loading

I've presented this talk at NG-Sri Lanka, first-ever Angular Conference of Sri Lanka. It contains different application loading techniques like Eager Loading, Lazy Loading.

How does preloading help and how we can create custom preloading strategy?

Avatar for Srashti Jain

Srashti Jain

September 14, 2019

Other Decks in Technology

Transcript

  1. 2

  2. 3

  3. 4

  4. 10 const routes: Routes = [ { path: 'user', component:

    UserComponent, pathMatch: 'full' }, { path: 'payment', component: PaymentComponent, pathMatch: 'full' }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] })
  5. “Lazy loading is a technique for loading web content when

    it's needed rather than all at once” 12
  6. 15

  7. 16 const routes: Routes = [ { path: 'user', loadChildren:

    () => import('./user/user.module').then(m => m.UserModule) }, { path: 'payment', loadChildren: () => import('./payment/payment.module').then(m => m.PaymentModule) } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] })
  8. Lazy Loading 19 » Lazy Loading: Loading it later (on

    Demand) » Better Startup Performance » Delay during execution for loading on demand
  9. 20

  10. “Preloading modules in Angular is very similar to lazy loading,

    with the exception that the modules will be loaded immediately after all the eager loaded modules are ready” 22
  11. Preloading 23 » The modules which might be needed later

    - are loaded after the application starts » When the module is actually needed, it’s available immediately
  12. 25 Separate chunk is created for every lazily loaded module

    and it will starts loading after all eagerly loaded modules are loaded
  13. 27 const routes: Routes = [ { path: 'user', loadChildren:

    () => import('./user/user.module').then(m => m.UserModule) }, { path: 'payment', loadChildren: () => import('./payment/payment.module').then(m => m.PaymentModule) } ]; @NgModule({ imports: [RouterModule.forRoot(routes, { preloadingStrategy: NoPreloading })], exports: [RouterModule] })
  14. 28 const routes: Routes = [ { path: 'user', loadChildren:

    () => import('./user/user.module').then(m => m.UserModule) }, { path: 'payment', loadChildren: () => import('./payment/payment.module').then(m => m.PaymentModule) } ]; @NgModule({ imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })], exports: [RouterModule] })
  15. 31 Separate chunk is created for every lazily loaded module

    and we can define which module we want to preload
  16. 33

  17. PROCESS IS TOO EASY Create a Service Implement Preloading Strategy

    into the Service Implement preload method 34
  18. 36 export class CustomLoadingService implements PreloadingStrategy { preload(route: Route, fn:

    () => Observable<any>): Observable<any> { if (route.data && route.data['preload']) { return fn(); } return of(null); } constructor() { } }
  19. 37 const routes: Routes = [ { path: 'user', data:

    { preload: true }, loadChildren: () => import('./user/user.module').then(m => m.UserModule) }, { path: 'payment', loadChildren: () => import('./payment/payment.module').then(m => m.PaymentModule) } ]; @NgModule({ imports: [RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingService })], exports: [RouterModule] })