// snippets/Loading/Priority-Hints-Audit.js | sha256:e88ca8e26ddc425e | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Priority-Hints-Audit.js (()=>{function e(e){const t=e.getBoundingClientRect();return t.top0&&t.left0&&t.width>0&&t.height>0}function t(e){const t=e.src||e.href||e.getAttribute("href")||"";return t.split("/").pop()?.split("?")[0]||t.slice(-50)||"(unknown)"}const r=new Map;function i(e){const t=e.src||e.href||e.getAttribute("href")||"";if(!t)return null;try{return r.get(new URL(t,location.origin).href)||null}catch{return null}}performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets")).forEach(e=>{try{r.set(new URL(e.name,location.origin).href,e)}catch{}});const o=Array.from(document.querySelectorAll("[fetchpriority]")),n=o.filter(e=>"IMG"!==e.tagName),c=(o.filter(e=>"IMG"===e.tagName),Array.from(document.querySelectorAll('link[rel="preload"][fetchpriority]'))),s=function(){const t=Array.from(document.querySelectorAll("img")).filter(e);let r=null,i=0;return t.forEach(e=>{const{width:t,height:o}=e.getBoundingClientRect(),n=t*o;n>i&&(i=n,r=e)}),r}(),l=[];if(c.filter(e=>"low"===e.getAttribute("fetchpriority")).forEach(e=>{l.push({code:"preload-low-priority",severity:"error",element:e,url:e.href,resource:t(e),message:'preload + fetchpriority="low" is contradictory',detail:'preload signals this resource is critical for the current page; fetchpriority="low" contradicts that.',fix:'Remove fetchpriority="low", or use fetchpriority="high" if the resource is truly critical'})}),o.filter(e=>"low"===e.getAttribute("fetchpriority")).forEach(e=>{const r=i(e);r&&r.startTime<500&&l.push({code:"low-priority-loaded-early",severity:"warning",element:e,url:e.src||e.href,resource:t(e),message:`fetchpriority="low" but loaded at ${r.startTime.toFixed(0)}ms`,detail:"This resource loaded very early. If it's genuinely non-critical, the browser is deprioritizing it correctly. Otherwise, reconsider the priority.",fix:'Verify the resource is non-critical, or remove fetchpriority="low"'})}),o.filter(e=>"high"===e.getAttribute("fetchpriority")).forEach(e=>{const r=i(e);r&&r.startTime>3e3&&l.push({code:"high-priority-loaded-late",severity:"warning",element:e,url:e.src||e.href,resource:t(e),message:`fetchpriority="high" but loaded at ${r.startTime.toFixed(0)}ms`,detail:"High-priority resources should load early. A late start may indicate render-blocking dependencies or incorrect placement in the document.",fix:"Move the element earlier in the HTML or investigate render-blocking dependencies"})}),s&&"high"!==s.getAttribute("fetchpriority")){const e=s.getAttribute("fetchpriority");l.push({code:"lcp-no-fetchpriority",severity:"warning",element:s,url:s.currentSrc||s.src,resource:(s.currentSrc||s.src).split("/").pop()?.split("?")[0]||"LCP image",message:"LCP candidate image "+(e?`has fetchpriority="${e}"`:"has no fetchpriority set"),detail:'The largest visible image is likely the LCP element and benefits most from fetchpriority="high".',fix:'Add fetchpriority="high" to the LCP image to improve Largest Contentful Paint'})}const h=o.filter(e=>"high"===e.getAttribute("fetchpriority")).length,a=o.filter(e=>"low"===e.getAttribute("fetchpriority")).length,u=o.filter(e=>"auto"===e.getAttribute("fetchpriority")).length;if(0===o.length&&!s)return{script:"Priority-Hints-Audit",status:"ok",count:0,items:[],issues:[]};s&&s.getAttribute("fetchpriority"),n.length>0&&n.forEach((e,t)=>{});const f=l.filter(e=>"error"===e.severity),g=l.filter(e=>"warning"===e.severity);return l.length>0&&[...f,...g].forEach(e=>{}),{script:"Priority-Hints-Audit",status:"ok",count:o.length,details:{highCount:h,lowCount:a,autoCount:u,preloadsWithPriorityCount:c.length,lcpCandidateHasFetchpriority:s?"high"===s.getAttribute("fetchpriority"):null},items:o.map(e=>({tag:e.tagName.toLowerCase(),fetchpriority:e.getAttribute("fetchpriority"),resource:(e.src||e.href||e.getAttribute("href")||"").split("/").pop()?.split("?")[0]||""})),issues:l.map(e=>({code:e.code,severity:e.severity,message:e.fix?`${e.message.replace(/\.$/,"")}. Fix: ${e.fix}`:e.message,...e.url?{target:e.url}:{}}))}})();