繁体   English   中英

Next.js ISR 页面在 CMS 中删除后未删除

[英]Next.js ISR page not being deleted after deleting it in CMS

我正在尝试 Next.js 并构建一个小应用程序,该应用程序从安装了 GraphQL 的无头 WordPress 应用程序中获取帖子。 然后我使用 Apollo/Client 获取 GraphQL 内容:

阿波罗客户端.js

import { ApolloClient, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({
  uri: process.env.WORDPRESS_GRAPHQL_ENDPOINT,
  cache: new InMemoryCache(),
});

export default client;

在索引中,我抓住了帖子:

index.js

import Head from "next/head";
import styles from "../styles/Home.module.css";

import { gql } from "@apollo/client";
import Link from "next/link";
import client from "../apollo-client";

function Home(props) {
  const { posts } = props;
  return (
    <div className={styles.container}>
      <Head>
        <title>Wordpress blog posts</title>
        <meta
          name="description"
          content="Wordpress blog posts with Apollo Client"
        />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main className={styles.main}>=
        <div className={styles.grid}>
          {posts.map((post) => (
            <a
              key={post.node.databaseId}
              href={`/blog/${post.node.slug}`}
              className={styles.card}
            >
              <h2>{post.node.title}</h2>
              <div dangerouslySetInnerHTML={{ __html: post.node.excerpt }} />
            </a>
          ))}
        </div>
      </main>
    </div>
  );
}

export async function getStaticProps() {
  const { data } = await client.query({
    query: gql`
      query Posts {
        posts {
          edges {
            node {
              title
              databaseId
              slug
              excerpt(format: RENDERED)
            }
          }
        }
      }
    `,
  });

  if (data.posts.edges === 0) {
    return { notFound: true };
  }

  return {
    props: {
      posts: data.posts.edges,
    },
    revalidate: 10,
  };
}

export default Home;

然后对于单个帖子页面:

/blog/[slug].js

import Link from "next/link";
import { gql } from "@apollo/client";
import client from "../../apollo-client";

export default function BlogPage(props) {
  const { post } = props;

  if (!post) {
    return <p>Loading...</p>;
  }

  return (
    <div>
      <h1>{post.title}</h1>
      <div dangerouslySetInnerHTML={{ __html: post.content }} />
      <Link href="/">
        <a>&larr; back to home</a>
      </Link>
    </div>
  );
}

export async function getStaticProps({ params }) {
  const { slug } = params;
  const result = await client.query({
    query: gql`
      query GetWordPressPostBySlug($id: ID!) {
        post(id: $id, idType: SLUG) {
          title
          content
        }
      }
    `,
    variables: { id: slug },
  });

  if (!result.data.post) {
    return { notFound: true };
  }

  return {
    props: {
      post: result.data.post,
    },
    revalidate: 10,
  };
}

export async function getStaticPaths() {
  const result = await client.query({
    query: gql`
      query GetWordPressPosts {
        posts {
          nodes {
            slug
          }
        }
      }
    `,
  });

  return {
    paths: result.data.posts.nodes.map(({ slug }) => {
      return {
        params: { slug },
      };
    }),
    fallback: true,
  };
}

添加新帖子时它可以工作,一旦我删除它,它就不会被删除。 在执行npm run devnpm run build然后npm start时都会发生这种情况

我可能在 ISR 和重新验证的工作方式上遇到了问题。 或者我的代码中可能遗漏了什么? 任何帮助,将不胜感激。

- 编辑 -

同时,在 Stackoverflow 和 Next.js github 存储库上还有几个线程,与我所经历的有关。 相关页面:

Next.js 不删除 CMS 中删除的动态页面

https://github.com/vercel/next.js/issues/25470

Next.js ISR 页面在 CMS 中删除后未删除

如何清除 NextJs GetStaticPaths 缓存/“取消发布”动态路由?

https://github.com/vercel/next.js/discussions/18967

我不确定这是否符合您的用例,但在我从事的一个项目中,如果源数据被删除,我们会返回 404。

export async function getStaticPaths() {
  // Get the paths we want to pre-render based on posts
  // We do not need to generate anything during build
  return {
    paths: [],
    fallback: true,
  };
}

export async function getStaticProps({ params: { slug } }) {
  // Run your data fetching code here
  try {
    const data = await getData(slug);
    return {
      props: { data, slug },
      revalidate: 30,
      notFound: !data,
    };
  } catch (e) {
    return {
      props: { data: null, slug },
      revalidate: 30,
      notFound: true,
    };
  }
}

文档: https://nextjs.org/blog/next-10#redirect-and-notfound-support-for-getstaticprops--getserversideprops

我认为问题在于fallback: true 你可以在NextJS文档中找到它。 为了在您导航到已删除的路线时收到404 ,您必须指定fallback: blocking

使用动态路由时,有一些方法可以处理那些不在getStaticPaths数组中的路径。 blockingtrue

不同之处在于第一个像getServerSideProps一样工作,所以它会在服务器中获取数据,它会在服务器中生成HTML然后返回它,在以后的请求中它将提供已经 static 版本的页面。 如果您想为那些已删除的路由返回404状态代码,这是您想要使用的方式。

后备: true作品不同。 它提供页面的 static 版本,但您必须在获取数据时准备该页面以具有加载器微调器或骨架。 如果您准备页面来执行此操作,即使您的getStaticProps函数中有返回该条件的条件,它也不会返回404页面。 实际上,如果你只return notFound: true属性,你的页面会抛出错误,因为它正在等待永远不会出现的道具,因为它只返回notFound

如果您更改fallback: blocking ,运行类似getServerSideProps ,将尝试获取数据,它将不存在,因为您之前删除了它,将return notFound: true404页面错误。 如果您使用 fallback true,它将尝试提供 static 页面,然后获取数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM