fix(pwa): harden service worker fetch handler to prevent ERR_FAILED
Build and Push Docker Image / build-and-push (push) Failing after 16m52s
Build and Push Docker Image / build-and-push (push) Failing after 16m52s
This commit is contained in:
+31
-8
@@ -1,9 +1,9 @@
|
|||||||
const CACHE_NAME = "checkflow-v1";
|
const CACHE_NAME = "checkflow-v2";
|
||||||
const STATIC_ASSETS = ["/", "/login", "/register", "/manifest.json", "/icons/icon.svg"];
|
const STATIC_ASSETS = ["/manifest.json", "/icons/icon.svg"];
|
||||||
|
|
||||||
self.addEventListener("install", (event) => {
|
self.addEventListener("install", (event) => {
|
||||||
event.waitUntil(
|
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();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
@@ -21,14 +21,37 @@ self.addEventListener("fetch", (event) => {
|
|||||||
const { request } = event;
|
const { request } = event;
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
|
||||||
// Network-first for API
|
// Only handle http/https GET requests to the same origin
|
||||||
if (url.pathname.startsWith("/api")) {
|
if (request.method !== "GET" || !url.protocol.startsWith("http") || url.origin !== self.location.origin) {
|
||||||
event.respondWith(fetch(request).catch(() => new Response(JSON.stringify({ error: "Offline" }), { headers: { "Content-Type": "application/json" } })));
|
|
||||||
return;
|
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(
|
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());
|
||||||
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user