UNPKG

web-vitals

Version:

Easily measure performance metrics in JavaScript

2 lines (1 loc) 6.55 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).webVitals=e.webVitals||{})}(this,(function(e){"use strict";var n,t=-1,i=function(e){addEventListener("pageshow",(function(n){n.persisted&&(t=n.timeStamp,e(n))}),!0)},r=function(){return window.performance&&(performance.getEntriesByType&&performance.getEntriesByType("navigation")[0]||function(){var e=performance.timing,n=performance.navigation.type,t={entryType:"navigation",startTime:0,type:2==n?"back_forward":1===n?"reload":"navigate"};for(var i in e)"navigationStart"!==i&&"toJSON"!==i&&(t[i]=Math.max(e[i]-e.navigationStart,0));return t}())},a=function(){var e=r();return e&&e.activationStart||0},o=function(e,n){var i=r(),o="navigate";return t>=0?o="back-forward-cache":i&&(o=document.prerendering||a()>0?"prerender":i.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:o}},u=function(e,n,t){try{if(PerformanceObserver.supportedEntryTypes.includes(e)){var i=new PerformanceObserver((function(e){n(e.getEntries())}));return i.observe(Object.assign({type:e,buffered:!0},t||{})),i}}catch(e){}},c=function(e,n){var t=function t(i){"pagehide"!==i.type&&"hidden"!==document.visibilityState||(e(i),n&&(removeEventListener("visibilitychange",t,!0),removeEventListener("pagehide",t,!0)))};addEventListener("visibilitychange",t,!0),addEventListener("pagehide",t,!0)},f=function(e,n,t,i){var r,a;return function(o){n.value>=0&&(o||i)&&((a=n.value-(r||0))||void 0===r)&&(r=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))}},s=-1,l=function(){c((function(e){var n=e.timeStamp;s=n}),!0)},d=function(){return s<0&&((s=window.webVitals.firstHiddenTime)===1/0&&l(),i((function(){setTimeout((function(){s="hidden"!==document.visibilityState||document.prerendering?1/0:0,l()}),0)}))),{get firstHiddenTime(){return s}}},p=function(e,n){n=n||{};var t,r=[1800,3e3],c=d(),s=o("FCP"),l=function(e){e.forEach((function(e){"first-contentful-paint"===e.name&&(v&&v.disconnect(),e.startTime<c.firstHiddenTime&&(s.value=e.startTime-a(),s.entries.push(e),t(!0)))}))},p=window.performance&&window.performance.getEntriesByName&&window.performance.getEntriesByName("first-contentful-paint")[0],v=p?null:u("paint",l);(p||v)&&(t=f(e,s,r,n.reportAllChanges),p&&l([p]),i((function(i){s=o("FCP"),t=f(e,s,r,n.reportAllChanges),requestAnimationFrame((function(){requestAnimationFrame((function(){s.value=performance.now()-i.timeStamp,t(!0)}))}))})))},v=!1,m=-1,h=function(e,n){n=n||{};var t=[.1,.25];v||(p((function(e){m=e.value})),v=!0);var r,a=function(n){m>-1&&e(n)},s=o("CLS",0),l=0,d=[],h=function(e){e.forEach((function(e){if(!e.hadRecentInput){var n=d[0],t=d[d.length-1];l&&e.startTime-t.startTime<1e3&&e.startTime-n.startTime<5e3?(l+=e.value,d.push(e)):(l=e.value,d=[e]),l>s.value&&(s.value=l,s.entries=d,r())}}))},g=u("layout-shift",h);g&&(r=f(a,s,t,n.reportAllChanges),c((function(){h(g.takeRecords()),r(!0)})),i((function(){l=0,m=-1,s=o("CLS",0),r=f(a,s,t,n.reportAllChanges)})))},g=function(e,n){n=n||{};var t,r=[100,300],a=d(),s=o("FID"),l=function(e){e.startTime<a.firstHiddenTime&&(s.value=e.processingStart-e.startTime,s.entries.push(e),t(!0))},p=function(e){e.forEach(l)},v=u("first-input",p);t=f(e,s,r,n.reportAllChanges),v&&c((function(){p(v.takeRecords()),v.disconnect()}),!0),console.warn('The web-vitals "base+polyfill" build is deprecated. See: https://bit.ly/3aqzsGm'),v||window.webVitals.firstInputPolyfill(l),i((function(){s=o("FID"),t=f(e,s,r,n.reportAllChanges),window.webVitals.resetFirstInputPolyfill(),window.webVitals.firstInputPolyfill(l)}))},y=0,T=1/0,w=0,b=function(e){e.forEach((function(e){e.interactionId&&(T=Math.min(T,e.interactionId),w=Math.max(w,e.interactionId),y=w?(w-T)/7+1:0)}))},C=function(){return n?y:performance.interactionCount||0},E=function(){"interactionCount"in performance||n||(n=u("event",b,{type:"event",buffered:!0,durationThreshold:0}))},I=0,P=function(){return C()-I},S=[],F={},A=function(e){var n=S[S.length-1],t=F[e.interactionId];if(t||S.length<10||e.duration>n.latency){if(t)t.entries.push(e),t.latency=Math.max(t.latency,e.duration);else{var i={id:e.interactionId,latency:e.duration,entries:[e]};F[i.id]=i,S.push(i)}S.sort((function(e,n){return n.latency-e.latency})),S.splice(10).forEach((function(e){delete F[e.id]}))}},L=function(e,n){n=n||{};var t=[200,500];E();var r,a=o("INP"),s=function(e){e.forEach((function(e){(e.interactionId&&A(e),"first-input"===e.entryType)&&(!S.some((function(n){return n.entries.some((function(n){return e.duration===n.duration&&e.startTime===n.startTime}))}))&&A(e))}));var n,t=(n=Math.min(S.length-1,Math.floor(P()/50)),S[n]);t&&t.latency!==a.value&&(a.value=t.latency,a.entries=t.entries,r())},l=u("event",s,{durationThreshold:n.durationThreshold||40});r=f(e,a,t,n.reportAllChanges),l&&(l.observe({type:"first-input",buffered:!0}),c((function(){s(l.takeRecords()),a.value<0&&P()>0&&(a.value=0,a.entries=[]),r(!0)})),i((function(){S=[],I=C(),a=o("INP"),r=f(e,a,t,n.reportAllChanges)})))},M={},k=function(e,n){n=n||{};var t,r=[2500,4e3],s=d(),l=o("LCP"),p=function(e){var n=e[e.length-1];if(n){var i=n.startTime-a();i<s.firstHiddenTime&&(l.value=i,l.entries=[n],t())}},v=u("largest-contentful-paint",p);if(v){t=f(e,l,r,n.reportAllChanges);var m=function(){M[l.id]||(p(v.takeRecords()),v.disconnect(),M[l.id]=!0,t(!0))};["keydown","click"].forEach((function(e){addEventListener(e,m,{once:!0,capture:!0})})),c(m,!0),i((function(i){l=o("LCP"),t=f(e,l,r,n.reportAllChanges),requestAnimationFrame((function(){requestAnimationFrame((function(){l.value=performance.now()-i.timeStamp,M[l.id]=!0,t(!0)}))}))}))}},B=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)},x=function(e,n){n=n||{};var t=[800,1800],u=o("TTFB"),c=f(e,u,t,n.reportAllChanges);B((function(){var s=r();if(s){if(u.value=Math.max(s.responseStart-a(),0),u.value<0||u.value>performance.now())return;u.entries=[s],c(!0),i((function(){u=o("TTFB",0),(c=f(e,u,t,n.reportAllChanges))(!0)}))}}))};e.getCLS=h,e.getFCP=p,e.getFID=g,e.getINP=L,e.getLCP=k,e.getTTFB=x,e.onCLS=h,e.onFCP=p,e.onFID=g,e.onINP=L,e.onLCP=k,e.onTTFB=x,Object.defineProperty(e,"__esModule",{value:!0})}));