Skip to content

Commit

Permalink
feat: add “opt.origins” for inspecting before queue;
Browse files Browse the repository at this point in the history
- 771 gz (+30 bytes)
  • Loading branch information
lukeed committed Dec 14, 2018
1 parent 6e526fc commit 1074d46
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function prefetcher(url) {
* @param {Array} options.urls - Array of URLs to prefetch (override)
* @param {Object} options.el - DOM element to prefetch in-viewport links of
* @param {Boolean} options.priority - Attempt higher priority fetch (low or high)
* @param {Array} options.origins - Allowed origins to prefetch (empty allows all)
* @param {Number} options.timeout - Timeout after which prefetching will occur
* @param {function} options.timeoutFn - Custom timeout function
*/
Expand All @@ -61,6 +62,8 @@ export default function (options) {

observer.priority = options.priority;

const allowed = options.origins || [];

options.timeoutFn(() => {
// If URLs are given, prefetch them.
if (options.urls) {
Expand All @@ -69,7 +72,11 @@ export default function (options) {
// If not, find all links and use IntersectionObserver.
Array.from(options.el.querySelectorAll('a'), link => {
observer.observe(link);
toPrefetch.add(link.href);
// If the anchor matches a permitted origin
// ~> An empty list means everything is allowed
if (!allowed.length || allowed.includes(link.hostname)) {
toPrefetch.add(link.href);
}
});
}
}, {timeout: options.timeout});
Expand Down

0 comments on commit 1074d46

Please sign in to comment.