Next.js Sanity 未构建博客页面

Next.js with Sanity not building Blog pages

我正在努力将 Sanity 与 Next.Js 进行首次集成,尝试将博客添加到个人网站。 Dev 工作正常,但是当我去部署或构建时,我收到一个错误,它找不到博客页面的道具之一。

抛出的错误是:

TypeError: Cannot read property 'title' of undefined

这是我用于 [slug].js 文件的内容:

import { useState, useEffect } from 'react';
import { motion } from 'framer-motion';
import Head from 'next/head';
import { useRouter } from 'next/router';
import Layout from '../../components/layout';
import Scrollbar from 'react-scrollbars-custom';

import Transitions from '../../lib/transitions';

import BlockContent from '@sanity/block-content-to-react';

import { postQuery, postSlugsQuery } from '../../lib/grocQueries';
import { getClient, overlayDrafts, sanityClient } from '../../lib/sanity.server';
import { urlForImage, usePreviewSubscription } from '../../lib/sanity';

const pageVariants = Transitions.pageVariant;
const pageTransition = Transitions.pageTransition;

export const Post = ({ data = {}, preview }) => {
    const router = useRouter();

    const slug = data?.post?.slug;
    const {
        data: { post, morePosts },
    } = usePreviewSubscription(postQuery, {
        params: { slug },
        initialData: data,
        enabled: preview && slug,
    });

    return (
        <Layout>
            <motion.article className='blog-article' initial='initial' animate='in' exit='out' variants={pageVariants} transition={pageTransition}>
                <Scrollbar style={{ width: '100%', height: '100%' }}>
                    <figure className='hero-container'>
                        <h1 className='blog-title'>{post.title} </h1>
                        {post.mainImage && <img className='blog-hero' alt='Some alt Text' src={urlForImage(post.mainImage).url()} />}
                    </figure>

                    <div className='copy-block'>
                        <BlockContent blocks={post.body} imageOptions={{ w: 860, fit: 'max' }} {...sanityClient.config()} />
                    </div>
                </Scrollbar>
            </motion.article>
        </Layout>
    );
};


export async function getStaticProps({ params, preview = false }) {
    const { post, morePosts } = await getClient(preview).fetch(postQuery, {
        slug: params.slug,
    });

    return {
        props: {
            preview,
            data: {
                post,
                morePosts: overlayDrafts(morePosts),
            },
        },
    };
}
export async function getStaticPaths() {
    const paths = await sanityClient.fetch(postSlugsQuery);
    return {
        paths: paths.map((slug) => ({ params: { slug } })),
        fallback: true,
    };
}

export default Post;
<figure className='hero-container'>
                        <h1 className='blog-title'>{post?.title} </h1>
                        {post?.mainImage && <img className='blog-hero' alt='Some alt Text' src={urlForImage(post?.mainImage).url()} />}
                    </figure>

接下来在构建期间不知道 post 对象及其键,因此最好进行可选链接。

有两件事可能需要在静态生成的页面中添加,以便它知道在没有任何道具的情况下该怎么做。

在页面中添加道具检查,所以它不会失败。

if (!props) return null // or !data in your case.

在 getStaticProps 中添加 notFound return 道具,以便它知道如何正确处理 404。

   return {
        notFound: true,
        revalidate: 300,
    }