From 3329932d276983bb74171fd51be617757cd241b2 Mon Sep 17 00:00:00 2001 From: Sean Engelhardt Date: Fri, 22 Feb 2019 10:04:50 +0100 Subject: [PATCH] Preloads modules on site component Adds module preloading on site modules. --- client/src/app/app-routing.module.ts | 6 ++-- client/src/app/shared/utils/app-preloader.ts | 27 +++++++++++++++++ client/src/app/site/site-routing.module.ts | 31 +++++++++++++------- 3 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 client/src/app/shared/utils/app-preloader.ts diff --git a/client/src/app/app-routing.module.ts b/client/src/app/app-routing.module.ts index 60260308c..22171a316 100644 --- a/client/src/app/app-routing.module.ts +++ b/client/src/app/app-routing.module.ts @@ -7,6 +7,7 @@ import { LoginLegalNoticeComponent } from './site/login/components/login-legal-n import { LoginPrivacyPolicyComponent } from './site/login/components/login-privacy-policy/login-privacy-policy.component'; import { ResetPasswordComponent } from './site/login/components/reset-password/reset-password.component'; import { ResetPasswordConfirmComponent } from './site/login/components/reset-password-confirm/reset-password-confirm.component'; +import { AppPreloader } from './shared/utils/app-preloader'; /** * Global app routing @@ -29,7 +30,8 @@ const routes: Routes = [ ]; @NgModule({ - imports: [RouterModule.forRoot(routes)], - exports: [RouterModule] + imports: [RouterModule.forRoot(routes, { preloadingStrategy: AppPreloader })], + exports: [RouterModule], + providers: [AppPreloader] }) export class AppRoutingModule {} diff --git a/client/src/app/shared/utils/app-preloader.ts b/client/src/app/shared/utils/app-preloader.ts new file mode 100644 index 000000000..798a3d39e --- /dev/null +++ b/client/src/app/shared/utils/app-preloader.ts @@ -0,0 +1,27 @@ +import { PreloadingStrategy, Route } from '@angular/router'; +import { Observable, of } from 'rxjs'; + +/** + * Custom preload strategy class + * + * @example + * ``` + * { + * path: 'agenda', + * loadChildren: './agenda/agenda.module#AgendaModule', + * data: { preload: true } + * } + * ``` + */ +export class AppPreloader implements PreloadingStrategy { + /** + * Custom preload function. + * Can be add to routes as data argument. + * + * @param route The route to load + * @param load The load function + */ + public preload(route: Route, load: Function): Observable { + return route.data && route.data.preload ? load() : of(null); + } +} diff --git a/client/src/app/site/site-routing.module.ts b/client/src/app/site/site-routing.module.ts index 4b56f5694..b5a21c703 100644 --- a/client/src/app/site/site-routing.module.ts +++ b/client/src/app/site/site-routing.module.ts @@ -2,7 +2,6 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { SiteComponent } from './site.component'; - import { AuthGuard } from '../core/core-services/auth-guard.service'; /** @@ -17,43 +16,53 @@ const routes: Routes = [ children: [ { path: '', - loadChildren: './common/common.module#CommonModule' + loadChildren: './common/common.module#CommonModule', + data: { preload: true } }, { path: 'agenda', - loadChildren: './agenda/agenda.module#AgendaModule' + loadChildren: './agenda/agenda.module#AgendaModule', + data: { preload: true } }, { path: 'assignments', - loadChildren: './assignments/assignments.module#AssignmentsModule' + loadChildren: './assignments/assignments.module#AssignmentsModule', + data: { preload: true } }, { path: 'mediafiles', - loadChildren: './mediafiles/mediafiles.module#MediafilesModule' + loadChildren: './mediafiles/mediafiles.module#MediafilesModule', + data: { preload: true } }, { path: 'motions', - loadChildren: './motions/motions.module#MotionsModule' + loadChildren: './motions/motions.module#MotionsModule', + data: { preload: true } }, { path: 'settings', - loadChildren: './config/config.module#ConfigModule' + loadChildren: './config/config.module#ConfigModule', + data: { preload: true } }, { path: 'users', - loadChildren: './users/users.module#UsersModule' + loadChildren: './users/users.module#UsersModule', + data: { preload: true } }, { path: 'tags', - loadChildren: './tags/tag.module#TagModule' + loadChildren: './tags/tag.module#TagModule', + data: { preload: true } }, { path: 'history', - loadChildren: './history/history.module#HistoryModule' + loadChildren: './history/history.module#HistoryModule', + data: { preload: true } }, { path: 'projectors', - loadChildren: './projector/projector.module#ProjectorModule' + loadChildren: './projector/projector.module#ProjectorModule', + data: { preload: true } } ], canActivateChild: [AuthGuard]