Skip to content

Commit

Permalink
A few quick size optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Jan 7, 2019
1 parent eb83845 commit 201c217
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 8 additions & 10 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,23 @@ function isIgnored(node, filter) {
* @param {Function} options.timeoutFn - Custom timeout function
*/
export default function (options) {
options = Object.assign({
timeout: 2e3,
priority: false,
timeoutFn: requestIdleCallback,
el: document,
}, options);
if (!options) options = {};

observer.priority = options.priority;
observer.priority = options.priority || false;

const allowed = options.origins || [location.hostname];
const ignores = options.ignores || [];

options.timeoutFn(() => {
const timeout = options.timeout || 2e3;
const timeoutFn = options.timeoutFn || requestIdleCallback;

timeoutFn(() => {
// If URLs are given, prefetch them.
if (options.urls) {
options.urls.forEach(prefetcher);
} else {
// If not, find all links and use IntersectionObserver.
Array.from(options.el.querySelectorAll('a'), link => {
Array.from((options.el || document).querySelectorAll('a'), link => {
observer.observe(link);
// If the anchor matches a permitted origin
// ~> A `[]` or `true` means everything is allowed
Expand All @@ -100,5 +98,5 @@ export default function (options) {
}
});
}
}, {timeout: options.timeout});
}, {timeout});
}
2 changes: 1 addition & 1 deletion src/prefetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const preFetched = {};
*/
function support(feature) {
const link = document.createElement('link');
return (link.relList || {}).supports && link.relList.supports(feature);
return link.relList && link.relList.supports && link.relList.supports(feature);
}

/**
Expand Down

0 comments on commit 201c217

Please sign in to comment.