Skip to content

Instantly share code, notes, and snippets.

@nirkaufman
Last active November 15, 2022 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirkaufman/c52e37a171531ad28bec0a41a52acfaa to your computer and use it in GitHub Desktop.
Save nirkaufman/c52e37a171531ad28bec0a41a52acfaa to your computer and use it in GitHub Desktop.
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