@skybolt/vite-plugin
Version:
Vite plugin for Skybolt - High-performance asset caching for multi-page applications
2 lines (1 loc) • 1.57 kB
JavaScript
/*! Skybolt Service Worker - @version 3.5.1 */const CACHE_NAME="skybolt-v1";self.addEventListener("install",()=>{console.log("[Skybolt SW] Installing"),self.skipWaiting()}),self.addEventListener("activate",e=>{console.log("[Skybolt SW] Activating"),e.waitUntil(Promise.all([self.clients.claim(),caches.keys().then(t=>Promise.all(t.filter(s=>s.startsWith("skybolt-")&&s!==CACHE_NAME).map(s=>(console.log("[Skybolt SW] Deleting old cache:",s),caches.delete(s)))))]))}),self.addEventListener("fetch",e=>{const t=new URL(e.request.url);t.origin!==self.location.origin||!(t.pathname.endsWith(".js")||t.pathname.endsWith(".css")||t.pathname.endsWith(".mjs"))||isDevMode()||e.respondWith(caches.open(CACHE_NAME).then(o=>o.match(e.request).then(l=>l?(console.log("[Skybolt SW] Cache hit:",t.pathname),l):(console.log("[Skybolt SW] Cache miss:",t.pathname),fetch(e.request).then(n=>(n.ok&&o.put(e.request,n.clone()),n))))))}),self.addEventListener("message",e=>{const{type:t}=e.data||{};switch(t){case"CLEAR_CACHE":caches.delete(CACHE_NAME).then(()=>{console.log("[Skybolt SW] Cache cleared via message"),e.ports[0]&&e.ports[0].postMessage({success:!0})});break;case"GET_CACHE_INFO":caches.open(CACHE_NAME).then(s=>s.keys().then(o=>{e.ports[0]&&e.ports[0].postMessage({name:CACHE_NAME,count:o.length,urls:o.map(l=>l.url)})}));break;default:console.warn("[Skybolt SW] Unknown message type:",t)}});function isDevMode(){if(self.location.search.includes("dev-mode"))return!0;const e=["5173","5174","3001"];return!!(self.location.hostname==="localhost"&&e.includes(self.location.port))}