Last active
November 15, 2022 08:57
-
-
Save nirkaufman/c52e37a171531ad28bec0a41a52acfaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {PreloadingStrategy, Route} from "@angular/router"; | |
import {Observable, of} from "rxjs"; | |
import {Injectable} from "@angular/core"; | |
/** | |
* Starting point for a CustomPreloading class from our live session! | |
* You can (and should) customize it further more to meet your needs | |
*/ | |
@Injectable({providedIn: 'root'}) | |
export class CustomPreloading implements PreloadingStrategy { | |
private slowConnection = ['slow-3g', '2g', '3g']; | |
preload(route: Route, load: () => Observable<any>): Observable<any> { | |
// control with custom route data | |
if(route.data && route.data['preload'] === false) { | |
return of(null); | |
} | |
// Don't preload for slow connections! | |
const connection = navigator?.connection; | |
if(connection) { | |
const networkSpeed = connection.effectiveType; | |
if(this.slowConnection.includes(networkSpeed)) { | |
return of(null); | |
} | |
} | |
return load(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment