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) {
|
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) {
|
||||||
}
|
}
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
try {
|
||||||
const post = await client.fetch(query, { slug });
|
const post = await client.fetch(query, { slug });
|
||||||
return post;
|
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 {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
try {
|
||||||
const post = await client.fetch(query, { slug });
|
const post = await client.fetch(query, { slug });
|
||||||
return post || null;
|
return post || null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching blog post metadata:", error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
try {
|
||||||
const post = await client.fetch(query, { slug });
|
const post = await client.fetch(query, { slug });
|
||||||
return post || null;
|
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
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
try {
|
||||||
const posts = await client.fetch(query);
|
const posts = await client.fetch(query);
|
||||||
return posts.map((post: { slug: string }) => post.slug);
|
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() {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
try {
|
||||||
return await client.fetch(query);
|
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() {
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
export const client = hasSanityConfig
|
||||||
|
? createClient({
|
||||||
projectId,
|
projectId,
|
||||||
dataset,
|
dataset,
|
||||||
apiVersion,
|
apiVersion,
|
||||||
useCdn: true, // Set to false if statically generating pages, using ISR or using the on-demand revalidation API
|
useCdn: true, // Set to false if statically generating pages, using ISR or using the on-demand revalidation API
|
||||||
});
|
})
|
||||||
|
: null;
|
||||||
|
|
||||||
const builder = imageUrlBuilder(client);
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue