// snippets/Loading/Compression-Audit.js | sha256:3ff52ec890aa8366 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Compression-Audit.js (()=>{const e={js:.7,css:.7,html:.7,json:.7,svg:.7,xml:.7,font:.5},t={js:"js",mjs:"js",cjs:"js",css:"css",html:"html",htm:"html",json:"json",webmanifest:"json",svg:"svg",xml:"xml",rss:"xml",atom:"xml",ttf:"font",otf:"font",eot:"font"},n=[...performance.getEntriesByType("navigation"),...performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets"))],s=n.some(e=>"contentEncoding"in e),o=[];n.forEach(e=>{const n=(e=>{try{return new URL(e)}catch{return null}})(e.name);if(!n||!/^https?:$/.test(n.protocol))return;const s=((e,n)=>{if("navigation"===e.entryType)return"html";const s=n.pathname.split(".").pop().toLowerCase();return t[s]?t[s]:"script"===e.initiatorType?"js":null})(e,n);s&&o.push({entry:e,url:n,type:s})});const i=[],r=[];let c=0,a=0;const d={};o.forEach(({entry:t,url:n,type:s})=>{const o=t.decodedBodySize||0,l=t.encodedBodySize||0;if(0===o&&n.origin!==location.origin)return void c++;if(o<1024)return void a++;const u=(t.contentEncoding||"").toLowerCase();if(u?"identity"!==u:lt.estimatedSavingsBytes-e.estimatedSavingsBytes);const l=r.reduce((e,t)=>e+t.decodedBytes,0),u=r.reduce((e,t)=>e+t.estimatedSavingsBytes,0),m=c>0,p=[];return r.length>0&&p.push({code:"uncompressed",severity:"warning",message:`${r.length} text resource(s) served without compression; enabling gzip or brotli could save about ${function(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const t=["B","KB","MB","GB"],n=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),t.length-1));return(e/Math.pow(1024,n)).toFixed(1)+" "+t[n]}(u)}`}),m&&p.push({code:"unknown-size",severity:"info",message:`${c} cross-origin text resource(s) without Timing-Allow-Origin report zero sizes, so their compression is unknown`}),!s&&o.length>0&&p.push({code:"content-encoding-unavailable",severity:"info",message:"PerformanceResourceTiming.contentEncoding is not available; compression is inferred from encodedBodySize versus decodedBodySize"}),{script:"Compression-Audit",status:"ok",count:r.length,corsLimitedAnalysis:m,details:{totalTextResources:o.length,compressedCount:i.length,uncompressedCount:r.length,sizeUnknownCount:c,skippedSmallCount:a,totalUncompressedBytes:l,estimatedSavingsBytes:u,byEncoding:d,contentEncodingSupported:s},items:r.slice(0,50),issues:p}})();