// snippets/Loading/Service-Worker-Analysis.js | sha256:1e4ed59b35983d19 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Service-Worker-Analysis.js (async()=>{if(!("serviceWorker"in navigator))return{script:"Service-Worker-Analysis",status:"unsupported",error:"Service Workers not supported in this browser",count:0,details:{},items:[],issues:[{code:"unsupported",severity:"error",message:"Service Workers not supported in this browser"}]};const e=await navigator.serviceWorker.getRegistrations(),t=navigator.serviceWorker.controller,r=performance.getEntriesByType("navigation")[0],n=performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets")),o=n.filter(e=>e.workerStart>0),s=o.filter(e=>0===e.transferSize&&e.encodedBodySize>0),a=o.filter(e=>e.transferSize>0),i=o.filter(e=>0===e.transferSize&&0===e.encodedBodySize),l=s.length+a.length,c=n.filter(e=>0===e.workerStart);let g=null;r&&r.workerStart>0&&(g=Math.round(10*Math.max(r.fetchStart-r.startTime-(r.workerStart-r.startTime),0))/10);const d=l>0?parseFloat((s.length/l*100).toFixed(1)):null,h=s.reduce((e,t)=>e+(t.encodedBodySize||0),0),u=new Map;for(const t of e)if(t.navigationPreload)try{const e=await t.navigationPreload.getState();u.set(t,e)}catch{}const p=[];if("caches"in window)try{const e=await caches.keys();for(const t of e){const e=await caches.open(t),r=await e.keys();p.push({name:t,entries:r.length})}}catch{}const v=[...p].sort((e,t)=>t.entries-e.entries).slice(0,20),w=e.map(e=>{const t=u.get(e)||null;let r="none",n=null;return e.active?(r="active",n=e.active.scriptURL):e.waiting?(r="waiting",n=e.waiting.scriptURL):e.installing&&(r="installing",n=e.installing.scriptURL),{scope:e.scope,scriptURL:n,state:r,hasWaiting:!!e.waiting,hasInstalling:!!e.installing,navigationPreloadEnabled:t?t.enabled:null,navigationPreloadHeaderValue:t?.headerValue||null}}),f=[];0===e.length&&f.push({code:"no-service-worker",severity:"info",message:"No Service Workers registered"}),e.length>0&&!t&&f.push({code:"not-controlled",severity:"warning",message:"Page is not controlled by a Service Worker (hard reload or first visit). Do a normal reload."});for(const e of w)e.hasWaiting&&f.push({code:"update-pending",severity:"warning",message:`Service Worker update pending for ${e.scope}. Call skipWaiting() to activate the new version.`,target:e.scope}),!1===e.navigationPreloadEnabled&&f.push({code:"navigation-preload-disabled",severity:"info",message:`Navigation Preload is disabled for ${e.scope}. Consider enabling it with registration.navigationPreload.enable().`,target:e.scope});null!==g&&g>100?f.push({code:"slow-startup",severity:"error",message:`High SW startup time (${g}ms). Enable Navigation Preload to reduce overhead.`}):null!==g&&g>50&&f.push({code:"slow-startup",severity:"warning",message:`Moderate SW startup time (${g}ms)`}),null!==d&&d<50?f.push({code:"low-cache-hit-rate",severity:"error",message:`Low SW cache hit rate (${d}%). Review the caching strategy.`}):null!==d&&d<80&&f.push({code:"low-cache-hit-rate",severity:"warning",message:`Moderate SW cache hit rate (${d}%). Review the caching strategy for more resources.`}),i.length>0&&f.push({code:"unknown-source",severity:"info",message:`${i.length} intercepted resource(s) are cross-origin without Timing-Allow-Origin, so their source is unknown. The hit rate excludes them.`});const m=null===d?null:d>=80?"good":d>=50?"needs-improvement":"poor";if(0===e.length)return{script:"Service-Worker-Analysis",status:"ok",count:0,details:{controlled:!1,controllerState:null,swOverheadMs:null,totalResources:n.length,swIntercepted:0,notIntercepted:n.length,fromCache:0,fromNetwork:0,fromUnknown:0,cacheHitRate:null,savedBytes:0,cacheStorageCount:p.length,cacheStorage:v},items:[],issues:f};for(const t of e)u.get(t);if(p.length>0){p.reduce((e,t)=>e+t.entries,0);for(const{}of p);}return{script:"Service-Worker-Analysis",status:"ok",count:e.length,...null!==m&&{rating:m},details:{controlled:!!t,controllerState:t?.state||null,swOverheadMs:g,totalResources:n.length,swIntercepted:o.length,notIntercepted:c.length,fromCache:s.length,fromNetwork:a.length,fromUnknown:i.length,cacheHitRate:d,savedBytes:h,cacheStorageCount:p.length,cacheStorage:v},items:w,issues:f}})();