top of page
bottom of page
function prefetch(nodeList) {
if (navigator.connection &&
(navigator.connection.effectiveType === 'slow-2g' ||
navigator.connection.effectiveType === '2g')) {
return;
}
if (navigator.deviceMemory && navigator.deviceMemory <= 2) {
return;
}
const fetched = {};
const observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
const link = entry.target.href;
if (!fetched[link]) {
fetched[link] = true;
fetch(link, { priority: 'low' });
}
observer.unobserve(entry.target);
}
});
});
nodeList.forEach(link => observer.observe(link));
}
const idleCallback = window.requestIdleCallback || function (cb) {
return setTimeout(() => cb(), 1);
};
idleCallback(function () {
const links = document.querySelectorAll('a[href]');
prefetch(links);
});