Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate links not being preloaded #403

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function listen(options = {}) {
const allowed = options.origins || [location.hostname];
const ignores = options.ignores || [];
const delay = options.delay || 0;
const hrefsInViewport = [];
const hrefsInViewport = new Map();

const timeoutFn = options.timeoutFn || requestIdleCallback;
const hrefFn = typeof options.hrefFn === 'function' && options.hrefFn;
Expand All @@ -116,16 +116,19 @@ export function listen(options = {}) {

const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
const set = hrefsInViewport.get(entry.target.href) || new Set();
hrefsInViewport.set(entry.target.href, set);

// On enter
if (entry.isIntersecting) {
entry = entry.target;
// Adding href to array of hrefsInViewport
hrefsInViewport.push(entry.href);
// Adding href to map of hrefsInViewport
set.add(entry);

// Setting timeout
setTimeoutIfDelay(() => {
// Do not prefetch if not found in viewport
if (!hrefsInViewport.includes(entry.href)) return;
if (!set || !set.size) return;

observer.unobserve(entry);

Expand Down Expand Up @@ -159,10 +162,7 @@ export function listen(options = {}) {
// On exit
} else {
entry = entry.target;
const index = hrefsInViewport.indexOf(entry.href);
if (index > -1) {
hrefsInViewport.splice(index);
}
set.delete(entry);
}
});
}, {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test-basic-usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<a href="main.css">CSS</a>
</section>
<a href="4.html" style="position:absolute;margin-top:900px;">Link 4</a>
<a href="1.html" style="position:absolute;margin-top:1000px;">Link 1 again</a>
<script src="../../dist/quicklink.umd.js"></script>
<script>
quicklink.listen();
Expand Down
2 changes: 2 additions & 0 deletions test/quicklink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mainSuite('should prefetch in-viewport links correctly (UMD)', async context =>
assert.ok(responseURLs.includes(`${server}/1.html`));
assert.ok(responseURLs.includes(`${server}/2.html`));
assert.ok(responseURLs.includes(`${server}/3.html`));
assert.ok(!responseURLs.includes(`${server}/4.html`));
});

mainSuite('should prefetch in-viewport links correctly (ES Modules)', async context => {
Expand All @@ -53,6 +54,7 @@ mainSuite('should prefetch in-viewport links correctly (ES Modules)', async cont
assert.ok(responseURLs.includes(`${server}/1.html`));
assert.ok(responseURLs.includes(`${server}/2.html`));
assert.ok(responseURLs.includes(`${server}/3.html`));
assert.ok(!responseURLs.includes(`${server}/4.html`));
});

mainSuite('should prefetch in-viewport links that scroll into view correctly (UMD)', async context => {
Expand Down