// snippets/Loading/Third-Party-Impact-by-Domain.js | sha256:c0d214a959c10e80 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Third-Party-Impact-by-Domain.js (()=>{const e=[];function n(e){const n=e.replace(/\.$/,"");if(/^\d{1,3}(\.\d{1,3}){3}$/.test(n)||n.includes(":"))return n;const t=n.split(".");return t.length<=2?n:2===t[t.length-1].length&&["ac","co","com","edu","go","gob","gouv","gov","govt","mil","ne","net","nom","or","org","sch"].includes(t[t.length-2])?t.slice(-3).join("."):t.slice(-2).join(".")}function t(t){const r=n(t);return r===n(location.hostname)||e.some(e=>n(String(e).trim().toLowerCase().replace(/^[a-z]+:\/\//,"").split("/")[0])===r)}const r=e=>{try{const r=new URL(e);return/^https?:$/.test(r.protocol)?{root:n(r.hostname),first:t(r.hostname)}:null}catch{return null}},o=!!window.PerformanceResourceTiming&&"renderBlockingStatus"in window.PerformanceResourceTiming.prototype,s=new Map,i=e=>(s.has(e)||s.set(e,{domain:e,requests:0,transferBytes:0,sizeUnknownCount:0,renderBlocking:!o&&null,loafMs:null,loafScripts:0}),s.get(e));let a=0,l=0,c=0,d=0;performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets")).forEach(e=>{const n=r(e.name);if(!n)return;if(n.first)return void a++;const t=i(n.root);if(t.requests++,l++,e.transferSize>0||e.encodedBodySize>0||e.decodedBodySize>0){const n=e.transferSize||e.encodedBodySize||0;t.transferBytes+=n,c+=n}else t.sizeUnknownCount++,d++;!o||"blocking"!==e.renderBlockingStatus||"script"!==e.initiatorType&&"link"!==e.initiatorType||(t.renderBlocking=!0)});const u=!!PerformanceObserver.supportedEntryTypes?.includes("long-animation-frame");let m=null,f=null,h=null;if(u){m=0,s.forEach(e=>{e.loafMs=0});const e=performance.getEntriesByType("long-animation-frame");f=e.length,h=e.reduce((e,n)=>e+n.duration,0),e.forEach(e=>{(e.scripts||[]).forEach(e=>{const n=r(e.sourceURL);if(!n||n.first)return;const t=i(n.root);t.loafMs=(t.loafMs||0)+e.duration,t.loafScripts++,m+=e.duration})}),s.forEach(e=>{e.loafMs=Math.round(e.loafMs||0)}),m=Math.round(m),h=Math.round(h)}const g=u?Math.max(0,h-m):null,p=u&&s.size>0&&0===m&&f>0,y=[...s.values()].sort((e,n)=>Number(n.renderBlocking)-Number(e.renderBlocking)||(n.loafMs||0)-(e.loafMs||0)||n.transferBytes-e.transferBytes||e.domain.localeCompare(n.domain)),k=y.filter(e=>e.renderBlocking),w=y.filter(e=>(e.loafMs||0)>250),B=d>0,b=[];var v;return k.length>0&&b.push({code:"render-blocking-domains",severity:"warning",message:`${k.length} third-party domain(s) serve render-blocking resources (${v=k,v.slice(0,5).map(e=>e.domain).join(", ")+(v.length>5?` and ${v.length-5} more`:"")}); load them async or defer, or self-host them`}),w.slice(0,5).forEach(e=>{b.push({code:"heavy-domain",severity:"warning",message:`${e.domain} scripts take ${e.loafMs} ms of main-thread time in long animation frames (over 250 ms); defer, delay or remove them`,target:e.domain})}),!o&&y.length>0&&b.push({code:"render-blocking-unsupported",severity:"info",message:"renderBlockingStatus is not supported in this browser (Chrome 107+), so which third-party domains block rendering is unknown; run the snippet in Chrome or Edge to check"}),p&&b.push({code:"loaf-unattributed",severity:"info",message:`${f} long animation frame(s) took ${h} ms, but no third-party script was attributed in them (Long Animation Frames only list scripts over 5 ms), so 0 ms per domain does not rule out third-party cost`}),B&&b.push({code:"unknown-size",severity:"info",message:`${d} third-party request(s) without Timing-Allow-Origin report zero sizes, so the transfer size is a lower bound`}),{script:"Third-Party-Impact-by-Domain",status:"ok",count:y.length,corsLimitedAnalysis:B,details:{thirdPartyRequests:l,thirdPartyBytes:c,thirdPartyLoafMs:m,longFrames:f,unattributedLoafMs:g,firstPartyRequests:a,blockingDomains:o?k.length:null,renderBlockingSupported:o,loafSupported:u,sizeUnknownCount:d},items:y.slice(0,50),issues:b}})();