// snippets/CoreWebVitals/LCP-Subparts.js | sha256:b165d410cc3983ea | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/CoreWebVitals/LCP-Subparts.js (async()=>{if(!PerformanceObserver.supportedEntryTypes?.includes("largest-contentful-paint"))return{script:"LCP-Subparts",status:"unsupported",error:"largest-contentful-paint entries not supported in this browser"};const e=e=>e<=2500?"good":e<=4e3?"needs-improvement":"poor",t=[{name:"Time to First Byte",key:"ttfb",target:800},{name:"Resource Load Delay",key:"resourceLoadDelay",targetPercent:10},{name:"Resource Load Time",key:"resourceLoadTime",targetPercent:40},{name:"Element Render Delay",key:"elementRenderDelay",targetPercent:10}],r=(e,t,r,a)=>{if(!e?.responseStart)return null;const n=Math.min(Math.max(t,e.responseStart-a),r);return{waiting:n-t,download:r-n}},a=(e,t)=>{const r=t.activationStart||0,a=Math.max(0,e.startTime-r),n=performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets")).findLast(t=>t.name===e.url&&(t.requestStart||t.startTime)<=e.startTime),o=Math.max(0,t.responseStart-r),s=Math.max(o,n?(n.requestStart||n.startTime)-r:0);return{resEntry:n,activationStart:r,ttfb:o,requestStart:s,responseEnd:Math.min(a,Math.max(s,n?n.responseEnd-r:0)),renderTime:a}},n=()=>{const e=performance.getEntriesByType("navigation")[0];return e?.responseStart>0&&e.responseStart{const s=o.getEntries().at(-1);if(!s)return;const u=n();if(!u)return;const{resEntry:l,activationStart:i,ttfb:c,requestStart:p,responseEnd:m,renderTime:d}=a(s,u),y=(r(l,p,m,i),{ttfb:c,resourceLoadDelay:p-c,resourceLoadTime:m-p,elementRenderDelay:d-m});e(d);if(s.element){const e=s.element;s.url&&(()=>{try{const e=new URL(s.url);return e.hostname!==location.hostname?`${e.hostname}/…/${e.pathname.split("/").pop()?.split("?")[0]}`:e.pathname.split("/").pop()?.split("?")[0]||s.url}catch{return s.url}})(),e.style.outline="3px dashed lime",e.style.outlineOffset="2px"}const f=t.map(e=>({...e,value:y[e.key],percent:y[e.key]/d*100})),h=f.reduce((e,t)=>e.value>t.value?e:t),v=(f.map(e=>{const t=e.target?e.value>e.target:e.percent>e.targetPercent;return{"Sub-part":e.key===h.key?`⚠️ ${e.name}`:e.name,Time:(n=e.value,`${Math.round(n)}ms`),"%":(r=e.value,a=d,`${Math.round(r/a*100)}%`),Status:t?"🔴 Over target":"✅ OK"};var r,a,n}),f.map(e=>{const t=Math.max(1,Math.round(e.value/d*40));return{key:e.key,bar:t}}));"█".repeat(v[0].bar),"▓".repeat(v[1].bar),"▒".repeat(v[2].bar),"░".repeat(v[3].bar),t.forEach(e=>performance.clearMeasures(e.name)),f.forEach(e=>{const t={ttfb:0,resourceLoadDelay:c,resourceLoadTime:p,elementRenderDelay:m};performance.measure(e.name,{start:t[e.key],end:t[e.key]+e.value})})});o.observe({type:"largest-contentful-paint",buffered:!0});const s=await new Promise(e=>{const t=[],r=new PerformanceObserver(e=>t.push(...e.getEntries()));r.observe({type:"largest-contentful-paint",buffered:!0}),setTimeout(()=>{r.disconnect(),e(t.at(-1)??null)},100)});if(!s)return{script:"LCP-Subparts",status:"error",error:"No LCP entries buffered"};const u=n();if(!u)return{script:"LCP-Subparts",status:"error",error:"No navigation entry"};const{resEntry:l,activationStart:i,ttfb:c,requestStart:p,responseEnd:m,renderTime:d}=a(s,u),y=Math.round(d),f=e(y),h=Math.round(c),v=Math.round(p-c),b=Math.round(m-p),g=Math.round(d-m),S=r(l,p,m,i),T=S?Math.min(b,Math.round(S.waiting)):null,M=[{key:"ttfb",value:h},{key:"resourceLoadDelay",value:v},{key:"resourceLoadTime",value:b},{key:"elementRenderDelay",value:g}].reduce((e,t)=>e.value>t.value?e:t).key;let L=null;return s.element&&(L=function(e){const t=e.getRootNode(),r=e=>{try{return 1===t.querySelectorAll(e).length}catch{return!1}},a=[];let n=e;for(;n&&1===n.nodeType;){if(n.id&&r(`#${window.CSS.escape(n.id)}`)){a.unshift(`#${window.CSS.escape(n.id)}`);break}const e=n.localName;let t=e+Array.from(n.classList).slice(0,2).map(e=>`.${window.CSS.escape(e)}`).join("");const o=n.parentNode?Array.from(n.parentNode.children):[n];if(o.filter(e=>e.matches(t)).length>1&&(t+=`:nth-of-type(${o.filter(t=>t.localName===e).indexOf(n)+1})`),a.unshift(t),r(a.join(" > ")))break;n=n.parentElement}return a.join(" > ")}(s.element)),{script:"LCP-Subparts",status:"ok",metric:"LCP",value:y,unit:"ms",rating:f,thresholds:{good:2500,needsImprovement:4e3},details:{element:L,url:s.url?(()=>{try{const e=new URL(s.url);return e.hostname!==location.hostname?`${e.hostname}/…/${e.pathname.split("/").pop()?.split("?")[0]}`:e.pathname.split("/").pop()?.split("?")[0]||s.url}catch{return s.url}})():null,subParts:{ttfb:{value:h,percent:Math.round(h/y*100),overTarget:h>800},resourceLoadDelay:{value:v,percent:Math.round(v/y*100),overTarget:v/y*100>10},resourceLoadTime:{value:b,percent:Math.round(b/y*100),overTarget:b/y*100>40,...S?{waiting:T,download:b-T}:{}},elementRenderDelay:{value:g,percent:Math.round(g/y*100),overTarget:g/y*100>10}},slowestPhase:M}}})();