[英]What should I use to host a public JSON file that rarely changes without redeploying my whole webapp?
I've a react app hosted on firebase and a realtime database for user data.我有一个托管在 firebase 上的反应应用程序和一个用于用户数据的实时数据库。
Currently, there's a json file in my app's code containing reference data.目前,我的应用程序代码中有一个 json 文件,其中包含参考数据。 This file might change once every few months.此文件可能每隔几个月更改一次。
Where could I host this file so that I could update it without redeploying the whole webapp?我可以在哪里托管这个文件,这样我就可以在不重新部署整个 web 应用程序的情况下更新它?
I thought about using Firebase Storage, but it seems inefficient to download the file on every render.我考虑过使用 Firebase 存储,但在每次渲染时下载文件似乎效率低下。 Is there a way to only download it once and cache it for a certain amount of time?有没有办法只下载一次并缓存一段时间?
thank you谢谢你
yes, you can fetch it the first time and store it in localStorate
, and about the expiration time, add a key as a specific expiration time, and when time over re-fetch it again, eg (for three month later):是的,您可以第一次获取它并将其存储在localStorate
中,关于到期时间,添加一个密钥作为特定的到期时间,当时间超过时重新获取它,例如(三个月后):
const date = new Date();
date.setMonth(date.getMonth() + 3);
const expirationTime = date.getTime();
{
data: {...some data},
expirationTime
}
and each time you have to check the expirationTime
if the current time was bigger than expirationTime
you have to re-fetch the data.并且每次您必须检查expirationTime
如果当前时间大于expirationTime
您必须重新获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.