更新NextJS后previewData不是函数错误

vsmadaxz  于 2023-05-28  发布在  其他
关注(0)|答案(1)|浏览(120)

我有一个NextJS和Sanity.io博客项目。今天我更新了NextJS从13.2.3到13.4.2,当我尝试运行它时,我得到了这个错误:
错误:(0,next_headers__WEBPACK_IMPORTED_MODULE_1__.previewData)不是函数
代码如下:

import { previewData } from "next/headers";
import { groq } from "next-sanity";
import { client } from "../../lib/sanity.client";
import PreviewSuspense from "../../components/PreviewSuspense";
import BlogList from "../../components/BlogList";
import PreviewBlogList from "../../components/PreviewBlogList";

const query = groq`
    *[_type == "post"] {
        ...,
        author->,
        categories[]->
    } | order(_createdAt desc)
`;

//export const revalidate = 60; // revalidate this page every 60 seconds

export default async function HomePage() {
  if (previewData()) {
    return (
      <PreviewSuspense
        fallback={
          <div role="status">
            <p className="text-center text-lg animate-pulse text-[#F7AB0A]">
              Loading Preview Data...
            </p>
          </div>
        }
      >
        <PreviewBlogList query={query} />
      </PreviewSuspense>
    );
  }

  const posts = await client.fetch(query);

  return <BlogList posts={posts} />;
}

我该怎么解决这个问题?我必须用别的东西吗?

ws51t4hk

ws51t4hk1#

在我的例子中,我这样实现
单位:app/api/draft/route.ts

import { draftMode } from "next/headers";
import { redirect } from "next/navigation";

export async function GET(request: Request) {
  draftMode().enable();
  redirect("/");
  // return new Response("Draft mode is enabled");
}

单位:app/api/exit-draft/route.ts

import { draftMode } from "next/headers";
import { redirect } from "next/navigation";

export async function GET(request: Request) {
  draftMode().disable();
  redirect("/");
  // return new Response("Draft mode is disabled");
}

单位:page.tsx

import Content from "@/components/home/Content";
import { draftMode } from "next/headers";

export default function Home() {
  const { isEnabled } = draftMode();
  if (isEnabled) {
    return (
      <Content>
        <p>Draft Mode</p>
      </Content>
    );
  }
  return (
    <Content>
      <p>Homepage</p>
    </Content>
  );
}

相关问题