From 011ad98ee010895b50bd3cbbca5696eacc0f984d Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sat, 18 Oct 2025 05:03:08 +0000 Subject: [PATCH] Improve API fallback robustness and avoid throwing to enable demo fallback silently cgen-b98e0911f28f4d11add2635b34ab43b3 --- client/lib/supabase-service.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/client/lib/supabase-service.ts b/client/lib/supabase-service.ts index 022b30df..4d1ab139 100644 --- a/client/lib/supabase-service.ts +++ b/client/lib/supabase-service.ts @@ -263,14 +263,23 @@ export const communityService = { try { const resp = await fetch(`/api/posts?limit=${encodeURIComponent(String(limit))}`); if (resp.ok) { - const payload = await resp.json(); - return (Array.isArray(payload) ? payload : []) as CommunityPost[]; + const ct = resp.headers.get("content-type") || ""; + if (ct.includes("application/json") || ct.includes("json")) { + const payload = await resp.json(); + return (Array.isArray(payload) ? payload : []) as CommunityPost[]; + } else { + const text = await resp.text(); + console.warn("API fallback returned non-JSON content-type:", ct, text.slice(0, 120)); + } + } else { + console.warn("API fallback /api/posts not ok:", resp.status, resp.statusText); } } catch (apiErr) { console.error("API fallback for getPosts failed:", (apiErr as any)?.message || apiErr); } - throw new Error("Unable to load posts"); + // As a last resort, return an empty array so callers can apply their own demo fallback without surfacing an error toast + return [] as CommunityPost[]; }, async createPost(