// snippets/Loading/Resource-Hints.js | sha256:07b5029ae84b9011 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Resource-Hints.js (()=>{function e(e){return e.name.endsWith("#webperf-snippets")}function r(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const r=["B","KB","MB","GB"],o=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),r.length-1));return(e/Math.pow(1024,o)).toFixed(1)+" "+r[o]}const o=[{rel:"preload",icon:"⚡",description:"Critical resource for current page",requiresAs:!0},{rel:"modulepreload",icon:"📦",description:"ES module for current page",requiresAs:!1},{rel:"preconnect",icon:"🔌",description:"Early connection to origin",requiresAs:!1},{rel:"dns-prefetch",icon:"🔍",description:"DNS lookup for origin",requiresAs:!1},{rel:"prefetch",icon:"📥",description:"Resource for future navigation",requiresAs:!1},{rel:"prerender",icon:"🖼️",description:"Pre-render page",requiresAs:!1}],t=[],i=[];o.forEach(r=>{document.querySelectorAll(`link[rel="${r.rel}"]`).forEach(o=>{const n=o.href||o.getAttribute("href")||"";let s="";try{s=new URL(n,location.origin).hostname}catch{}const c={rel:r.rel,icon:r.icon,href:n,shortHref:n.length>50?"..."+n.slice(-47):n,hostname:s,as:o.getAttribute("as")||"",crossorigin:o.crossOrigin||o.getAttribute("crossorigin")||"",fetchpriority:o.fetchPriority||o.getAttribute("fetchpriority")||"",type:o.type||"",media:o.media||"",element:o};t.push(c),r.requiresAs&&!c.as&&i.push({hint:c,code:"preload-missing-as",issue:"Missing 'as' attribute (required for preload)",severity:"error",fix:'Add as="script|style|font|image|fetch"'}),"font"!==c.as||c.crossorigin||i.push({hint:c,code:"font-preload-no-crossorigin",issue:"Font preload without crossorigin (causes double fetch)",severity:"error",fix:'Add crossorigin="anonymous"'}),"preconnect"!==r.rel||c.crossorigin||performance.getEntriesByType("resource").filter(r=>!e(r)).filter(e=>e.name.includes(s)&&("script"===e.initiatorType||"css"===e.initiatorType||"font"===e.initiatorType)).length>0&&i.push({hint:c,code:"preconnect-no-crossorigin",issue:"Preconnect may need crossorigin for CORS resources",severity:"warning",fix:'Consider adding crossorigin="anonymous"'})})});const n=t.filter(e=>"preload"===e.rel),s=performance.getEntriesByType("resource").filter(r=>!e(r));n.forEach(e=>{s.some(r=>r.name===e.href)||i.push({hint:e,code:"unused-preload",issue:"Preloaded resource not used within page load",severity:"warning",fix:"Remove unused preload or verify it's needed"})});const c=t.filter(e=>"preconnect"===e.rel||"dns-prefetch"===e.rel);c.forEach(e=>{!s.some(r=>{try{return new URL(r.name).hostname===e.hostname}catch{return!1}})&&e.hostname&&i.push({hint:e,code:"unused-preconnect",issue:`No resources loaded from ${e.hostname}`,severity:"warning",fix:"Remove unused preconnect or verify it's needed for future resources"})}),n.length>6&&i.push({hint:{rel:"preload",href:`${n.length} preloads`},code:"too-many-preloads",issue:`Too many preloads (${n.length}) - they compete for bandwidth`,severity:"warning",fix:"Limit to 5-6 most critical resources"});const a=new Map;let l=0;s.forEach(e=>{try{const r=new URL(e.name);if(r.hostname!==location.hostname){a.has(r.origin)||a.set(r.origin,{count:0,size:0,types:new Set});const o=a.get(r.origin);o.count++,o.size+=e.transferSize||0,0===e.transferSize&&0===e.encodedBodySize&&0===e.decodedBodySize&&e.duration>0&&l++,o.types.add(e.initiatorType)}}catch{}});const h=new Set([...c].map(e=>{try{return new URL(e.href).origin}catch{return""}})),u=[];a.forEach((e,r)=>{!h.has(r)&&e.count>=2&&u.push({origin:r,...e})}),o.map(e=>{const r=t.filter(r=>r.rel===e.rel).length;return{type:e.rel,count:r,icon:e.icon}}).forEach(e=>{}),o.forEach(e=>{const r=t.filter(r=>r.rel===e.rel);0!==r.length&&(r.map(r=>{const o={Resource:r.shortHref};if("preload"===e.rel||"modulepreload"===e.rel){o.As=r.as||"⚠️ missing",o.Crossorigin=r.crossorigin||"-",r.fetchpriority&&(o.Priority=r.fetchpriority);const e=s.some(e=>e.name===r.href);o.Used=e?"✅":"⚠️ No"}if("preconnect"===e.rel||"dns-prefetch"===e.rel){o.Origin=r.hostname,o.Crossorigin=r.crossorigin||"-";const e=s.filter(e=>{try{return new URL(e.name).hostname===r.hostname}catch{return!1}});o["Resources Loaded"]=e.length}return o}),r.forEach((e,r)=>{}))});const p=i.filter(e=>"error"===e.severity),d=i.filter(e=>"warning"===e.severity);i.length>0&&(p.length>0&&p.forEach(e=>{}),d.length>0&&d.forEach(e=>{})),u.length>0&&(u.sort((e,r)=>r.count-e.count).slice(0,5).map(e=>({Origin:e.origin,Resources:e.count,Size:r(e.size),Types:Array.from(e.types).join(", ")})),u.slice(0,3).forEach(e=>{}));const f=e=>i.some(r=>r.hint===e&&"error"===r.severity)?0:i.some(r=>r.hint===e)?1:2,g=t.map((e,r)=>({h:e,index:r})).sort((e,r)=>f(e.h)-f(r.h)||e.index-r.index).map(({h:e})=>e);return{script:"Resource-Hints",status:"ok",count:t.length,corsLimitedAnalysis:l>0,details:{sizeUnknownCount:l,byType:Object.fromEntries(o.map(e=>[e.rel,t.filter(r=>r.rel===e.rel).length])),missingPreconnectsCount:u.length,missingPreconnects:u.slice(0,20).map(e=>({origin:e.origin,requestCount:e.count,sizeBytes:e.size,resourceTypes:Array.from(e.types),recommendedHint:"preconnect"}))},items:g.slice(0,50).map(e=>({rel:e.rel,href:e.shortHref,as:e.as,crossorigin:e.crossorigin,fetchpriority:e.fetchpriority})),issues:i.map(e=>({code:e.code,severity:e.severity,message:`${e.hint.rel}: ${e.issue}. Fix: ${e.fix}`,...e.hint.element?{target:e.hint.href}:{}}))}})();