Skip to content

Commit d221c51

Browse files
committed
golf: use Set & share prefetch caller;
- 873 gz / 719 br
1 parent 1037e38 commit d221c51

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

src/index.mjs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
import prefetch from './prefetch.mjs';
1818
import requestIdleCallback from './request-idle-callback.mjs';
1919

20-
const loaderFunctions = new Map();
20+
const toPrefetch = new Set();
21+
2122
const observer = new IntersectionObserver(entries => {
2223
entries.forEach(entry => {
2324
if (entry.isIntersecting) {
2425
const url = entry.target.href;
25-
const fn = loaderFunctions.get(url);
26-
if (fn) fn.call(null);
26+
if (toPrefetch.has(url)) {
27+
toPrefetch.delete(url);
28+
prefetch(url, observer.priority);
29+
}
2730
}
2831
});
2932
});
@@ -56,16 +59,12 @@ export default function (options) {
5659
return;
5760
}
5861

62+
observer.priority = options.priority;
63+
5964
// If not, find all links and use IntersectionObserver.
6065
Array.from(options.el.querySelectorAll('a'), link => {
6166
observer.observe(link);
62-
63-
// Generate loader functions for each link
64-
const uri = link.href;
65-
loaderFunctions.set(uri, () => {
66-
loaderFunctions.delete(uri);
67-
prefetch(uri, options.priority);
68-
});
67+
toPrefetch.add(link.href);
6968
});
7069
}, {timeout: options.timeout});
7170
}

0 commit comments

Comments
 (0)