fix: add error handling to sw-dev.js fetch requests

Add proper error handling to prevent uncaught promise rejections when
image fetches fail. If a fetch fails, try to return cached response,
or gracefully handle the error instead of letting it propagate as an
uncaught promise rejection.
This commit is contained in:
Gigi
2025-10-31 01:54:23 +01:00
parent 5a8b885d25
commit aab8176987

View File

@@ -28,8 +28,16 @@ self.addEventListener('fetch', (event) => {
cache.put(event.request, response.clone())
}
return response
}).catch((error) => {
// If fetch fails (network error, CORS, etc.), return cached response if available
// or let the error propagate so the browser can handle it
// Don't cache failed responses
return cachedResponse || Promise.reject(error)
})
})
}).catch(() => {
// If cache.open or match fails, try to fetch directly without caching
return fetch(event.request)
})
)
}