// snippets/Loading/TTFB-Resources.js | sha256:358f34a389183974 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/TTFB-Resources.js (()=>{function t(t){return t.name.endsWith("#webperf-snippets")}const e=[],r=t=>t.responseStart-t.requestStart,s=t=>t.responseStart>0;function n(t){const e=t.replace(/\.$/,"");if(/^\d{1,3}(\.\d{1,3}){3}$/.test(e)||e.includes(":"))return e;const r=e.split(".");return r.length<=2?e:2===r[r.length-1].length&&["ac","co","com","edu","go","gob","gouv","gov","govt","mil","ne","net","nom","or","org","sch"].includes(r[r.length-2])?r.slice(-3).join("."):r.slice(-2).join(".")}const i=t=>{try{return!function(t){const r=n(t);return r===n(location.hostname)||e.some(t=>n(String(t).trim().toLowerCase().replace(/^[a-z]+:\/\//,"").split("/")[0])===r)}(new URL(t).hostname)}catch{return!1}};new PerformanceObserver(e=>{const n=e.getEntries().filter(e=>!t(e)),o=(n.filter(t=>!s(t)),n.filter(s).map(t=>{const e=i(t.name);return{ttfb:r(t),duration:t.duration,type:t.initiatorType,thirdParty:e,resource:t.name.length>70?"..."+t.name.slice(-67):t.name,fullUrl:t.name}}).sort((t,e)=>e.ttfb-t.ttfb));if(0===o.length)return;const a=o.map(t=>t.ttfb),c=(a.reduce((t,e)=>t+e,0),Math.max(...a),Math.min(...a),o.filter(t=>t.thirdParty),o.filter(t=>t.ttfb>500),o.slice(0,25).map(t=>({"TTFB (ms)":t.ttfb.toFixed(0),"Duration (ms)":t.duration.toFixed(0),Type:t.type,"3rd Party":t.thirdParty?"Yes":"",Resource:t.resource})),o.slice(0,5));c[0].ttfb>500&&c.forEach((t,e)=>{})}).observe({type:"resource",buffered:!0});const o=performance.getEntriesByType("resource").filter(e=>!t(e)),a=o.filter(t=>!s(t)).length,c=o.filter(s).map(t=>({url:t.name,ttfbMs:Math.round(r(t)),durationMs:Math.round(t.duration),type:t.initiatorType,isThirdParty:i(t.name)})).sort((t,e)=>e.ttfbMs-t.ttfbMs);if(0===c.length)return{script:"TTFB-Resources",status:"ok",count:0,corsLimitedAnalysis:a>0,details:{avgTtfbMs:null,maxTtfbMs:null,minTtfbMs:null,thirdPartyCount:0,slowCount:0,corsRestrictedCount:a},items:[],issues:[{code:"no-resources",severity:"info",message:"No resources with TTFB data available"}]};const u=c.map(t=>t.ttfbMs);return{script:"TTFB-Resources",status:"ok",count:c.length,corsLimitedAnalysis:a>0,details:{avgTtfbMs:Math.round(u.reduce((t,e)=>t+e,0)/u.length),maxTtfbMs:Math.max(...u),minTtfbMs:Math.min(...u),thirdPartyCount:c.filter(t=>t.isThirdParty).length,slowCount:c.filter(t=>t.ttfbMs>500).length,corsRestrictedCount:a},items:c.slice(0,50)}})();