|
| 1 | +--- |
| 2 | +title: Preloading strategy |
| 3 | +description: "How to implement a module preloading strategy" |
| 4 | +tags: ["angular", "modules", "preloading", "router"] |
| 5 | +pubDate: Mar 8, 2023 |
| 6 | +contributedBy: "@rubenperegrina" |
| 7 | +--- |
| 8 | + |
| 9 | +If we have our application separated by modules and we apply lazy loading. |
| 10 | +Angular by default will load the modules as the user needs them. |
| 11 | +There are several ways to control the loading of modules. |
| 12 | + |
| 13 | +### Lazy loading |
| 14 | + |
| 15 | +Modules will be loaded as the user requires them. |
| 16 | + |
| 17 | +```typescript |
| 18 | +const routes: Routes = [ |
| 19 | + { |
| 20 | + path: "contact", |
| 21 | + loadChildren: () => |
| 22 | + import("./contact/contact.module").then((m) => m.ContactModule), |
| 23 | + }, |
| 24 | + { |
| 25 | + path: "about", |
| 26 | + loadChildren: () => |
| 27 | + import("./about/about.module").then((m) => m.AboutModule), |
| 28 | + }, |
| 29 | +]; |
| 30 | + |
| 31 | +@NgModule({ |
| 32 | + imports: [RouterModule.forRoot(routes)], |
| 33 | + exports: [RouterModule], |
| 34 | +}) |
| 35 | +export class AppRoutingModule {} |
| 36 | + |
| 37 | +//StandAlone API version |
| 38 | + |
| 39 | +//Lazy loading another routing config |
| 40 | +export const routes: Routes = [ |
| 41 | + { |
| 42 | + path: "contact", |
| 43 | + loadChildren: () => |
| 44 | + import("./contact/contact.routes").then((m) => m.CONTACT_ROUTES), |
| 45 | + }, |
| 46 | + { |
| 47 | + path: "about", |
| 48 | + loadChildren: () => |
| 49 | + import("./about/about.routes").then((m) => m.ABOUT_ROUTES), |
| 50 | + }, |
| 51 | +]; |
| 52 | + |
| 53 | +//Directly lazy loading a standalone component |
| 54 | +export const routes: Routes = [ |
| 55 | + { |
| 56 | + path: "contact", |
| 57 | + loadComponent: () => |
| 58 | + import("./contact/contact.component").then((m) => m.ContactComponent), |
| 59 | + }, |
| 60 | + { |
| 61 | + path: "about", |
| 62 | + loadComponent: () => |
| 63 | + import("./about/about.component").then((m) => m.AboutComponent), |
| 64 | + }, |
| 65 | +]; |
| 66 | + |
| 67 | +//In your main.ts add: |
| 68 | +bootstrapApplication(AppComponent, { |
| 69 | + providers: [provideRouter(routes)], |
| 70 | +}); |
| 71 | +``` |
| 72 | + |
| 73 | +### Preload all the modules |
| 74 | + |
| 75 | +In this way, all the modules of our application will be loaded at once and ready to use. |
| 76 | + |
| 77 | +```typescript |
| 78 | +@NgModule({ |
| 79 | + imports: [ |
| 80 | + RouterModule.forRoot(routes, { |
| 81 | + preloadingStrategy: PreloadAllModules, |
| 82 | + }), |
| 83 | + ], |
| 84 | + exports: [RouterModule], |
| 85 | +}) |
| 86 | +export class AppRoutingModule {} |
| 87 | + |
| 88 | +//StandAlone API version |
| 89 | +//In your main.ts add: |
| 90 | +bootstrapApplication(AppComponent, { |
| 91 | + providers: [provideRouter(routes, withPreloading(PreloadAllModules))], |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +### Customised module preloading |
| 96 | + |
| 97 | +We can choose which modules to load first, so the most used modules will already be loaded. |
| 98 | + |
| 99 | +```typescript |
| 100 | +@Injectable({ |
| 101 | + providedIn:'root' |
| 102 | +}) |
| 103 | +export class PreloadingStrategyService implements PreloadingStrategy { |
| 104 | + private preloadedModules: string[] = []; |
| 105 | + |
| 106 | + preload(route: Route, load: () => Observable): Observable { |
| 107 | + if (route.data && route.data['preload'] && route.path) { |
| 108 | + this.preloadedModules.push(route.path); |
| 109 | + return load(); |
| 110 | + } else { |
| 111 | + return of(null); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +const routes: Routes = [ |
| 117 | + { |
| 118 | + path: "contact", |
| 119 | + loadChildren: () => |
| 120 | + import("./contact/contact.module").then((m) => m.ContactModule), |
| 121 | + }, |
| 122 | + { |
| 123 | + path: "about", |
| 124 | + loadChildren: () => |
| 125 | + import("./about/about.module").then((m) => m.AboutModule), |
| 126 | + data: { preload: true }, |
| 127 | + }, |
| 128 | +]; |
| 129 | + |
| 130 | +@NgModule({ |
| 131 | + imports: [ |
| 132 | + RouterModule.forRoot(routes, { |
| 133 | + preloadingStrategy: PreloadingStrategyService, |
| 134 | + }), |
| 135 | + ], |
| 136 | + exports: [RouterModule], |
| 137 | +}) |
| 138 | +export class AppRoutingModule {} |
| 139 | + |
| 140 | +//StandAlone API version |
| 141 | +//In your main.ts add: |
| 142 | +bootstrapApplication(AppComponent, { |
| 143 | + providers: [provideRouter(routes, withPreloading(PreloadingStrategyService))], |
| 144 | +}); |
| 145 | +``` |
| 146 | + |
| 147 | +### Using ngx-quicklink |
| 148 | + |
| 149 | +ngx-quicklink is a library that loads modules as their link appears on screen. |
| 150 | +To install the library: `npm i ngx-quicklink` |
| 151 | + |
| 152 | +```typescript |
| 153 | +import { QuicklinkStrategy } from "ngx-quicklink"; |
| 154 | + |
| 155 | +@NgModule({ |
| 156 | + imports: [ |
| 157 | + RouterModule.forRoot(routes, { |
| 158 | + preloadingStrategy: QuicklinkStrategy, |
| 159 | + }), |
| 160 | + ], |
| 161 | + exports: [RouterModule], |
| 162 | +}) |
| 163 | +export class AppRoutingModule {} |
| 164 | + |
| 165 | +//StandAlone API version |
| 166 | +//In your main.ts add: |
| 167 | +import { quicklinkProviders, QuicklinkStrategy } from 'ngx-quicklink'; |
| 168 | + |
| 169 | +bootstrapApplication(AppComponent, { |
| 170 | + providers: [ |
| 171 | + provideRouter(routes, withPreloading(QuicklinkStrategy)), |
| 172 | + quicklinkProviders |
| 173 | + ] |
| 174 | +}) |
| 175 | + |
| 176 | +// Import the QuicklinkDirective in all your standalone components that use preloading: |
| 177 | +import { RouterModule } from '@angular/router'; |
| 178 | +import { QuicklinkDirective } from 'ngx-quicklink'; |
| 179 | + |
| 180 | +@Component({ |
| 181 | + standalone: true, |
| 182 | + imports: [RouterModule, QuicklinkDirective], |
| 183 | + template: ` |
| 184 | + <a routerLink="/form">Form</a> |
| 185 | + `, |
| 186 | +}) |
| 187 | +``` |
0 commit comments