// snippets/Loading/FCP.js | sha256:cd37fc62ecf86388 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/FCP.js (()=>{function e(e){return e.name.endsWith("#webperf-snippets")}const t=e=>e<=1800?"good":e<=3e3?"needs-improvement":"poor",r=!!window.PerformanceResourceTiming&&"renderBlockingStatus"in window.PerformanceResourceTiming.prototype;new PerformanceObserver(n=>{const o=n.getEntriesByName("first-contentful-paint")[0];if(!o)return;const a=performance.getEntriesByType("navigation")[0],s=a?.activationStart||0,i=Math.max(0,o.startTime-s),p=(t(i),Math.max(0,(a?.responseStart??0)-s)),c=performance.getEntriesByType("resource").filter(t=>!e(t)).filter(e=>"blocking"===e.renderBlockingStatus),m=c.length?Math.max(...c.map(e=>e.responseEnd))-s:p;r&&c.length>0&&(Math.max(0,m-p),c.sort((e,t)=>t.responseEnd-e.responseEnd).forEach(e=>{e.name.split("/").pop().split("?")}))}).observe({type:"paint",buffered:!0});const n=performance.getEntriesByName("first-contentful-paint")[0];if(!n)return{script:"FCP",status:"error",error:"No FCP entry yet"};const o=performance.getEntriesByType("navigation")[0],a=o?.activationStart||0,s=Math.max(0,n.startTime-a),i=t(s),p=Math.max(0,(o?.responseStart??0)-a),c=r?performance.getEntriesByType("resource").filter(t=>!e(t)).filter(e=>"blocking"===e.renderBlockingStatus):[],m=Math.max(p,...c.map(e=>e.responseEnd-a)),l=Math.round(p),u=r?Math.round(m-p):null,d=r?Math.max(0,Math.round(s)-l-u):null;return{script:"FCP",status:"ok",metric:"FCP",value:Math.round(s),unit:"ms",rating:i,thresholds:{good:1800,needsImprovement:3e3},details:{ttfbMs:l,renderBlockingLoadMs:u,renderDelayMs:d,blockingResourceCount:r?c.length:null,renderBlockingSupported:r},items:[...c].sort((e,t)=>t.responseEnd-e.responseEnd).slice(0,50).map(e=>({url:e.name.split("/").pop().split("?")[0]||e.name,type:"link"===e.initiatorType?"CSS":"JS",durationMs:Math.round(e.duration)}))}})();