Skip to content

Commit

Permalink
clean(src/index.mjs, src/prefetch.mjs): further reshuffling
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 24, 2018
1 parent 4aecc0f commit 9fe6036
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
22 changes: 17 additions & 5 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
**/

import prefetchURLs from './prefetch.mjs';
import prefetch from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';

/**
Expand Down Expand Up @@ -42,7 +42,19 @@ function extractInViewportLinks(el) {
observer.observe(link);
});
});
}
};

/**
* Prefetch an array of URLs using rel=prefetch
* if supported. Falls back to XHR otherwise.
* @param {Array} urls - Array of URLs to prefetch
* @param {string} priority - "priority" of the request
*/
const prefetchURLs = function (urls, priority) {
urls.forEach(url => {
prefetch(url, priority);
});
};

/**
* Prefetch an array of URLs if the user's effective
Expand All @@ -56,16 +68,16 @@ function extractInViewportLinks(el) {
* @param {string} options.priority - Attempt to fetch with higher priority (low or high)
*/
export default function (options) {
options = options || { priority: 'low' };
options = options || {priority: 'low'};
requestIdleCallback(() => {
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
prefetchLinks(options.urls, options.priority);
prefetchURLs(options.urls, options.priority);
} else {
// Element to extract in-viewport links for
const el = options.el || document;
extractInViewportLinks(el).then(urls => {
prefetchLinks(urls, options.priority);
prefetchURLs(urls, options.priority);
});
}
});
Expand Down
14 changes: 1 addition & 13 deletions src/prefetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,4 @@ const prefetch = function (url, priority) {
});
};

/**
* Prefetch an array of URLs using rel=prefetch
* if supported. Falls back to XHR otherwise.
* @param {Array} urls - Array of URLs to prefetch
* @param {string} priority - "priority" of the request
*/
const prefetchURLs = function (urls, priority) {
urls.forEach(url => {
prefetch(url, priority);
});
};

export default prefetchURLs;
export default prefetch;

0 comments on commit 9fe6036

Please sign in to comment.