diff --git a/templates/sw.js b/templates/sw.js index 97acbd8..3425ec2 100644 --- a/templates/sw.js +++ b/templates/sw.js @@ -38,9 +38,18 @@ self.addEventListener('fetch', (event) => { // Navigations: try the network first, fall back to the offline page. // The dashboard route falls back to the offline-capable task app instead // of the generic offline page, since it can still show/edit cached tasks. + // A 5xx response (e.g. a reverse proxy up-front returning 502/503 while + // the app server itself is down) is treated the same as a network + // failure - fetch() only rejects on true network errors, not bad status + // codes, so that has to be checked explicitly. if (request.mode === 'navigate') { event.respondWith( - fetch(request).catch(() => { + fetch(request).then((response) => { + if (response.status >= 500) { + throw new Error(`Server error: ${response.status}`); + } + return response; + }).catch(() => { const path = new URL(request.url).pathname; return caches.match(path === '/' ? OFFLINE_TASKS_URL : OFFLINE_URL); })