UNPKG

3.45 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).webVitals={})}(this,(function(t){"use strict";var e=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)}},n=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){}},i=!1,a=function(t,e){i||"undefined"!=typeof InstallTrigger||(addEventListener("beforeunload",(function(){})),i=!0);addEventListener("visibilitychange",(function n(i){"hidden"===document.visibilityState&&(t(i),e&&removeEventListener("visibilitychange",n,!0))}),!0)},o=function(t){addEventListener("pageshow",(function(e){e.persisted&&t(e)}),!0)},r=new WeakSet,u=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)))}},c=-1,s=function(){a((function(t){var e=t.timeStamp;c=e}),!0)},f=function(){return c<0&&((c=self.webVitals.firstHiddenTime)===1/0&&s(),o((function(){setTimeout((function(){c="hidden"===document.visibilityState?0:1/0,s()}),0)}))),{get timeStamp(){return c}}};t.getCLS=function(t,i){var r,c=e("CLS",0),s=function(t){t.hadRecentInput||(c.value+=t.value,c.entries.push(t),r())},f=n("layout-shift",s);f&&(r=u(t,c,i),a((function(){f.takeRecords().map(s),r()})),o((function(){c=e("CLS",0),r=u(t,c,i)})))},t.getFCP=function(t,i){var a,c=f(),s=e("FCP"),d=n("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),a()))}));d&&(a=u(t,s,i),o((function(n){s=e("FCP"),a=u(t,s,i),requestAnimationFrame((function(){requestAnimationFrame((function(){s.value=performance.now()-n.timeStamp,r.add(s),a()}))}))})))},t.getFID=function(t,i){var c,s=f(),d=e("FID"),l=function(t){t.startTime<s.timeStamp&&(d.value=t.processingStart-t.startTime,d.entries.push(t),r.add(d),c())},m=n("first-input",l);c=u(t,d,i),m&&a((function(){m.takeRecords().map(l),m.disconnect()}),!0),m||window.webVitals.firstInputPolyfill(l),o((function(){d=e("FID"),c=u(t,d,i),window.webVitals.resetFirstInputPolyfill(),window.webVitals.firstInputPolyfill(l)}))},t.getLCP=function(t,i){var c,s=f(),d=e("LCP"),l=function(t){var e=t.startTime;e<s.timeStamp&&(d.value=e,d.entries.push(t)),c()},m=n("largest-contentful-paint",l);if(m){c=u(t,d,i);var p=function(){r.has(d)||(m.takeRecords().map(l),m.disconnect(),r.add(d),c())};["keydown","click"].map((function(t){addEventListener(t,p,{once:!0,capture:!0})})),a(p,!0),o((function(n){d=e("LCP"),c=u(t,d,i),requestAnimationFrame((function(){requestAnimationFrame((function(){d.value=performance.now()-n.timeStamp,r.add(d),c()}))}))}))}},t.getTTFB=function(t){var n,i=e("TTFB");n=function(){try{var e=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=e.responseStart,i.entries=[e],t(i)}catch(t){}},"complete"===document.readyState?setTimeout(n,0):addEventListener("pageshow",n)},Object.defineProperty(t,"__esModule",{value:!0})}));