// snippets/Loading/CSS-Media-Queries-Analysis.js | sha256:73fee01419785662 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/CSS-Media-Queries-Analysis.js (()=>{async function e(e=768){function s(e){try{const s=new URL(e,location.href);return/^https?:$/.test(s.protocol)?(s.hash="webperf-snippets",s.href):e}catch{return e}}const t=[...document.styleSheets],r=[];let n=0;function o(e){const s=e.match(/\.-?[_a-zA-Z][\w-]*/g)||[],t=e.match(/[a-z-]+\s*:/g)||[];return{classes:s.length,properties:t.length}}function i(s,t,n,i,a){var c;(function(s){if(!s)return!1;const t=s.match(/min-width:\s*(\d+)(px|em|rem)/i);if(t){const s=parseInt(t[1]),r=t[2].toLowerCase();if("px"===r&&s>e)return!0;if("em"===r&&s>e/16)return!0;if("rem"===r&&s>e/16)return!0}return!1})(n)&&r.push({source:s,inline:t,mediaQuery:n,...o(a),bytes:(c=i,new Blob([c]).size)})}function a(e,s){const t=/@media\s*([^{]+)\{((?:[^{}]|\{[^{}]*\})*)\}/g;let r;for(;null!==(r=t.exec(e));)i(s,!1,r[1].trim(),r[0],r[2])}for(let e=0;e tag #${e}`;try{const e=r.cssRules||r.rules;e&&[...e].forEach(e=>{if(e.type===CSSRule.MEDIA_RULE){const s=[...e.cssRules].map(e=>e.cssText).join("");i(c,o,e.media.mediaText,e.cssText,s)}})}catch{if(r.href)try{const e=await fetch(s(r.href));a(await e.text(),r.href)}catch{n++}}}const c=new Map;for(const e of r){const s=c.get(e.mediaQuery)??{mediaQuery:e.mediaQuery,rules:0,sources:new Set,classes:0,properties:0,bytes:0};s.rules++,s.sources.add(e.source),s.classes+=e.classes,s.properties+=e.properties,s.bytes+=e.bytes,c.set(e.mediaQuery,s)}const u=[...c.values()].map(({sources:e,...s})=>({mediaQuery:s.mediaQuery,rules:s.rules,files:e.size,classes:s.classes,properties:s.properties,bytes:s.bytes})).sort((e,s)=>s.bytes-e.bytes),l=new Map;for(const e of r){const s=l.get(e.source)??{source:e.source,inline:e.inline,rules:0,bytes:0};s.rules++,s.bytes+=e.bytes,l.set(e.source,s)}const h=[...l.values()],d=r.reduce((e,s)=>e+s.bytes,0),p=r.length?Math.round(u.length/r.length*1e3)/1e3:0,f=[];return 0===r.length?f.push({code:"no-wide-rules",severity:"info",message:`No @media rules for viewports wider than ${e}px`}):(f.push({code:"wide-rules",severity:d>51200?"warning":"info",message:`${r.length} @media rules for viewports wider than ${e}px add ${function(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const s=["B","KB","MB","GB"],t=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),s.length-1));return(e/Math.pow(1024,t)).toFixed(1)+" "+s[t]}(d)} of CSS that narrower screens download and parse without using. Move them to a stylesheet with a media attribute, such as , which narrower screens fetch at low priority without blocking rendering`}),r.length>=20&&p<.1&&f.push({code:"repeated-queries",severity:"info",message:`${u.length} distinct queries repeat across ${r.length} @media blocks, the pattern of component-scoped or compiled CSS. Grouping the rules of each query under one block shortens the stylesheets`})),n>0&&f.push({code:"unreadable-stylesheets",severity:"info",message:`${n} cross-origin stylesheet(s) could not be read, so their @media rules are not counted`}),f.forEach(e=>{}),{script:"CSS-Media-Queries-Analysis",status:"ok",count:u.length,details:{minWidth:e,rules:r.length,totalUnique:u.length,uniquenessRatio:p,classes:r.reduce((e,s)=>e+s.classes,0),properties:r.reduce((e,s)=>e+s.properties,0),bytes:d,bySource:h.slice(0,50),corsBlockedCount:n},items:u.slice(0,50),issues:f}}return window.analyzeCSSMediaQueries=e,e()})();