diff --git a/src/index.mjs b/src/index.mjs index 14a4fb0e..6cf9c62a 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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); diff --git a/src/prefetch.mjs b/src/prefetch.mjs index f84be395..c2613b22 100644 --- a/src/prefetch.mjs +++ b/src/prefetch.mjs @@ -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(() => {