fix(pwa): 🐛 don't serve errored resources in service worker proxy

This commit is contained in:
Julien Oculi 2024-07-17 12:21:22 +02:00
parent 5669489dc9
commit 087843c800

View file

@ -90,7 +90,9 @@ async function fetchHandler(event: FetchEvent) {
// Cache first for "pre-cached-urls" // Cache first for "pre-cached-urls"
if (preCachedUrls.includes(url.pathname) && method === 'GET') { if (preCachedUrls.includes(url.pathname) && method === 'GET') {
const cached = await cache.match(event.request) ?? const cached = await cache.match(event.request) ??
await fetch(event.request).catch(() => null) ?? await fetch(event.request).then((response) =>
response.ok ? response : null
).catch(() => null) ??
await cache.match(event.request, { ignoreSearch: true }) await cache.match(event.request, { ignoreSearch: true })
if (cached === undefined) { if (cached === undefined) {
@ -105,7 +107,12 @@ async function fetchHandler(event: FetchEvent) {
const response = fetch(event.request) const response = fetch(event.request)
.then((response) => { .then((response) => {
// Update cache if (!response.ok) {
throw new Error(
`failed to get request: [${response.status}] ${response.statusText}`,
)
}
// Update cache if ok
cache.put(event.request, response.clone()) cache.put(event.request, response.clone())
return response return response
}) })