54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
/* VitFit Service Worker — shell-only caching for PWA install */
|
|
'use strict';
|
|
|
|
const CACHE_NAME = 'vitfit-v13';
|
|
const SHELL_ASSETS = [
|
|
'/',
|
|
'/index.html',
|
|
'/dashboard.html',
|
|
'/styles/base.css',
|
|
'/styles/layout.css',
|
|
'/styles/components.css',
|
|
'/styles/display.css',
|
|
'/js/core.js',
|
|
'/js/socket-client.js',
|
|
'/js/display.js',
|
|
'/manifest.json',
|
|
];
|
|
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil(
|
|
caches.open(CACHE_NAME).then(cache => cache.addAll(SHELL_ASSETS))
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (e) => {
|
|
e.waitUntil(
|
|
caches.keys().then(keys =>
|
|
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
|
|
)
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (e) => {
|
|
const url = new URL(e.request.url);
|
|
|
|
// API and socket: always network
|
|
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/socket.io')) return;
|
|
|
|
// Navigation: network-first, fallback to cached index
|
|
if (e.request.mode === 'navigate') {
|
|
e.respondWith(
|
|
fetch(e.request).catch(() => caches.match('/index.html'))
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Static assets: cache-first
|
|
e.respondWith(
|
|
caches.match(e.request).then(cached => cached || fetch(e.request))
|
|
);
|
|
});
|