Skip to content

Commit

Permalink
clean(index, prefetch): move connection logic to prefetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 24, 2018
1 parent c4fb77a commit a7cafa4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ function extractInViewportLinks(el) {
export default function (options) {
options = options || { priority: 'low' };
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;
}
// Don't prefetch if Save-Data is enabled..
if (navigator.connection.saveData) {
return;
}
}
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
prefetchLinks(options.urls, options.priority);
Expand Down
20 changes: 18 additions & 2 deletions src/prefetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,29 @@ const preFetched = {};

const prefetch = function (url, priority) {
return new Promise(resolve => {
if ('connection' in navigator) {
// Don't prefetch if the user is on 2G..
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
resolve();
return;
}
// Don't prefetch if Save-Data is enabled..
if (navigator.connection.saveData) {
resolve();
return;
}
}
if (preFetched[url]) {
resolve();
return;
}

if (priority && priority === `high`) {
highPriFetchStrategy(url);
highPriFetchStrategy(url)
.then(() => {
resolve();
preFetched[url] = true;
})
.catch(() => { });
} else {
supportedPrefetchStrategy(url)
.then(() => {
Expand Down

0 comments on commit a7cafa4

Please sign in to comment.