Skip to content

Instantly share code, notes, and snippets.

@jroakes
Created February 9, 2019 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jroakes/a5b9963ccc9bb4e83c2d510484d1f3c2 to your computer and use it in GitHub Desktop.
Save jroakes/a5b9963ccc9bb4e83c2d510484d1f3c2 to your computer and use it in GitHub Desktop.
let uP,mT,lT;const pf=document.createElement('link'),isSupported=pf.relList&&pf.relList.supports&&pf.relList.supports('prefetch'),allowQueryString='instantAllowQueryString'in document.body.dataset;if(isSupported){pf.rel='prefetch',document.head.appendChild(pf);const a={capture:!0,passive:!0};document.addEventListener('touchstart',tL,a),document.addEventListener('mouseover',mouseoverListener,a)}function tL(a){lT=performance.now();const b=a.target.closest('a');b&&iP(b)&&(b.addEventListener('touchcancel',tecL,{passive:!0}),b.addEventListener('touchend',tecL,{passive:!0}),uP=b.href,preload(b.href))}function tecL(){uP=void 0,sP()}function mouseoverListener(a){if(!(1100>performance.now()-lT)){const b=a.target.closest('a');b&&iP(b)&&(b.addEventListener('mouseout',mL,{passive:!0}),uP=b.href,mT=setTimeout(()=>{preload(b.href),mT=void 0},65))}}function mL(a){a.relatedTarget&&a.target.closest('a')==a.relatedTarget.closest('a')||(mT?(clearTimeout(mT),mT=void 0):(uP=void 0,sP()))}function iP(a){if(uP!=a.href){const b=new URL(a.href);return b.origin==location.origin?!allowQueryString&&b.search||b.pathname+b.search==location.pathname+location.search&&b.hash?void 0:!('noInstant'in a.dataset)||void 0:void 0}}function preload(a){pf.href=a}function sP(){pf.removeAttribute('href')}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment