// snippets/Loading/Server-Timing-Early-Hints.js | sha256:5d59b89aac1ec7b3 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Server-Timing-Early-Hints.js (()=>{const e="Server-Timing-Early-Hints",[r]=performance.getEntriesByType("navigation");if(!r)return{script:e,status:"error",error:"No navigation entry available"};if(!Array.isArray(r.serverTiming))return{script:e,status:"unsupported",error:"Server-Timing entries are not supported in this browser"};const i=e=>Math.round(100*e)/100,s=Math.max(0,r.activationStart??0),n=e=>e>0?i(Math.max(0,e-s)):0,t=(e,r,s)=>({source:r,url:s,name:e.name,durationMs:i(e.duration||0),description:e.description||""}),o=r.serverTiming.map(e=>t(e,"navigation",location.href));let a=0,d=0;for(const e of performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets"))){if(e.serverTiming.length>0){a++;for(const r of e.serverTiming)o.push(t(r,"resource",e.name));continue}let r=!1;try{r=new URL(e.name).origin!==location.origin}catch{}r&&0===e.transferSize&&0===e.encodedBodySize&&0===e.decodedBodySize&&d++}o.sort((e,r)=>r.durationMs-e.durationMs);const c=o.slice(0,50),m=n(r.firstInterimResponseStart??0),g=n(r.finalResponseHeadersStart??r.responseStart),p=m>0,l={responseStartMs:n(r.responseStart),firstInterimResponseStartMs:m,finalResponseHeadersStartMs:g,earlyHintsReceived:p,earlyHintsLeadMs:p?i(Math.max(0,g-m)):0,serverTimingCount:r.serverTiming.length},h=[];return 0===r.serverTiming.length&&h.push({code:"no-server-timing",severity:"info",message:"The document response sends no Server-Timing header. Add one, for example Server-Timing: db;dur=53, to expose backend timings in DevTools and in the Performance API."}),d>0&&h.push({code:"timing-hidden",severity:"info",message:`${d} cross-origin resource(s) hide their timing because they lack Timing-Allow-Origin, so their Server-Timing cannot be read. Add Timing-Allow-Origin to expose it.`}),p||h.push({code:"no-early-hints",severity:"info",message:"No 103 Early Hints response was observed for the document. If the server can send Link preload or preconnect headers before the final response, Early Hints lets the browser start those fetches earlier."}),{script:e,status:"ok",count:o.length,corsLimitedAnalysis:d>0,details:{navigation:l,resourcesWithServerTiming:a,corsRestrictedCount:d},items:c,issues:h}})();