UNPKG

6.61 kBJavaScriptView Raw
1var e,n,t,r,i,a=-1,o=function(e){addEventListener("pageshow",(function(n){n.persisted&&(a=n.timeStamp,e(n))}),!0)},c=function(){return window.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0]},u=function(){var e=c();return e&&e.activationStart||0},f=function(e,n){var t=c(),r="navigate";return a>=0?r="back-forward-cache":t&&(r=document.prerendering||u()>0?"prerender":t.type.replace(/_/g,"-")),{name:e,value:void 0===n?-1:n,rating:"good",delta:0,entries:[],id:"v3-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:r}},s=function(e,n,t){try{if(PerformanceObserver.supportedEntryTypes.includes(e)){var r=new PerformanceObserver((function(e){n(e.getEntries())}));return r.observe(Object.assign({type:e,buffered:!0},t||{})),r}}catch(e){}},d=function(e,n){var t=function t(r){"pagehide"!==r.type&&"hidden"!==document.visibilityState||(e(r),n&&(removeEventListener("visibilitychange",t,!0),removeEventListener("pagehide",t,!0)))};addEventListener("visibilitychange",t,!0),addEventListener("pagehide",t,!0)},l=function(e,n,t,r){var i,a;return function(o){n.value>=0&&(o||r)&&((a=n.value-(i||0))||void 0===i)&&(i=n.value,n.delta=a,n.rating=function(e,n){return e>n[1]?"poor":e>n[0]?"needs-improvement":"good"}(n.value,t),e(n))}},p=-1,v=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},m=function(){d((function(e){var n=e.timeStamp;p=n}),!0)},h=function(){return p<0&&(p=v(),m(),o((function(){setTimeout((function(){p=v(),m()}),0)}))),{get firstHiddenTime(){return p}}},g=function(e,n){n=n||{};var t,r=[1800,3e3],i=h(),a=f("FCP"),c=function(e){e.forEach((function(e){"first-contentful-paint"===e.name&&(p&&p.disconnect(),e.startTime<i.firstHiddenTime&&(a.value=e.startTime-u(),a.entries.push(e),t(!0)))}))},d=window.performance&&window.performance.getEntriesByName&&window.performance.getEntriesByName("first-contentful-paint")[0],p=d?null:s("paint",c);(d||p)&&(t=l(e,a,r,n.reportAllChanges),d&&c([d]),o((function(i){a=f("FCP"),t=l(e,a,r,n.reportAllChanges),requestAnimationFrame((function(){requestAnimationFrame((function(){a.value=performance.now()-i.timeStamp,t(!0)}))}))})))},y=!1,T=-1,E=function(e,n){n=n||{};var t=[.1,.25];y||(g((function(e){T=e.value})),y=!0);var r,i=function(n){T>-1&&e(n)},a=f("CLS",0),c=0,u=[],p=function(e){e.forEach((function(e){if(!e.hadRecentInput){var n=u[0],t=u[u.length-1];c&&e.startTime-t.startTime<1e3&&e.startTime-n.startTime<5e3?(c+=e.value,u.push(e)):(c=e.value,u=[e]),c>a.value&&(a.value=c,a.entries=u,r())}}))},v=s("layout-shift",p);v&&(r=l(i,a,t,n.reportAllChanges),d((function(){p(v.takeRecords()),r(!0)})),o((function(){c=0,T=-1,a=f("CLS",0),r=l(i,a,t,n.reportAllChanges)})))},w={passive:!0,capture:!0},C=new Date,L=function(r,i){e||(e=i,n=r,t=new Date,A(removeEventListener),S())},S=function(){if(n>=0&&n<t-C){var i={entryType:"first-input",name:e.type,target:e.target,cancelable:e.cancelable,startTime:e.timeStamp,processingStart:e.timeStamp+n};r.forEach((function(e){e(i)})),r=[]}},b=function(e){if(e.cancelable){var n=(e.timeStamp>1e12?new Date:performance.now())-e.timeStamp;"pointerdown"==e.type?function(e,n){var t=function(){L(e,n),i()},r=function(){i()},i=function(){removeEventListener("pointerup",t,w),removeEventListener("pointercancel",r,w)};addEventListener("pointerup",t,w),addEventListener("pointercancel",r,w)}(n,e):L(n,e)}},A=function(e){["mousedown","keydown","touchstart","pointerdown"].forEach((function(n){return e(n,b,w)}))},F=function(t,i){i=i||{};var a,c=[100,300],u=h(),p=f("FID"),v=function(e){e.startTime<u.firstHiddenTime&&(p.value=e.processingStart-e.startTime,p.entries.push(e),a(!0))},m=function(e){e.forEach(v)},g=s("first-input",m);a=l(t,p,c,i.reportAllChanges),g&&d((function(){m(g.takeRecords()),g.disconnect()}),!0),g&&o((function(){var o;p=f("FID"),a=l(t,p,c,i.reportAllChanges),r=[],n=-1,e=null,A(addEventListener),o=v,r.push(o),S()}))},I=0,P=1/0,k=0,M=function(e){e.forEach((function(e){e.interactionId&&(P=Math.min(P,e.interactionId),k=Math.max(k,e.interactionId),I=k?(k-P)/7+1:0)}))},B=function(){return i?I:performance.interactionCount||0},D=function(){"interactionCount"in performance||i||(i=s("event",M,{type:"event",buffered:!0,durationThreshold:0}))},N=0,R=function(){return B()-N},q=[],x={},H=function(e){var n=q[q.length-1],t=x[e.interactionId];if(t||q.length<10||e.duration>n.latency){if(t)t.entries.push(e),t.latency=Math.max(t.latency,e.duration);else{var r={id:e.interactionId,latency:e.duration,entries:[e]};x[r.id]=r,q.push(r)}q.sort((function(e,n){return n.latency-e.latency})),q.splice(10).forEach((function(e){delete x[e.id]}))}},O=function(e,n){n=n||{};var t=[200,500];D();var r,i=f("INP"),a=function(e){e.forEach((function(e){(e.interactionId&&H(e),"first-input"===e.entryType)&&(!q.some((function(n){return n.entries.some((function(n){return e.duration===n.duration&&e.startTime===n.startTime}))}))&&H(e))}));var n,t=(n=Math.min(q.length-1,Math.floor(R()/50)),q[n]);t&&t.latency!==i.value&&(i.value=t.latency,i.entries=t.entries,r())},c=s("event",a,{durationThreshold:n.durationThreshold||40});r=l(e,i,t,n.reportAllChanges),c&&(c.observe({type:"first-input",buffered:!0}),d((function(){a(c.takeRecords()),i.value<0&&R()>0&&(i.value=0,i.entries=[]),r(!0)})),o((function(){q=[],N=B(),i=f("INP"),r=l(e,i,t,n.reportAllChanges)})))},j={},_=function(e,n){n=n||{};var t,r=[2500,4e3],i=h(),a=f("LCP"),c=function(e){var n=e[e.length-1];if(n){var r=n.startTime-u();r<i.firstHiddenTime&&(a.value=r,a.entries=[n],t())}},p=s("largest-contentful-paint",c);if(p){t=l(e,a,r,n.reportAllChanges);var v=function(){j[a.id]||(c(p.takeRecords()),p.disconnect(),j[a.id]=!0,t(!0))};["keydown","click"].forEach((function(e){addEventListener(e,v,{once:!0,capture:!0})})),d(v,!0),o((function(i){a=f("LCP"),t=l(e,a,r,n.reportAllChanges),requestAnimationFrame((function(){requestAnimationFrame((function(){a.value=performance.now()-i.timeStamp,j[a.id]=!0,t(!0)}))}))}))}},z=function e(n){document.prerendering?addEventListener("prerenderingchange",(function(){return e(n)}),!0):"complete"!==document.readyState?addEventListener("load",(function(){return e(n)}),!0):setTimeout(n,0)},G=function(e,n){n=n||{};var t=[800,1800],r=f("TTFB"),i=l(e,r,t,n.reportAllChanges);z((function(){var a=c();if(a){if(r.value=Math.max(a.responseStart-u(),0),r.value<0||r.value>performance.now())return;r.entries=[a],i(!0),o((function(){r=f("TTFB",0),(i=l(e,r,t,n.reportAllChanges))(!0)}))}}))};export{E as getCLS,g as getFCP,F as getFID,O as getINP,_ as getLCP,G as getTTFB,E as onCLS,g as onFCP,F as onFID,O as onINP,_ as onLCP,G as onTTFB};