Skip to content

Commit a7cafa4

Browse files
committed
clean(index, prefetch): move connection logic to prefetcher
1 parent c4fb77a commit a7cafa4

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/index.mjs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,6 @@ function extractInViewportLinks(el) {
5858
export default function (options) {
5959
options = options || { priority: 'low' };
6060
requestIdleCallback(() => {
61-
if ('connection' in navigator) {
62-
// Don't prefetch if the user is on 2G..
63-
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
64-
return;
65-
}
66-
// Don't prefetch if Save-Data is enabled..
67-
if (navigator.connection.saveData) {
68-
return;
69-
}
70-
}
7161
// Prefetch an array of URLs if supplied (as an override)
7262
if (options.urls !== undefined && options.urls.length > 0) {
7363
prefetchLinks(options.urls, options.priority);

src/prefetch.mjs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,29 @@ const preFetched = {};
8383

8484
const prefetch = function (url, priority) {
8585
return new Promise(resolve => {
86+
if ('connection' in navigator) {
87+
// Don't prefetch if the user is on 2G..
88+
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
89+
resolve();
90+
return;
91+
}
92+
// Don't prefetch if Save-Data is enabled..
93+
if (navigator.connection.saveData) {
94+
resolve();
95+
return;
96+
}
97+
}
8698
if (preFetched[url]) {
8799
resolve();
88100
return;
89101
}
90-
91102
if (priority && priority === `high`) {
92-
highPriFetchStrategy(url);
103+
highPriFetchStrategy(url)
104+
.then(() => {
105+
resolve();
106+
preFetched[url] = true;
107+
})
108+
.catch(() => { });
93109
} else {
94110
supportedPrefetchStrategy(url)
95111
.then(() => {

0 commit comments

Comments
 (0)