// snippets/Loading/Resource-Hints-Validation.js | sha256:dbffcd164a73b907 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Resource-Hints-Validation.js (()=>{document;const e=Array.from(document.querySelectorAll('link[rel="preload"]')),t=Array.from(document.querySelectorAll('link[rel="preconnect"]')),n=Array.from(document.querySelectorAll('link[rel="dns-prefetch"]')),r=performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets")),o=e=>{try{const t=e.startsWith("http")?e:new URL(e,location.href).href;return new URL(t).origin}catch{return null}},a=e=>{try{const t=new URL(e,location.origin);return t.origin+t.pathname+t.search}catch{return e}},s=new Set(r.map(e=>a(e.name))),i=new Set(r.map(e=>o(e.name)).filter(Boolean));i.add(location.origin);const c=document.querySelectorAll("img[src]");if(0===e.length);else{const t=e.map(e=>{const t=e.href,n=e.getAttribute("as")||"unknown",o=e.getAttribute("media"),i=a(t),c=t.split("/").pop()?.split("?").shift()||t,l=s.has(i),d=r.find(e=>a(e.name)===i);let h=!0;if(o)try{h=window.matchMedia(o).matches}catch{h=!1}return{shortUrl:c,href:t,as:n,media:o,mediaMatches:h,wasUsed:l,size:d?(m=d.decodedBodySize||d.transferSize,m?(m/1024).toFixed(1)+" KB":"N/A"):"N/A",link:e};var m}),n=t.filter(e=>e.wasUsed),o=(t.filter(e=>!e.wasUsed&&(!e.media||e.mediaMatches)),t.filter(e=>!e.wasUsed&&e.media&&!e.mediaMatches)),i=e.length-o.length;i>0&&Math.round(n.length/i*100)}const l=new Set;if(c.forEach(e=>{try{const t=o(e.src);t&&t!==location.origin&&!i.has(t)&&l.add(t)}catch{}}),0===t.length);else{const e=t.map(e=>{const t=e.href,n=o(t),a=!!n&&i.has(n),s=!!n&&l.has(n),c=r.filter(e=>o(e.name)===n).length;return{domain:n,href:t,wasUsed:a,isLazyLoaded:s,requestCount:c,link:e}}),n=e.filter(e=>e.wasUsed),a=e.filter(e=>!e.wasUsed&&e.isLazyLoaded);e.filter(e=>!e.wasUsed&&!e.isLazyLoaded),Math.round((n.length+a.length)/t.length*100)}if(0===n.length);else{const e=n.map(e=>{const t=e.href,n=o(t),a=!!n&&i.has(n),s=!!n&&l.has(n),c=r.filter(e=>o(e.name)===n).length;return{domain:n,href:t,wasUsed:a,isLazyLoaded:s,requestCount:c,link:e}}),t=e.filter(e=>e.wasUsed),a=e.filter(e=>!e.wasUsed&&e.isLazyLoaded);e.filter(e=>!e.wasUsed&&!e.isLazyLoaded),Math.round((t.length+a.length)/n.length*100)}const d=new Set(t.map(e=>o(e.href)).filter(Boolean)),h=new Set(n.map(e=>o(e.href)).filter(Boolean)),m=Array.from(d).filter(e=>h.has(e));m.length>0&&m.forEach(e=>{});const f=new Set;c.forEach(e=>{try{const t=o(e.src);t&&t!==location.origin&&f.add(t)}catch{}});const u=Array.from(f).filter(e=>!i.has(e));u.length>0&&u.map(e=>{const t=Array.from(c).filter(t=>{try{return o(t.src)===e}catch{return!1}}).length;return{domain:e,imgCount:t}}).filter(e=>e.domain&&"null"!==e.domain).sort((e,t)=>t.imgCount-e.imgCount).forEach(e=>{});const g=new Set([...t.map(e=>o(e.href)),...n.map(e=>o(e.href))]),p={};r.forEach(e=>{const t=o(e.name);t&&t!==location.origin&&(p[t]=(p[t]||0)+1)});const w=Object.entries(p).filter(([e,t])=>t>=2&&!g.has(e)).sort((e,t)=>t[1]-e[1]),y=[...w.filter(([,e])=>e>=5).slice(0,3),...w.filter(([,e])=>e>=2&&e<5).slice(0,5)];w.length>0&&y.forEach(([e,t])=>{});const L=new Set;c.forEach(e=>{try{const t=o(e.src);t&&t!==location.origin&&!i.has(t)&&L.add(t)}catch{}});const U=e=>{const t=o(e.href);return t&&!i.has(t)&&!L.has(t)},A=[...e.filter(e=>!s.has(a(e.href))&&!(e=>{const t=e.getAttribute("media");if(!t)return!1;try{return!window.matchMedia(t).matches}catch{return!0}})(e)).map(e=>({domain:o(e.href),href:e.href,requestCount:0,action:"remove-unused-preload",recommendedHint:null})),...t.filter(U).map(e=>({domain:o(e.href),href:e.href,requestCount:0,action:"remove-unused-preconnect",recommendedHint:null})),...n.filter(U).map(e=>({domain:o(e.href),href:e.href,requestCount:0,action:"remove-unused-dns-prefetch",recommendedHint:null}))],S=A.length;return{script:"Resource-Hints-Validation",status:"ok",count:e.length+t.length+n.length,details:{preloadCount:e.length,preconnectCount:t.length,dnsPrefetchCount:n.length,totalResourcesLoaded:r.length,unusedHints:S,missingPreconnects:w.length,redundantHints:m.length},items:[...A,...w.map(([e,t])=>({domain:e,href:null,requestCount:t,action:t>=5?"add-preconnect":"add-dns-prefetch",recommendedHint:t>=5?"preconnect":"dns-prefetch"})),...m.map(e=>({domain:e,href:null,requestCount:p[e]||0,action:"remove-dns-prefetch",recommendedHint:"preconnect"}))].slice(0,50),issues:[...S>0?[{code:"unused-hints",severity:"warning",message:`${S} unused hint(s) waste bandwidth`}]:[],...w.length>0?[{code:"missing-connection-hints",severity:"info",message:`${w.length} domain(s) could benefit from connection hints`}]:[],...m.length>0?[{code:"redundant-hints",severity:"warning",message:`${m.length} domain(s) have both preconnect and dns-prefetch (redundant)`}]:[]]}})();