fix(pwa): harden service worker fetch handler to prevent ERR_FAILED
Build and Push Docker Image / build-and-push (push) Failing after 16m52s

This commit is contained in:
2026-08-21 20:34:42 +09:00
parent 04fef885a8
commit 09f8fdc55e
+31 -8
View File
@@ -1,9 +1,9 @@
const CACHE_NAME = "checkflow-v1";
const STATIC_ASSETS = ["/", "/login", "/register", "/manifest.json", "/icons/icon.svg"];
const CACHE_NAME = "checkflow-v2";
const STATIC_ASSETS = ["/manifest.json", "/icons/icon.svg"];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS)).catch(() => {})
);
self.skipWaiting();
});
@@ -21,14 +21,37 @@ self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);
// Network-first for API
if (url.pathname.startsWith("/api")) {
event.respondWith(fetch(request).catch(() => new Response(JSON.stringify({ error: "Offline" }), { headers: { "Content-Type": "application/json" } })));
// Only handle http/https GET requests to the same origin
if (request.method !== "GET" || !url.protocol.startsWith("http") || url.origin !== self.location.origin) {
return;
}
// Cache-first for static
// Network-first for API & navigation routes to avoid stale/failed page loads
if (url.pathname.startsWith("/api") || request.mode === "navigate") {
event.respondWith(
caches.match(request).then((cached) => cached || fetch(request))
fetch(request).catch(() => {
if (url.pathname.startsWith("/api")) {
return new Response(JSON.stringify({ error: "Offline or service unavailable" }), {
headers: { "Content-Type": "application/json" },
});
}
return caches.match(request).then((cached) => cached || Response.error());
})
);
return;
}
// Cache-first for static assets with safe network fallback
event.respondWith(
caches.match(request).then((cached) => {
if (cached) return cached;
return fetch(request).then((response) => {
if (response && response.status === 200 && response.type === "basic") {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)).catch(() => {});
}
return response;
}).catch(() => Response.error());
})
);
});