diff --git a/src/index.ts b/src/index.ts index cfa48d0..c9acfbc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,15 +39,33 @@ export class IdlePreload /*implements PreloadingStrategy*/ { * include zone to run outside of zone.js */ constructor(private _ngZone: NgZone, @Inject(REQUEST_IDLE_CALLBACK) private requestIdleCallback: any) {} + loadingRoute = new Set(); + loading = true; + + /*Checking for slow internet connection*/ + const conn = typeof navigator !== 'undefined' ? (navigator as any).connection : undefined; + if (conn) { + if ((conn.effectiveType || '').includes('2g') || conn.saveData) { this.loading = false}; + } /* * fire off preloading async modules */ - preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { - this.requestIdleCallback(fn); - return of(null); + if (this.loading) { + preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { + /*To avoid reloading the route*/ + if (this.loadingRoute.has(route)) { + return null; + } + /*loading only required modules*/ + if(route.data && !route.data.preload) { + return null; + } else { + this.requestIdleCallback(fn); + return of(null); + } + } } - } /*