Skip to content

Commit

Permalink
Add handling for effectiveconnectiontype
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 23, 2018
1 parent ce0aa40 commit 47dbaf5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ function extractInViewportLinks(el) {
export default function quicklink(options) {
options = options || {};
requestIdleCallback(() => {
if ('connection' in navigator) {
// Don't prefetch if the user is on 2G..
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
return;
}
}
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
fetchLinks(options.urls);
Expand All @@ -50,7 +56,9 @@ export default function quicklink(options) {
let el = options.el || document;
extractInViewportLinks(el).then((urls) => {
fetchLinks(urls);
});
}
});
}
});
}

// TODO: add preload / high prio?

0 comments on commit 47dbaf5

Please sign in to comment.