// snippets/Loading/Speculation-Rules-Inspector.js | sha256:6bd00e36c4f0b97c | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Speculation-Rules-Inspector.js (()=>{const e={activated:!document.prerendering,activatedAt:null};document.prerendering&&document.addEventListener("prerenderingchange",()=>{e.activated=!0,e.activatedAt=Math.round(performance.now())},{once:!0});const r=()=>{const r=[],t=[],s={},n="function"==typeof window.HTMLScriptElement.supports&&window.HTMLScriptElement.supports("speculationrules"),[i]=performance.getEntriesByType("navigation"),a=Math.round(i?.activationStart??0),o=i?.deliveryType??"",c=[...document.querySelectorAll('script[type="speculationrules"]')];return c.forEach((e,n)=>{let i;try{i=JSON.parse(e.textContent)}catch(e){return void r.push({code:"invalid-json",severity:"error",message:`Speculation rules script #${n+1} is not valid JSON (${e.message}). The browser ignores the whole script.`})}for(const[e,a]of Object.entries(i??{}))if(Array.isArray(a)){s[e]=(s[e]??0)+a.length;for(const s of a){const i=s.source??(s.where?"document":"list"),a=Array.isArray(s.urls)?s.urls:[],o=s.eagerness??("document"===i?"conservative":"immediate");t.push({action:e,source:i,eagerness:o,urls:a.slice(0,20),urlCount:a.length,where:s.where??null,requires:s.requires??[],referrerPolicy:s.referrer_policy??null,scriptIndex:n});const c="prerender"===e||"prerender_until_script"===e;c&&"list"===i&&("immediate"===o||"eager"===o)&&a.length>10&&r.push({code:"eager-list-capped",severity:"warning",message:`A ${e} rule lists ${a.length} URLs with eagerness "${o}". Chrome caps eager prerenders, so the extra URLs are not prerendered. Fix: list fewer URLs or use "moderate".`}),!c||"document"!==i||"immediate"!==o&&"eager"!==o||r.push({code:"eager-document-rule",severity:"warning",message:`A document ${e} rule uses eagerness "${o}", which prerenders matching links without a user signal and can waste bandwidth and CPU. Fix: use "moderate" or "conservative".`})}}}),0===c.length?r.push({code:"no-rules",severity:"info",message:"No speculation rules found in the DOM. Rules delivered in the Speculation-Rules HTTP header are not readable from JavaScript."}):0!==t.length||r.some(e=>"error"===e.severity)||r.push({code:"no-prefetch-or-prerender",severity:"info",message:"Speculation rules scripts were found but contain no prefetch or prerender rules."}),!n&&c.length>0&&r.push({code:"unsupported",severity:"info",message:"This browser does not support speculation rules, so the rules are ignored."}),a>0&&r.push({code:"prerendered",severity:"info",message:`This page was prerendered and activated at ${a}ms. Navigation timings are relative to activationStart.`}),{script:"Speculation-Rules-Inspector",status:"ok",count:t.length,details:{supported:n,prerendering:!0===document.prerendering,activated:e.activated,activatedAt:e.activatedAt,activationStart:a,deliveryType:o,scriptCount:c.length,actions:s},items:t.slice(0,50),issues:r}};window.getSpeculationRulesInspection=r;const t=r();return t.issues.forEach(e=>{}),document.prerendering?{script:"Speculation-Rules-Inspector",status:"tracking",message:"This page is prerendering. Call getSpeculationRulesInspection() after activation to see the final timings.",getDataFn:"getSpeculationRulesInspection"}:t})();