内容在Next.js重新部署之前不会更新。

huangapple go评论67阅读模式
英文:

Content is not updating until re-deploy in Next.js

问题

Next.js 12版本中,使用getServerSideProps时,我在将内容从生产CMS获取到生产博客上遇到了问题。

当我从CMS添加条目到本地应用时,它会在本地应用上更新。但它不会在生产站点上更新,除非我重新部署我的站点。
我正在寻找解决方法,考虑以下选项:

  • 我是否应该尝试像文档中那样缓存动态响应
res.setHeader(
   'Cache-Control',
   'public, s-maxage=10, stale-while-revalidate=59'
)
  • 我是否应该切换到getStaticProps,其中我可以使用revalidate属性?
  • 我是否应该使用第三方库来解决这个问题?
  • 这篇文章是否可能与我的问题相关?

以下是通过getServerSideProps获取数据的代码片段:

async function getAllArticles() {
  const { data } = await apolloClient.query({
   query: gql`
     query getAllArticles {
       name
     }
   `,
 });
 return data;
}
export async function getServerSideProps() {
  const allArticles = await getAllArticles();

  return {
    props: {
      allArticles
    },
  };
}

const Articles: NextPage = ({allArticles}) => {
... // allArticles在重新部署之前不会更新

任何帮助将不胜感激。

英文:

In Next.js version 12 app using getServerSideProps I am having an issue getting content from my production CMS onto my production blog.

When I add entries to local app from CMS, it updates on my local app.
It doesn't update my production site unless I redeploy my site on production.<br>
I'm looking for a workaround and think about those options:

  • Should I try to cache dynamic responseslike in docs?
 res.setHeader(
    &#39;Cache-Control&#39;,
    &#39;public, s-maxage=10, stale-while-revalidate=59&#39;
  )
  • should I switch to getstaticprops where I can use revalidate property?
  • should I use 3rd party library to fix the issue?
  • Could this article be related to my Problem?

Here is a snippet of the code fetching via getServerSideProps:

 async function getAllArticles() {
   const { data } = await apolloClient.query({
    query: gql`
      query getAllArticles {
        name
      }
    `,
  });
  return data;
}
export async function getServerSideProps() {
  const allArticles = await getAllArticles();

  return {
    props: {
      allArticles
    },
  };
}

const Articles: NextPage = ({allArticles}) =&gt; {
... // allArticles don&#39;t update until redeploy

Any help will be appreciated

答案1

得分: 1

If you are using a CMS for getting data, using getStaticProps with revalidate will be a better approach for performance and keeping your website updated with the latest data.

export const getStaticProps = async () => {
    const res = await axios.get("/api/...");
    const data = res?.data;

    return {
        props: {
            data,
        },
        revalidate: 20,
    }
};

function Component({ data }) {
  // render your component with the fetched data
}

In the dev environment, the website will fetch data instantly.

In the prod environment, the website will fetch data every 20 seconds.

英文:

If you are using a CMS for getting data, using getStaticProps with revalidate will be a better approach for performance and keeping your website updated with latest data.

<!-- language: lang-js -->

export const getStaticProps = async () =&gt; {
    const res = await axios.get(&quot;/api/...&quot;);
    const data = res?.data;
    
    return {
      props: {
        data,
      },
      revalidate: 20,
    }
};


function Component({ data }) {
  // render your component with the fetched data
}

<!-- end snippet -->

In dev environment the website will fetch data instantly.

In prod environment the website will fetch data each 20s.

huangapple
  • 本文由 发表于 2023年3月31日 16:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定