Skip to content

Commit

Permalink
feat(prefetch links scrolling into viewport) for #6
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Dec 5, 2018
1 parent 2d46f53 commit d1e825d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions demos/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<a href="../test/main.css">CSS</a>
</section>
<a href="../test/4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<div style="height:800px; width:800px;">test</div>
<script src="../dist/quicklink.umd.js"></script>
<script>
quicklink();
Expand Down
13 changes: 8 additions & 5 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import prefetch from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';

/**
* Extract links from a provided DOM element that are
* in the user's viewport.
* Prefetch in-viewport links from a target DOM element. The
* element will be observed using Intersection Observer.
* @param {Object} el DOM element to check for in-viewport links
* @param {Object} options Quicklink options object
* @return {Promise} resolving with list of URLs found
*/
function extractInViewportLinks(el) {
function fetchInViewportLinks(el, options) {
return new Promise((resolve, reject) => {
const urls = [];
const links = el.querySelectorAll('a');
Expand All @@ -36,6 +37,9 @@ function extractInViewportLinks(el) {
// Link is out of the view
}
});
// prefetch() maintains a list of in-memory URLs
// previously fetched so we don't attempt a refetch
prefetchURLs(urls, options.priority);
resolve(urls);
});
links.forEach(link => {
Expand Down Expand Up @@ -85,8 +89,7 @@ export default function (options) {
} else {
// Element to extract in-viewport links for
const el = options.el || document;
extractInViewportLinks(el).then(urls => {
prefetchURLs(urls, options.priority);
fetchInViewportLinks(el, options).then(urls => {
resolve(urls);
});
}
Expand Down

0 comments on commit d1e825d

Please sign in to comment.