From 4a9e9e523f1906f483c02d5ac2fb016a80426641 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Tue, 14 Oct 2025 04:44:37 +0000 Subject: [PATCH] Add Supabase fallback to Blog list fetch cgen-5ef4288ffd8343b29c5e44bcec1903fa --- client/pages/Blog.tsx | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/client/pages/Blog.tsx b/client/pages/Blog.tsx index 7597b9ea..99adfaa4 100644 --- a/client/pages/Blog.tsx +++ b/client/pages/Blog.tsx @@ -174,8 +174,31 @@ export default function Blog() { let cancelled = false; (async () => { try { - const res = await fetch("/api/blog?limit=50"); - const data = res.ok ? await res.json() : []; + let res = await fetch("/api/blog?limit=50"); + let data: any = []; + try { + if (res.ok) data = await res.json(); + } catch (err) { + // fallback to Supabase REST if server API not available (dev) + try { + const sbUrl = import.meta.env.VITE_SUPABASE_URL; + const sbKey = import.meta.env.VITE_SUPABASE_ANON_KEY; + if (sbUrl && sbKey) { + const url = `${sbUrl.replace(/\/$/, "")}/rest/v1/blog_posts?select=id,slug,title,excerpt,author,date,read_time,category,image,likes,comments,published_at&order=published_at.desc&limit=50`; + const sbRes = await fetch(url, { + headers: { + apikey: sbKey as string, + Authorization: `Bearer ${sbKey}`, + }, + }); + if (sbRes.ok) data = await sbRes.json(); + } + } catch (e) { + console.warn("Supabase fallback failed:", e); + data = []; + } + } + if (!cancelled && Array.isArray(data)) { const mapped: BlogPost[] = data.map((r: any) => ({ id: r.id,