UNPKG

prefetch-page

Version:

浏览器在空闲时预加载可见区域的超链接,以加速后续页面的加载速度 | Browser prefetchs visible area hyperlinks at idle time to speed up subsequent page loads

3 lines (2 loc) 1.32 kB
var prefetch=function(){"use strict";var e=location,t=e.hostname,n=e.href,r=[];function o(e){var t=navigator,n=t.connection||t.mozConnection||t.webkitConnection||{},o=n.saveData,i=n.effectiveType;o||/2g/.test(i)||r.includes(e)||(r.push(e),function(e){var t=document.createElement("link");t.rel="prefetch",t.href=e,document.head.appendChild(t)}(e))}return function(e){if((i=document.createElement("link")).relList&&i.relList.supports&&i.relList.supports("prefetch")&&IntersectionObserver){var i;e||(e={});var c=function(e){try{if("string"==typeof e){var t=document.querySelector(e);if(1===t.nodeType)return t}if(1===e.nodeType)return e}catch(e){}return document}(e.el),u=e.origins||[t],f=e.limit||1/0,a=(e.threshold||0)/100,s=e.delay||0,h=e.customs||[],l=new IntersectionObserver((function(e){e.forEach((function(e){var t=e.isIntersecting,n=e.target;t?n.prefetch=function(e,t){return t?setTimeout(e,t):e()}((function(){l.unobserve(n),r.length<f&&o(n.href)}),s):clearTimeout(n.prefetch)}))}),{threshold:a});c.querySelectorAll("a:not([target=_blank])").forEach((function(e){if("*"===u||u.includes(e.hostname)){if(e.href===n)return;var t=new URL(e.href,n);t.origin+t.pathname===e.href&&l.observe(e)}}));for(var p=0;p<h.length;p++)o(h[p]);return function(){r=[],l.disconnect()}}}}(); //# sourceMappingURL=prefetch.js.map