UNPKG

3.19 kBJavaScriptView Raw
1var t=function(t,e){return{name:t,value:void 0===e?-1:0,delta:0,entries:[],id:"v1-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12)}},e=function(t,e){try{if(PerformanceObserver.supportedEntryTypes.includes(t)){var n=new PerformanceObserver((function(t){return t.getEntries().map(e)}));return n.observe({type:t,buffered:!0}),n}}catch(t){}},n=!1,i=function(t,e){n||"undefined"!=typeof InstallTrigger||(addEventListener("beforeunload",(function(){})),n=!0);addEventListener("visibilitychange",(function n(i){"hidden"===document.visibilityState&&(t(i),e&&removeEventListener("visibilitychange",n,!0))}),!0)},a=function(t){addEventListener("pageshow",(function(e){e.persisted&&t(e)}),!0)},r=new WeakSet,o=function(t,e,n){var i;return function(){e.value>=0&&(n||r.has(e)||"hidden"===document.visibilityState)&&(e.delta=e.value-(i||0),(e.delta||void 0===i)&&(i=e.value,t(e)))}},u=function(n,r){var u,c=t("CLS",0),s=function(t){t.hadRecentInput||(c.value+=t.value,c.entries.push(t),u())},f=e("layout-shift",s);f&&(u=o(n,c,r),i((function(){f.takeRecords().map(s),u()})),a((function(){c=t("CLS",0),u=o(n,c,r)})))},c=-1,s=function(){i((function(t){var e=t.timeStamp;c=e}),!0)},f=function(){return c<0&&((c=self.webVitals.firstHiddenTime)===1/0&&s(),a((function(){setTimeout((function(){c="hidden"===document.visibilityState?0:1/0,s()}),0)}))),{get timeStamp(){return c}}},d=function(n,i){var u,c=f(),s=t("FCP"),d=e("paint",(function(t){"first-contentful-paint"===t.name&&(d&&d.disconnect(),t.startTime<c.timeStamp&&(s.value=t.startTime,s.entries.push(t),r.add(s),u()))}));d&&(u=o(n,s,i),a((function(e){s=t("FCP"),u=o(n,s,i),requestAnimationFrame((function(){requestAnimationFrame((function(){s.value=performance.now()-e.timeStamp,r.add(s),u()}))}))})))},m=function(n,u){var c,s=f(),d=t("FID"),m=function(t){t.startTime<s.timeStamp&&(d.value=t.processingStart-t.startTime,d.entries.push(t),r.add(d),c())},v=e("first-input",m);c=o(n,d,u),v&&i((function(){v.takeRecords().map(m),v.disconnect()}),!0),v||window.webVitals.firstInputPolyfill(m),a((function(){d=t("FID"),c=o(n,d,u),window.webVitals.resetFirstInputPolyfill(),window.webVitals.firstInputPolyfill(m)}))},v=function(n,u){var c,s=f(),d=t("LCP"),m=function(t){var e=t.startTime;e<s.timeStamp&&(d.value=e,d.entries.push(t)),c()},v=e("largest-contentful-paint",m);if(v){c=o(n,d,u);var l=function(){r.has(d)||(v.takeRecords().map(m),v.disconnect(),r.add(d),c())};["keydown","click"].map((function(t){addEventListener(t,l,{once:!0,capture:!0})})),i(l,!0),a((function(e){d=t("LCP"),c=o(n,d,u),requestAnimationFrame((function(){requestAnimationFrame((function(){d.value=performance.now()-e.timeStamp,r.add(d),c()}))}))}))}},l=function(e){var n,i=t("TTFB");n=function(){try{var t=performance.getEntriesByType("navigation")[0]||function(){var t=performance.timing,e={entryType:"navigation",startTime:0};for(var n in t)"navigationStart"!==n&&"toJSON"!==n&&(e[n]=Math.max(t[n]-t.navigationStart,0));return e}();i.value=i.delta=t.responseStart,i.entries=[t],e(i)}catch(t){}},"complete"===document.readyState?setTimeout(n,0):addEventListener("pageshow",n)};export{u as getCLS,d as getFCP,m as getFID,v as getLCP,l as getTTFB};