UNPKG

quicklink

Version:

Faster subsequent page-loads by prefetching in-viewport links during idle time

2 lines (1 loc) 3.25 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).quicklink={})}(this,function(e){"use strict";function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function n(e,n){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,n){if(e){if("string"==typeof e)return t(e,n);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(e,n):void 0}}(e))||n){r&&(e=r);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(e,t,n,r){var o={headers:{accept:"*/*"}};return t||(o.mode="no-cors"),n&&(o.credentials="include"),o.priority=r?"high":"low",window.fetch?fetch(e,o):function(e,t){return new Promise(function(n,r,o){(o=new XMLHttpRequest).open("GET",e,o.withCredentials=t),o.setRequestHeader("Accept","*/*"),o.onload=function(){200===o.status?n():r()},o.send()})}(e,n)}var o,i=(o=document.createElement("link")).relList&&o.relList.supports&&o.relList.supports("prefetch")?function(e,t){return new Promise(function(n,r,o){(o=document.createElement("link")).rel="prefetch",o.href=e,t&&o.setAttribute("crossorigin","anonymous"),o.onload=n,o.onerror=r,document.head.append(o)})}:r,a=window.requestIdleCallback||function(e){var t=Date.now();return setTimeout(function(){e({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-t))}})},1)},u=new Set;function c(e,t){return Array.isArray(t)?t.some(function(t){return c(e,t)}):(t.test||t).call(t,e.href,e)}function s(e,t){var n=navigator.connection;return n?n.saveData?Promise.reject(new Error("Cannot prefetch, Save-Data is enabled")):/2g/.test(n.effectiveType)?Promise.reject(new Error("Cannot prefetch, network conditions are poor")):Promise.all([e].flat().map(function(e){return u.has(e)?[]:(u.add(e),(t?r:i)(new URL(e,location.href).toString()))})):Promise.resolve()}e.listen=function(e){if(void 0===e&&(e={}),window.IntersectionObserver){var t=function(e){e=e||1;var t=[],n=0;function r(){n<e&&t.length>0&&(t.shift()(),n++)}return[function(e){t.push(e)>1||r()},function(){n--,r()}]}(e.throttle||Number.Infinity),r=t[0],o=t[1],i=e.limit||Number.Infinity,f=e.origins||[location.hostname],l=e.ignores||[],d=e.timeoutFn||a,m=e.prefetchChunks,h=function(t){s(t,e.priority).then(o).catch(function(t){o(),e.onError&&e.onError(t)})},p=new IntersectionObserver(function(e){for(var t,o=function(){var e=t.value,n=e.isIntersecting,o=e.target;if(!n)return 1;p.unobserve(o),u.size<i&&r(function(){m?m(o,h):h(o.href)})},a=n(e);!(t=a()).done;)o()});return d(function(){for(var t,r=n((e.el||document).querySelectorAll("a[href]"));!(t=r()).done;){var o=t.value;f.length&&!f.includes(o.hostname)||c(o,l)||p.observe(o)}},{timeout:e.timeout||2e3}),function(){u.clear(),p.disconnect()}}},e.prefetch=s});