Skip to content

Commit d3746e1

Browse files
authored
Added threshold option to allow users select the % of link areas that entered the viewport before prefetching (#214)
1 parent 2b3dc21 commit d3746e1

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ Default: `Infinity`
131131
132132
The _total_ requests that can be prefetched while observing the `options.el` container.
133133
134+
#### options.threshold
135+
Type: `Number`<br>
136+
Default: `0`
137+
138+
The _area percentage_ of each link that must have entered the viewport to be fetched, in its decimal form (e.g. 0.25 = 25%).
139+
134140
#### options.throttle
135141
Type: `Number`<br>
136142
Default: `Infinity`

src/index.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function isIgnored(node, filter) {
4949
* @param {Array|RegExp|Function} [options.ignores] - Custom filter(s) that run after origin checks
5050
* @param {Number} [options.timeout] - Timeout after which prefetching will occur
5151
* @param {Number} [options.throttle] - The concurrency limit for prefetching
52+
* @param {Number} [options.threshold] - The area percentage of each link that must have entered the viewport to be fetched
5253
* @param {Number} [options.limit] - The total number of prefetches to allow
5354
* @param {Number} [options.delay] - Time each link needs to stay inside viewport before prefetching (milliseconds)
5455
* @param {Function} [options.timeoutFn] - Custom timeout function
@@ -63,6 +64,7 @@ export function listen(options) {
6364

6465
const [toAdd, isDone] = throttle(options.throttle || 1/0);
6566
const limit = options.limit || 1/0;
67+
const threshold = options.threshold || 0;
6668

6769
const allowed = options.origins || [location.hostname];
6870
const ignores = options.ignores || [];
@@ -113,6 +115,8 @@ export function listen(options) {
113115
}
114116
}
115117
});
118+
}, {
119+
threshold
116120
});
117121

118122
timeoutFn(() => {

test/quicklink.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('quicklink tests', function () {
274274
expect(ours).to.include(`https://example.com/?url=${server}/3.html`);
275275
expect(ours).to.include(`https://example.com/?url=${server}/4.html`);
276276
});
277-
277+
278278
it('should delay prefetch for in-viewport links correctly (UMD)', async function () {
279279
const responseURLs = [];
280280
page.on('response', resp => {
@@ -303,4 +303,26 @@ describe('quicklink tests', function () {
303303
expect(responseURLs).to.include(`${server}/4.html`);
304304
});
305305

306+
it('should consider threshold option before prefetching (UMD)', async function () {
307+
const responseURLs = [];
308+
page.on('response', resp => {
309+
responseURLs.push(resp.url());
310+
});
311+
312+
await page.goto(`${server}/test-threshold.html`);
313+
await page.setViewport({
314+
width: 1000,
315+
height: 800,
316+
});
317+
await page.waitFor(1000);
318+
expect(responseURLs).to.be.an('array');
319+
expect(responseURLs).to.include(`${server}/1.html`);
320+
expect(responseURLs).to.include(`${server}/2.html`);
321+
await page.evaluate(_ => {
322+
window.scrollBy(0, window.innerHeight);
323+
});
324+
await page.waitFor(400);
325+
expect(responseURLs).to.include(`${server}/3.html`);
326+
expect(responseURLs).to.include(`${server}/4.html`);
327+
});
306328
});

test/test-threshold.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<title>Prefetch: Basic Usage</title>
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
<link rel="stylesheet" media="screen" href="main.css">
10+
<style>
11+
ul {
12+
display: flex;
13+
flex-wrap: wrap;
14+
justify-content: space-evenly;
15+
}
16+
17+
li {
18+
margin-bottom: 32px;
19+
}
20+
21+
a {
22+
display: block;
23+
width: 400px;
24+
height: 600px;
25+
background: #ccc;
26+
text-align: center;
27+
}
28+
</style>
29+
<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>
30+
</head>
31+
32+
<body>
33+
<ul>
34+
<li>
35+
<a href="1.html">Link 1</a>
36+
</li>
37+
<li>
38+
<a href="2.html">Link 2</a>
39+
</li>
40+
<li>
41+
<a href="3.html">Link 3</a>
42+
</li>
43+
<li>
44+
<a href="4.html">Link 4</a>
45+
</li>
46+
</ul>
47+
<script src="../dist/quicklink.umd.js"></script>
48+
<script>
49+
quicklink.listen({threshold: 0.5});
50+
</script>
51+
</body>
52+
53+
</html>

0 commit comments

Comments
 (0)