Skip to content

Commit

Permalink
clean(index.mjs, prefetch.mjs) move prefetching logic to one place
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 24, 2018
1 parent 6e7a838 commit 6e5d9fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
17 changes: 3 additions & 14 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import prefetch from './prefetch.mjs';
import prefetchLinks from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';

/**
* Prefetch an array of URLs using rel=prefetch
* if supported. Falls back to XHR otherwise.
* @param {Array} urls Array of URLs to prefetch
*/
function fetchLinks(urls) {
urls.forEach(url => {
prefetch(url);
});
}

/**
* Extract links from a provided DOM element that are
* in the user's viewport.
Expand Down Expand Up @@ -64,12 +53,12 @@ export default function (options) {
}
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
fetchLinks(options.urls);
prefetchLinks(options.urls);
} else {
// Element to extract in-viewport links for
const el = options.el || document;
extractInViewportLinks(el).then(urls => {
fetchLinks(urls);
prefetchLinks(urls);
});
}
});
Expand Down
13 changes: 12 additions & 1 deletion src/prefetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@ const prefetch = function (url) {
supportedPrefetchStrategy(url);
};

export default prefetch;
/**
* Prefetch an array of URLs using rel=prefetch
* if supported. Falls back to XHR otherwise.
* @param {Array} urls Array of URLs to prefetch
*/
const prefetchLinks = function (urls) {
urls.forEach(url => {
prefetch(url);
});
}

export default prefetchLinks;

0 comments on commit 6e5d9fb

Please sign in to comment.