Skip to content

Instantly share code, notes, and snippets.

@moeriki
Created April 21, 2023 12:35
Show Gist options
  • Save moeriki/ef2011f4ebf29012ecf4c667f3f2bd32 to your computer and use it in GitHub Desktop.
Save moeriki/ef2011f4ebf29012ecf4c667f3f2bd32 to your computer and use it in GitHub Desktop.
var webVitals=function(e){"use strict";var t,n=function(){return window.performance&&(performance.getEntriesByType&&performance.getEntriesByType("navigation")[0]||function(){var e=performance.timing,t=performance.navigation.type,n={entryType:"navigation",startTime:0,type:2==t?"back_forward":1===t?"reload":"navigate"};for(var r in e)"navigationStart"!==r&&"toJSON"!==r&&(n[r]=Math.max(e[r]-e.navigationStart,0));return n}())},r=function(e){if("loading"===document.readyState)return"loading";var t=n();if(t){if(e<t.domInteractive)return"loading";if(0===t.domContentLoadedEventStart||e<t.domContentLoadedEventStart)return"dom-interactive";if(0===t.domComplete||e<t.domComplete)return"dom-content-loaded"}return"complete"},i=function(e){var t=e.nodeName;return 1===e.nodeType?t.toLowerCase():t.toUpperCase().replace(/^#/,"")},a=function(e,t){var n="";try{for(;e&&9!==e.nodeType;){var r=e,a=r.id?"#"+r.id:i(r)+(r.className&&r.className.trim().length?"."+r.className.trim().replace(/\s+/g,"."):"");if(n.length+a.length>(t||100)-1)return n||a;if(n=n?a+">"+n:a,r.id)break;e=r.parentNode}}catch(e){}return n},o=-1,u=function(){return o},c=function(e){addEventListener("pageshow",(function(t){t.persisted&&(o=t.timeStamp,e(t))}),!0)},s=function(){var e=n();return e&&e.activationStart||0},f=function(e,t){var r=n(),i="navigate";return u()>=0?i="back-forward-cache":r&&(document.prerendering||s()>0?i="prerender":document.wasDiscarded?i="restore":r.type&&(i=r.type.replace(/_/g,"-"))),{name:e,value:void 0===t?-1:t,rating:"good",delta:0,entries:[],id:"v3-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},d=function(e,t,n){try{if(PerformanceObserver.supportedEntryTypes.includes(e)){var r=new PerformanceObserver((function(e){Promise.resolve().then((function(){t(e.getEntries())}))}));return r.observe(Object.assign({type:e,buffered:!0},n||{})),r}}catch(e){}},l=function(e,t,n,r){var i,a;return function(o){t.value>=0&&(o||r)&&((a=t.value-(i||0))||void 0===i)&&(i=t.value,t.delta=a,t.rating=function(e,t){return e>t[1]?"poor":e>t[0]?"needs-improvement":"good"}(t.value,n),e(t))}},m=function(e){requestAnimationFrame((function(){return requestAnimationFrame((function(){return e()}))}))},v=function(e){var t=function(t){"pagehide"!==t.type&&"hidden"!==document.visibilityState||e(t)};addEventListener("visibilitychange",t,!0),addEventListener("pagehide",t,!0)},p=function(e){var t=!1;return function(n){t||(e(n),t=!0)}},h=-1,g=function(e){"hidden"===document.visibilityState&&h>-1&&(h="visibilitychange"===e.type?e.timeStamp:0,y())},T=function(){addEventListener("visibilitychange",g,!0),addEventListener("prerenderingchange",g,!0)},y=function(){removeEventListener("visibilitychange",g,!0),removeEventListener("prerenderingchange",g,!0)},S=function(){return h<0&&((h=window.webVitals.firstHiddenTime)===1/0&&T(),c((function(){setTimeout((function(){h="hidden"!==document.visibilityState||document.prerendering?1/0:0,T()}),0)}))),{get firstHiddenTime(){return h}}},b=function(e){document.prerendering?addEventListener("prerenderingchange",(function(){return e()}),!0):e()},E=[1800,3e3],C=function(e,t){t=t||{},b((function(){var n,r=S(),i=f("FCP"),a=d("paint",(function(e){e.forEach((function(e){"first-contentful-paint"===e.name&&(a.disconnect(),e.startTime<r.firstHiddenTime&&(i.value=Math.max(e.startTime-s(),0),i.entries.push(e),n(!0)))}))}));a&&(n=l(e,i,E,t.reportAllChanges),c((function(r){i=f("FCP"),n=l(e,i,E,t.reportAllChanges),m((function(){i.value=performance.now()-r.timeStamp,n(!0)}))})))}))},w=[.1,.25],L=[100,300],F=0,P=1/0,M=0,I=function(e){e.forEach((function(e){e.interactionId&&(P=Math.min(P,e.interactionId),M=Math.max(M,e.interactionId),F=M?(M-P)/7+1:0)}))},x=function(){return t?F:performance.interactionCount||0},A=function(){"interactionCount"in performance||t||(t=d("event",I,{type:"event",buffered:!0,durationThreshold:0}))},B=[200,500],k=0,D=function(){return x()-k},N=[],R={},q=function(e){var t=N[N.length-1],n=R[e.interactionId];if(n||N.length<10||e.duration>t.latency){if(n)n.entries.push(e),n.latency=Math.max(n.latency,e.duration);else{var r={id:e.interactionId,latency:e.duration,entries:[e]};R[r.id]=r,N.push(r)}N.sort((function(e,t){return t.latency-e.latency})),N.splice(10).forEach((function(e){delete R[e.id]}))}},V=function(e,t){t=t||{},b((function(){A();var n,r=f("INP"),i=function(e){e.forEach((function(e){(e.interactionId&&q(e),"first-input"===e.entryType)&&(!N.some((function(t){return t.entries.some((function(t){return e.duration===t.duration&&e.startTime===t.startTime}))}))&&q(e))}));var t,i=(t=Math.min(N.length-1,Math.floor(D()/50)),N[t]);i&&i.latency!==r.value&&(r.value=i.latency,r.entries=i.entries,n())},a=d("event",i,{durationThreshold:t.durationThreshold||40});n=l(e,r,B,t.reportAllChanges),a&&(a.observe({type:"first-input",buffered:!0}),v((function(){i(a.takeRecords()),r.value<0&&D()>0&&(r.value=0,r.entries=[]),n(!0)})),c((function(){N=[],k=x(),r=f("INP"),n=l(e,r,B,t.reportAllChanges)})))}))},H=[2500,4e3],O={},_=[800,1800],j=function e(t){document.prerendering?b((function(){return e(t)})):"complete"!==document.readyState?addEventListener("load",(function(){return e(t)}),!0):setTimeout(t,0)},z=function(e,t){t=t||{};var r=f("TTFB"),i=l(e,r,_,t.reportAllChanges);j((function(){var a=n();if(a){var o=a.responseStart;if(o<=0||o>performance.now())return;r.value=Math.max(o-s(),0),r.entries=[a],i(!0),c((function(){r=f("TTFB",0),(i=l(e,r,_,t.reportAllChanges))(!0)}))}}))};return e.CLSThresholds=w,e.FCPThresholds=E,e.FIDThresholds=L,e.INPThresholds=B,e.LCPThresholds=H,e.TTFBThresholds=_,e.onCLS=function(e,t){!function(e,t){t=t||{},C(p((function(){var n,r=f("CLS",0),i=0,a=[],o=function(e){e.forEach((function(e){if(!e.hadRecentInput){var t=a[0],n=a[a.length-1];i&&e.startTime-n.startTime<1e3&&e.startTime-t.startTime<5e3?(i+=e.value,a.push(e)):(i=e.value,a=[e])}})),i>r.value&&(r.value=i,r.entries=a,n())},u=d("layout-shift",o);u&&(n=l(e,r,w,t.reportAllChanges),v((function(){o(u.takeRecords()),n(!0)})),c((function(){i=0,r=f("CLS",0),n=l(e,r,w,t.reportAllChanges),m((function(){return n()}))})),setTimeout(n,0))})))}((function(t){!function(e){if(e.entries.length){var t=e.entries.reduce((function(e,t){return e&&e.value>t.value?e:t}));if(t&&t.sources&&t.sources.length){var n=(i=t.sources).find((function(e){return e.node&&1===e.node.nodeType}))||i[0];if(n)return void(e.attribution={largestShiftTarget:a(n.node),largestShiftTime:t.startTime,largestShiftValue:t.value,largestShiftSource:n,largestShiftEntry:t,loadState:r(t.startTime)})}}var i;e.attribution={}}(t),e(t)}),t)},e.onFCP=function(e,t){C((function(t){!function(e){if(e.entries.length){var t=n(),i=e.entries[e.entries.length-1];if(t){var a=t.activationStart||0,o=Math.max(0,t.responseStart-a);return void(e.attribution={timeToFirstByte:o,firstByteToFCP:e.value-o,loadState:r(e.entries[0].startTime),navigationEntry:t,fcpEntry:i})}}e.attribution={timeToFirstByte:0,firstByteToFCP:e.value,loadState:r(u())}}(t),e(t)}),t)},e.onFID=function(e,t){!function(e,t){t=t||{},b((function(){var n,r=S(),i=f("FID"),a=function(e){e.startTime<r.firstHiddenTime&&(i.value=e.processingStart-e.startTime,i.entries.push(e),n(!0))},o=function(e){e.forEach(a)},u=d("first-input",o);n=l(e,i,L,t.reportAllChanges),u&&v(p((function(){o(u.takeRecords()),u.disconnect()}))),console.warn('The web-vitals "base+polyfill" build is deprecated. See: https://bit.ly/3aqzsGm'),u||window.webVitals.firstInputPolyfill(a),c((function(){i=f("FID"),n=l(e,i,L,t.reportAllChanges),window.webVitals.resetFirstInputPolyfill(),window.webVitals.firstInputPolyfill(a)}))}))}((function(t){!function(e){var t=e.entries[0];e.attribution={eventTarget:a(t.target),eventType:t.name,eventTime:t.startTime,eventEntry:t,loadState:r(t.startTime)}}(t),e(t)}),t)},e.onINP=function(e,t){V((function(t){!function(e){if(e.entries.length){var t=e.entries.sort((function(e,t){return t.duration-e.duration||t.processingEnd-t.processingStart-(e.processingEnd-e.processingStart)}))[0];e.attribution={eventTarget:a(t.target),eventType:t.name,eventTime:t.startTime,eventEntry:t,loadState:r(t.startTime)}}else e.attribution={}}(t),e(t)}),t)},e.onLCP=function(e,t){!function(e,t){t=t||{},b((function(){var n,r=S(),i=f("LCP"),a=function(e){var t=e[e.length-1];t&&t.startTime<r.firstHiddenTime&&(i.value=Math.max(t.startTime-s(),0),i.entries=[t],n())},o=d("largest-contentful-paint",a);if(o){n=l(e,i,H,t.reportAllChanges);var u=p((function(){O[i.id]||(a(o.takeRecords()),o.disconnect(),O[i.id]=!0,n(!0))}));["keydown","click"].forEach((function(e){addEventListener(e,u,!0)})),v(u),c((function(r){i=f("LCP"),n=l(e,i,H,t.reportAllChanges),m((function(){i.value=performance.now()-r.timeStamp,O[i.id]=!0,n(!0)}))}))}}))}((function(t){!function(e){if(e.entries.length){var t=n();if(t){var r=t.activationStart||0,i=e.entries[e.entries.length-1],o=i.url&&performance.getEntriesByType("resource").filter((function(e){return e.name===i.url}))[0],u=Math.max(0,t.responseStart-r),c=Math.max(u,o?(o.requestStart||o.startTime)-r:0),s=Math.max(c,o?o.responseEnd-r:0),f=Math.max(s,i?i.startTime-r:0),d={element:a(i.element),timeToFirstByte:u,resourceLoadDelay:c-u,resourceLoadTime:s-c,elementRenderDelay:f-s,navigationEntry:t,lcpEntry:i};return i.url&&(d.url=i.url),o&&(d.lcpResourceEntry=o),void(e.attribution=d)}}e.attribution={timeToFirstByte:0,resourceLoadDelay:0,resourceLoadTime:0,elementRenderDelay:e.value}}(t),e(t)}),t)},e.onTTFB=function(e,t){z((function(t){!function(e){if(e.entries.length){var t=e.entries[0],n=t.activationStart||0,r=Math.max(t.domainLookupStart-n,0),i=Math.max(t.connectStart-n,0),a=Math.max(t.requestStart-n,0);e.attribution={waitingTime:r,dnsTime:i-r,connectionTime:a-i,requestTime:e.value-a,navigationEntry:t}}else e.attribution={waitingTime:0,dnsTime:0,connectionTime:0,requestTime:0}}(t),e(t)}),t)},Object.defineProperty(e,"__esModule",{value:!0}),e}({});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment