homelabby/web/public/sw.js
Mikkel Georgsen 95a50f4abd feat(03-05): PWA manifest, service worker, icons, and registration hook
- web/public/manifest.json with display=standalone, theme_color=#faff69, 192+512 icons
- web/public/sw.js with app-shell cache strategy (API calls network-only)
- web/public/icons/icon-192.png and icon-512.png generated via gen-icons.cjs
- web/scripts/gen-icons.cjs pure-Node.js PNG icon generator (black canvas, volt H monogram)
- web/src/hooks/usePWA.ts registers service worker on app load
- web/index.html: added theme-color meta tag
- web/src/App.tsx: calls usePWA() hook
2026-04-10 06:22:05 +00:00

56 lines
1.4 KiB
JavaScript

const CACHE_NAME = 'hwlab-shell-v1'
// App shell assets to cache on install
const SHELL_ASSETS = [
'/',
'/index.html',
]
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL_ASSETS))
)
self.skipWaiting()
})
self.addEventListener('activate', (event) => {
// Remove old caches
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k))
)
)
)
self.clients.claim()
})
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url)
// API requests: network only — never cache
if (url.pathname.startsWith('/api/')) {
return
}
// Navigation requests: serve from cache, fall back to network (app shell)
if (event.request.mode === 'navigate') {
event.respondWith(
caches.match('/').then((cached) => cached ?? fetch(event.request))
)
return
}
// Static assets: cache-first
event.respondWith(
caches.match(event.request).then(
(cached) => cached ?? fetch(event.request).then((response) => {
if (response.ok && event.request.method === 'GET') {
const clone = response.clone()
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone))
}
return response
})
)
)
})