UNPKG

quicklink

Version:

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

2 lines (1 loc) 1 kB
"use strict";var e=require("react"),r=require("route-manifest"),t=require("./quicklink.js"),n=function(e){return e&&e.href||e||""},o=function(e,t,o){void 0===o&&(o=n);var i=r(window.__rmanifest,e.pathname).files.map(o).filter(Boolean);i.length>0?t(i):t(e.href)};exports.withQuicklink=function(r,n){return void 0===n&&(n={}),function(i){var u,c,s,f,a,l,d,v,h,m,w,k,p,E,q=(u={root:document.body.parentElement},f=void 0===(s=(c=void 0===u?{}:u).root)?null:s,a=c.rootMargin,d=void 0===(l=c.threshold)?0:l,v=e.useState({}),h=v[0],m=v[1],w=e.useState(null),k=w[0],p=w[1],E=e.useRef(null),e.useEffect(function(){E.current&&E.current.disconnect(),E.current=new window.IntersectionObserver(function(e){var r=e[0];return m(r)},{root:f,rootMargin:a,threshold:d});var e=E.current;return k&&e.observe(k),function(){return e.disconnect()}},[k,f,a,d]),[p,h]),b=q[0],g=q[1].intersectionRatio;return e.useEffect(function(){n.prefetchChunks=o,g>0&&t.listen(n)},[g]),e.createElement("div",{ref:b},e.createElement(r,i))}};