From 810b3ed17e579b3000d012cca57989e73a5587d5 Mon Sep 17 00:00:00 2001 From: Yazharasu Date: Wed, 21 Sep 2022 17:42:41 +0530 Subject: [PATCH 1/3] Checking internet connection & preload requirement Modules which doesn't require preloading can be provided with data: {preload: false} in the route. Internet connection checked before preloading. If the connection is on a 2G network or if the data saver is enabled, preloading will not be done. --- src/index.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index cfa48d0..ec36f6b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,15 +39,31 @@ 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; + + /*To avoid reloading the route*/ + if (this.loadingRoute.has(route)) { + this.loading = false; + } + /*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 + * fire off preloading async modules - loading only required modules */ - preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { - this.requestIdleCallback(fn); - return of(null); - } - + if (this.loading) { + if(route.data && !route.data.preload) { + return null; + } else { + preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { + this.requestIdleCallback(fn); + return of(null); + } + } } /* From 6bf08107acebf688abbc2214031148a515c3bad9 Mon Sep 17 00:00:00 2001 From: Yazharasu Date: Sun, 25 Sep 2022 21:10:11 +0530 Subject: [PATCH 2/3] route fix --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index ec36f6b..c63ddc1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,16 +55,16 @@ export class IdlePreload /*implements PreloadingStrategy*/ { /* * fire off preloading async modules - loading only required modules */ - if (this.loading) { - if(route.data && !route.data.preload) { + preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { + if(this.loading) { + if(route.data && !route.data.preload) { return null; } else { - preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { this.requestIdleCallback(fn); return of(null); } + } } -} /* * raw providers From 63f13afd496638ca67b0ed75dd2cc6b383783883 Mon Sep 17 00:00:00 2001 From: Yazharasu Date: Sun, 25 Sep 2022 21:21:48 +0530 Subject: [PATCH 3/3] route fix --- src/index.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index c63ddc1..c9acfbc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -42,10 +42,6 @@ export class IdlePreload /*implements PreloadingStrategy*/ { loadingRoute = new Set(); loading = true; - /*To avoid reloading the route*/ - if (this.loadingRoute.has(route)) { - this.loading = false; - } /*Checking for slow internet connection*/ const conn = typeof navigator !== 'undefined' ? (navigator as any).connection : undefined; if (conn) { @@ -53,18 +49,24 @@ export class IdlePreload /*implements PreloadingStrategy*/ { } /* - * fire off preloading async modules - loading only required modules + * fire off preloading async modules */ - preload(route: /*Route*/ any, fn: any /* () => Observable*/ ): any/* Observable */ { - if(this.loading) { - if(route.data && !route.data.preload) { + 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); } } - } + } +} /* * raw providers