completionId: cgen-524f9880492f4a358c7402f466038e1d
cgen-524f9880492f4a358c7402f466038e1d
This commit is contained in:
parent
68de139298
commit
c6e3f7b4e6
1 changed files with 31 additions and 23 deletions
|
|
@ -29,32 +29,40 @@ export default function BlogPost() {
|
||||||
let data: any = null;
|
let data: any = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Attempt to parse JSON response from server route
|
// Attempt to parse JSON response from server route
|
||||||
if (res.ok) data = await res.json();
|
if (res.ok) data = await res.json();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// If server returned HTML (dev server) or invalid JSON, fall back to Supabase REST
|
// If server returned HTML (dev server) or invalid JSON, fall back to Supabase REST
|
||||||
try {
|
try {
|
||||||
const sbUrl = import.meta.env.VITE_SUPABASE_URL;
|
const sbUrl = import.meta.env.VITE_SUPABASE_URL;
|
||||||
const sbKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
const sbKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
||||||
if (sbUrl && sbKey) {
|
if (sbUrl && sbKey) {
|
||||||
const url = `${sbUrl.replace(/\/$/, "")}/rest/v1/blog_posts?slug=eq.${encodeURIComponent(
|
const url = `${sbUrl.replace(/\/$/, "")}/rest/v1/blog_posts?slug=eq.${encodeURIComponent(
|
||||||
String(slug),
|
String(slug),
|
||||||
)}&select=id,slug,title,excerpt,author,date,read_time,category,image,body_html,published_at`;
|
)}&select=id,slug,title,excerpt,author,date,read_time,category,image,body_html,published_at`;
|
||||||
const sbRes = await fetch(url, {
|
const sbRes = await fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
apikey: sbKey as string,
|
apikey: sbKey as string,
|
||||||
Authorization: `Bearer ${sbKey}`,
|
Authorization: `Bearer ${sbKey}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (sbRes.ok) {
|
if (sbRes.ok) {
|
||||||
const arr = await sbRes.json();
|
const arr = await sbRes.json();
|
||||||
data = Array.isArray(arr) && arr.length ? arr[0] : null;
|
data = Array.isArray(arr) && arr.length ? arr[0] : null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
console.warn("Supabase fallback fetch failed:", err);
|
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn("Supabase fallback fetch failed:", err);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If API and Supabase both fail, try seed data
|
||||||
|
if (!data) {
|
||||||
|
const seedPost = blogSeedPosts.find((p) => p.slug === slug);
|
||||||
|
if (seedPost) {
|
||||||
|
data = seedPost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!cancelled) setPost(data);
|
if (!cancelled) setPost(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue