fix(web): make blog routes resilient without Sanity config

This commit is contained in:
Spherrrical 2026-04-01 15:40:39 -07:00
parent d5de792cb4
commit b0809a20ab
6 changed files with 72 additions and 16 deletions

View file

@ -39,6 +39,10 @@ function loadFont(fileName: string, baseUrl: string) {
} }
async function getBlogPost(slug: string) { async function getBlogPost(slug: string) {
if (!client) {
return null;
}
const query = `*[_type == "blog" && slug.current == $slug && published == true][0] { const query = `*[_type == "blog" && slug.current == $slug && published == true][0] {
_id, _id,
title, title,
@ -53,8 +57,13 @@ async function getBlogPost(slug: string) {
} }
}`; }`;
const post = await client.fetch(query, { slug }); try {
return post; const post = await client.fetch(query, { slug });
return post;
} catch (error) {
console.error("Error fetching blog post for OG image:", error);
return null;
}
} }
function formatDate(dateString: string): string { function formatDate(dateString: string): string {

View file

@ -17,6 +17,10 @@ interface BlogPost {
} }
async function getBlogPost(slug: string): Promise<BlogPost | null> { async function getBlogPost(slug: string): Promise<BlogPost | null> {
if (!client) {
return null;
}
const query = `*[_type == "blog" && slug.current == $slug && published == true][0] { const query = `*[_type == "blog" && slug.current == $slug && published == true][0] {
_id, _id,
title, title,
@ -26,8 +30,13 @@ async function getBlogPost(slug: string): Promise<BlogPost | null> {
author author
}`; }`;
const post = await client.fetch(query, { slug }); try {
return post || null; const post = await client.fetch(query, { slug });
return post || null;
} catch (error) {
console.error("Error fetching blog post metadata:", error);
return null;
}
} }
export async function generateMetadata({ export async function generateMetadata({

View file

@ -25,6 +25,10 @@ interface BlogPost {
} }
async function getBlogPost(slug: string): Promise<BlogPost | null> { async function getBlogPost(slug: string): Promise<BlogPost | null> {
if (!client) {
return null;
}
const query = `*[_type == "blog" && slug.current == $slug && published == true][0] { const query = `*[_type == "blog" && slug.current == $slug && published == true][0] {
_id, _id,
title, title,
@ -51,17 +55,31 @@ async function getBlogPost(slug: string): Promise<BlogPost | null> {
author author
}`; }`;
const post = await client.fetch(query, { slug }); try {
return post || null; const post = await client.fetch(query, { slug });
return post || null;
} catch (error) {
console.error("Error fetching blog post:", error);
return null;
}
} }
async function getAllBlogSlugs(): Promise<string[]> { async function getAllBlogSlugs(): Promise<string[]> {
if (!client) {
return [];
}
const query = `*[_type == "blog" && published == true] { const query = `*[_type == "blog" && published == true] {
"slug": slug.current "slug": slug.current
}`; }`;
const posts = await client.fetch(query); try {
return posts.map((post: { slug: string }) => post.slug); const posts = await client.fetch(query);
return posts.map((post: { slug: string }) => post.slug);
} catch (error) {
console.error("Error fetching blog slugs:", error);
return [];
}
} }
export async function generateStaticParams() { export async function generateStaticParams() {

View file

@ -44,6 +44,10 @@ function formatDate(dateString: string): string {
} }
async function getBlogPosts(): Promise<BlogPost[]> { async function getBlogPosts(): Promise<BlogPost[]> {
if (!client) {
return [];
}
const query = `*[_type == "blog" && published == true] | order(publishedAt desc) { const query = `*[_type == "blog" && published == true] | order(publishedAt desc) {
_id, _id,
title, title,
@ -58,7 +62,12 @@ async function getBlogPosts(): Promise<BlogPost[]> {
featured featured
}`; }`;
return await client.fetch(query); try {
return await client.fetch(query);
} catch (error) {
console.error("Error fetching blog posts:", error);
return [];
}
} }
export default async function BlogPage() { export default async function BlogPage() {

View file

@ -10,6 +10,10 @@ interface BlogPost {
} }
async function getBlogPosts(): Promise<BlogPost[]> { async function getBlogPosts(): Promise<BlogPost[]> {
if (!client) {
return [];
}
const query = `*[_type == "blog" && published == true] | order(publishedAt desc) { const query = `*[_type == "blog" && published == true] | order(publishedAt desc) {
slug, slug,
publishedAt, publishedAt,

View file

@ -6,15 +6,22 @@ const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET; const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET;
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION; const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION;
export const client = createClient({ export const hasSanityConfig = Boolean(projectId && dataset && apiVersion);
projectId,
dataset,
apiVersion,
useCdn: true, // Set to false if statically generating pages, using ISR or using the on-demand revalidation API
});
const builder = imageUrlBuilder(client); export const client = hasSanityConfig
? createClient({
projectId,
dataset,
apiVersion,
useCdn: true, // Set to false if statically generating pages, using ISR or using the on-demand revalidation API
})
: null;
const builder = client ? imageUrlBuilder(client) : null;
export function urlFor(source: SanityImageSource) { export function urlFor(source: SanityImageSource) {
if (!builder) {
throw new Error("Sanity client is not configured.");
}
return builder.image(source); return builder.image(source);
} }