【问题标题】:API data does not update in production, but works locallyAPI 数据不会在生产中更新,但在本地工作
【发布时间】:2021-09-15 08:59:49
【问题描述】:

我有一个问题,我看不到错误。

我使用 Next.js 制作的应用程序使用 API 来获取货币值。

在本地,日期和价格会在修改时更新。但在生产中它不起作用,它只显示部署完成时加载的最后一个数据。

我在索引中使用getStaticProps 来获取每个货币类别的数据。

export default function Home({ oficial, blue, bolsa, turista, contadoliqui }) {
  return (
    <Layout>
      <Box p={10}>
        <Stack
          display={{ md: "flex" }}
          spacing={8}
          direction={["column", "column", "row"]}
        >
          <DolarOficialCard oficial={oficial} />
          <DolarBlueCard blue={blue} />
        </Stack>
        <Stack
          display={{ md: "flex" }}
          spacing={8}
          mt={8}
          direction={["column", "column", "row"]}
        >
          <DolarBolsaCard bolsa={bolsa} />
          <DolarContado contadoliqui={contadoliqui} />
        </Stack>
        <Box direction={["column", "column", "row"]} spacing={8} mt={8}>
          <DolarTuristaCard turista={turista} />
        </Box>
        <Box align="center" mt={50}>
          <Alert status="info" justifyContent="center">
            <AlertIcon />
            Última Actualización: {oficial.fecha}
          </Alert>
        </Box>
      </Box>
    </Layout>
  );
}

export const getStaticProps = async () => {
  const oficial = await getDolarOficial();
  const blue = await getDolarBlue();
  const bolsa = await getDolarBolsa();
  const turista = await getDolarTurista();
  const contadoliqui = await getDolarLiqui();

  return {
    props: {
      oficial,
      blue,
      bolsa,
      turista,
      contadoliqui,
    },
  };
};

【问题讨论】:

    标签: javascript reactjs next.js


    【解决方案1】:

    getStaticProps 表示你的页面是静态页面,只生成一次。如果要始终获取最新数据,则需要使用getServerSideProps

    否则,您可以使用增量静态再生。您需要在预定义的“秒”内将您的 getStaticProps 发送到 revalidate

    export const getStaticProps = async () => {
      const oficial = await getDolarOficial();
      const blue = await getDolarBlue();
      const bolsa = await getDolarBolsa();
      const turista = await getDolarTurista();
      const contadoliqui = await getDolarLiqui();
    
      return {
        props: {
          oficial,
          blue,
          bolsa,
          turista,
          contadoliqui,
        },
          revalidate: 10, // Revalidate every 10 seconds with new data.
      };
    };
    

    在开发(下一个开发)中,getStaticProps 将在每个 请求。

    这就是为什么它可以在本地工作但不能在生产环境中工作的原因。

    【讨论】:

    • 我尝试了你的建议,但它在我的生产中不起作用
    • 您可能想开始一个新问题,连同您的代码一起获得答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2021-06-08
    • 2021-12-05
    • 2023-03-19
    • 2016-07-16
    • 2018-03-23
    相关资源
    最近更新 更多