如何解决该 api 在生产中不起作用,它不会更新数据但如果它在本地工作
我遇到了问题,但我看不到错误。
我使用 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","row"]}
>
<DolarBolsaCard bolsa={bolsa} />
<DolarContado contadoliqui={contadoliqui} />
</Stack>
<Box direction={["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,contadoliqui,},};
};
解决方法
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 将在每个 请求。
这就是为什么它可以在本地运行但不能在生产中运行的原因。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。