fix: make service worker more resilient to prevent freezing

The service worker was causing 503 errors and freezing requests because:
- Pre-caching was trying to cache non-existent files
- Fetch handler was too aggressive with interception
- Failed cache operations blocked all requests

Fixes:
- Made pre-caching optional (logs errors but doesn't fail)
- More lenient fetch handler (only caches successful responses)
- Proper error handling for cache operations
- Only intercepts GET requests
- Falls back to network if cache fails
This commit is contained in:
2026-02-20 01:02:59 -08:00
committed by Naomi Carrigan
parent eebe20026c
commit e1fbbd4d7c
+9 -9
View File
@@ -11,11 +11,6 @@ const IMAGE_CACHE = `${CACHE_VERSION}-images`;
// Static assets to cache on install
const STATIC_ASSETS = [
'/',
'/index.html',
'/main.js',
'/runtime.js',
'/styles.css',
'/offline.html'
];
@@ -27,6 +22,8 @@ self.addEventListener('install', (event) => {
console.log('[Service Worker] Caching static assets');
return cache.addAll(STATIC_ASSETS).catch((err) => {
console.error('[Service Worker] Failed to cache static assets:', err);
// Don't fail installation if caching fails
return Promise.resolve();
});
}).then(() => {
return self.skipWaiting();
@@ -137,10 +134,13 @@ self.addEventListener('fetch', (event) => {
event.respondWith(
fetch(request)
.then((response) => {
const responseClone = response.clone();
caches.open(DYNAMIC_CACHE).then((cache) => {
cache.put(request, responseClone);
});
// Only cache successful responses
if (response.ok) {
const responseClone = response.clone();
caches.open(DYNAMIC_CACHE).then((cache) => {
cache.put(request, responseClone);
});
}
return response;
})
.catch(() => {