Skip to content

Commit

Permalink
fix: 修复刚部署时数据为空页面崩溃的bug (CaliCastle#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle authored Dec 19, 2023
2 parents c5e2648 + b434712 commit fee4e0e
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/(main)/blog/BlogPostPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function BlogPostPage({
</time>
<span className="inline-flex items-center space-x-1.5">
<ScriptIcon />
<span>{post.categories.join(', ')}</span>
<span>{post.categories?.join(', ')}</span>
</span>
</motion.div>
<motion.h1
Expand Down
2 changes: 1 addition & 1 deletion app/(main)/blog/BlogPosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getLatestBlogPosts } from '~/sanity/queries'
import { BlogPostCard } from './BlogPostCard'

export async function BlogPosts({ limit = 5 }) {
const posts = await getLatestBlogPosts({ limit, forDisplay: true })
const posts = await getLatestBlogPosts({ limit, forDisplay: true }) || []
const postIdKeys = posts.map(({ _id }) => kvKeys.postViews(_id))

let views: number[] = []
Expand Down
2 changes: 1 addition & 1 deletion app/(main)/projects/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ProjectCard } from '~/app/(main)/projects/ProjectCard'
import { getSettings } from '~/sanity/queries'

export async function Projects() {
const { projects } = await getSettings()
const projects = (await getSettings()).projects || []

return (
<ul
Expand Down
4 changes: 2 additions & 2 deletions sanity/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const getLatestBlogPostsQuery = ({
}
}`
export const getLatestBlogPosts = (options: GetBlogPostsOptions) =>
clientFetch<Post[]>(getLatestBlogPostsQuery(options))
clientFetch<Post[] | null>(getLatestBlogPostsQuery(options))

export const getBlogPostQuery = groq`
*[_type == "post" && slug.current == $slug && !(_id in path("drafts.**"))][0] {
Expand Down Expand Up @@ -109,4 +109,4 @@ export const getSettingsQuery = () =>
}
}`
export const getSettings = () =>
clientFetch<{ projects: Project[] }>(getSettingsQuery())
clientFetch<{ projects: Project[] | null }>(getSettingsQuery())
2 changes: 1 addition & 1 deletion sanity/schemas/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const Post = z.object({
}),
publishedAt: z.string(),
description: z.string(),
categories: z.array(z.string()),
categories: z.array(z.string()).optional(),
body: z.any(),
readingTime: z.number(),
mood: z.enum(['happy', 'sad', 'neutral']),
Expand Down

0 comments on commit fee4e0e

Please sign in to comment.