mirror of
https://github.com/katanemo/plano.git
synced 2026-06-02 14:35:14 +02:00
fix(web): make blog routes resilient without Sanity config
This commit is contained in:
parent
d5de792cb4
commit
b0809a20ab
6 changed files with 72 additions and 16 deletions
|
|
@ -39,6 +39,10 @@ function loadFont(fileName: string, baseUrl: string) {
|
|||
}
|
||||
|
||||
async function getBlogPost(slug: string) {
|
||||
if (!client) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const query = `*[_type == "blog" && slug.current == $slug && published == true][0] {
|
||||
_id,
|
||||
title,
|
||||
|
|
@ -53,8 +57,13 @@ async function getBlogPost(slug: string) {
|
|||
}
|
||||
}`;
|
||||
|
||||
const post = await client.fetch(query, { slug });
|
||||
return post;
|
||||
try {
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ interface BlogPost {
|
|||
}
|
||||
|
||||
async function getBlogPost(slug: string): Promise<BlogPost | null> {
|
||||
if (!client) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const query = `*[_type == "blog" && slug.current == $slug && published == true][0] {
|
||||
_id,
|
||||
title,
|
||||
|
|
@ -26,8 +30,13 @@ async function getBlogPost(slug: string): Promise<BlogPost | null> {
|
|||
author
|
||||
}`;
|
||||
|
||||
const post = await client.fetch(query, { slug });
|
||||
return post || null;
|
||||
try {
|
||||
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({
|
||||
|
|
|
|||
|
|
@ -25,6 +25,10 @@ interface BlogPost {
|
|||
}
|
||||
|
||||
async function getBlogPost(slug: string): Promise<BlogPost | null> {
|
||||
if (!client) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const query = `*[_type == "blog" && slug.current == $slug && published == true][0] {
|
||||
_id,
|
||||
title,
|
||||
|
|
@ -51,17 +55,31 @@ async function getBlogPost(slug: string): Promise<BlogPost | null> {
|
|||
author
|
||||
}`;
|
||||
|
||||
const post = await client.fetch(query, { slug });
|
||||
return post || null;
|
||||
try {
|
||||
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[]> {
|
||||
if (!client) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const query = `*[_type == "blog" && published == true] {
|
||||
"slug": slug.current
|
||||
}`;
|
||||
|
||||
const posts = await client.fetch(query);
|
||||
return posts.map((post: { slug: string }) => post.slug);
|
||||
try {
|
||||
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() {
|
||||
|
|
|
|||
|
|
@ -44,6 +44,10 @@ function formatDate(dateString: string): string {
|
|||
}
|
||||
|
||||
async function getBlogPosts(): Promise<BlogPost[]> {
|
||||
if (!client) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const query = `*[_type == "blog" && published == true] | order(publishedAt desc) {
|
||||
_id,
|
||||
title,
|
||||
|
|
@ -58,7 +62,12 @@ async function getBlogPosts(): Promise<BlogPost[]> {
|
|||
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() {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ interface BlogPost {
|
|||
}
|
||||
|
||||
async function getBlogPosts(): Promise<BlogPost[]> {
|
||||
if (!client) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const query = `*[_type == "blog" && published == true] | order(publishedAt desc) {
|
||||
slug,
|
||||
publishedAt,
|
||||
|
|
|
|||
|
|
@ -6,15 +6,22 @@ const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
|
|||
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET;
|
||||
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION;
|
||||
|
||||
export const client = createClient({
|
||||
projectId,
|
||||
dataset,
|
||||
apiVersion,
|
||||
useCdn: true, // Set to false if statically generating pages, using ISR or using the on-demand revalidation API
|
||||
});
|
||||
export const hasSanityConfig = Boolean(projectId && dataset && apiVersion);
|
||||
|
||||
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) {
|
||||
if (!builder) {
|
||||
throw new Error("Sanity client is not configured.");
|
||||
}
|
||||
return builder.image(source);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue