78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
/**
|
|
* Tractatus Service Worker - Minimal Version
|
|
* ONLY for version checking and update notifications
|
|
* NO aggressive caching (rely on HTTP caching instead)
|
|
*/
|
|
|
|
const CACHE_VERSION = '0.1.3';
|
|
|
|
// Install immediately, don't cache anything
|
|
self.addEventListener('install', (event) => {
|
|
console.log('[Service Worker] Installing version:', CACHE_VERSION);
|
|
self.skipWaiting(); // Activate immediately
|
|
});
|
|
|
|
// Activate and clean up old caches
|
|
self.addEventListener('activate', (event) => {
|
|
console.log('[Service Worker] Activating version:', CACHE_VERSION);
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
// Delete ALL caches - we're not using SW caching anymore
|
|
return Promise.all(
|
|
cacheNames.map((cacheName) => {
|
|
console.log('[Service Worker] Deleting cache:', cacheName);
|
|
return caches.delete(cacheName);
|
|
})
|
|
);
|
|
}).then(() => {
|
|
return self.clients.claim(); // Take control immediately
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event - DON'T intercept, let browser handle normally
|
|
self.addEventListener('fetch', (event) => {
|
|
// Do nothing - let requests go directly to network
|
|
// This allows nginx caching and ?v= parameters to work properly
|
|
return;
|
|
});
|
|
|
|
// Version check function
|
|
async function checkVersion() {
|
|
try {
|
|
const response = await fetch('/version.json', { cache: 'no-store' });
|
|
const serverVersion = await response.json();
|
|
|
|
return {
|
|
currentVersion: CACHE_VERSION,
|
|
serverVersion: serverVersion.version,
|
|
updateAvailable: CACHE_VERSION !== serverVersion.version,
|
|
forceUpdate: serverVersion.forceUpdate,
|
|
changelog: serverVersion.changelog
|
|
};
|
|
} catch (error) {
|
|
console.error('[Service Worker] Version check failed:', error);
|
|
return {
|
|
currentVersion: CACHE_VERSION,
|
|
serverVersion: null,
|
|
updateAvailable: false,
|
|
error: true
|
|
};
|
|
}
|
|
}
|
|
|
|
// Message handler
|
|
self.addEventListener('message', (event) => {
|
|
if (event.data.type === 'CHECK_VERSION') {
|
|
checkVersion().then((versionInfo) => {
|
|
event.ports[0].postMessage({
|
|
type: 'VERSION_INFO',
|
|
...versionInfo
|
|
});
|
|
});
|
|
}
|
|
|
|
if (event.data.type === 'SKIP_WAITING') {
|
|
self.skipWaiting();
|
|
}
|
|
});
|