简体   繁体   English

在 Next.js 中获取非 API 数据的最佳实践是什么(通过 Vercel 部署)

[英]What is the best practice to fetch non-api data in Next.js(deploying through Vercel)

I am trying to fetch my own data before rendering my application.我试图在呈现我的应用程序之前获取我自己的数据。 Fortunately, Next.js provides getStaticProps() for fetching data.幸运的是, Next.js提供了getStaticProps()来获取数据。

I am currently using fs module to fetch my data from json file in local directory.我目前正在使用fs模块从本地目录中的 json 文件中获取我的数据。

export async function getStaticProps() {
  const rawData = fs.readFileSync('./dataset/test.json');
  const data = modifyData(JSON.parse(rawData));
  return {
    props: {
      data
    }
  }
}

But the problem is, for securing my raw data, I didn't push them to the GitHub remote repository.但问题是,为了保护我的原始数据,我没有将它们推送到 GitHub 远程存储库。 Forgetting this, when I tried to deploy my app through vercel, it couldn't read any data from my GitHub repository as the repository does not contain any data to fetch from...忘记这一点,当我尝试通过 vercel 部署我的应用程序时,它无法从我的 GitHub 存储库中读取任何数据,因为存储库不包含任何要从中获取的数据......

I don't want to push my raw data to GitHub.我不想将我的原始数据推送到 GitHub。

I would like to know我想知道

  • the best practice to fetch raw data without pushing any of them to the remote repository在不将任何原始数据推送到远程存储库的情况下获取原始数据的最佳实践

If there are some fundamentals of Next.js or any other I missed, please let me know and correct me.如果有 Next.js 的一些基础知识或我遗漏的任何其他内容,请告诉我并纠正我。

Being "serverless", you've got to host your data somewhere, in a database or as a file .作为“无服务器”,您必须将数据托管在某个地方,在数据库中或作为文件

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

相关问题 使用 replaceAll 将 Next.js 应用程序部署到 Vercel 时出错 function - Error when deploying Next.js app to vercel with replaceAll function 使用 getStaticProps() 将 Next.js 部署到 Vercel 的速率限制问题 - Rate limit issue deploying Next.js to Vercel using getStaticProps() 组件内部 Next.js 数据获取的最佳实践 - Best practice for Next.js data fetching inside a component 向 Vercel 上的简单 Next.js web 应用程序添加基本密码保护的最佳方法是什么? - What is the best way add basic password protection to a simple Next.js web app on Vercel? Next.js 收藏列表最佳实践 - Next.js favorite list best practice 如何在 next.js 框架中从后端 API 获取数据 - How to fetch data from backend API in next.js framework 使用来自 API 的数据初始化 Next.js webapp 并将其添加到 state 的最佳实践是什么? - What are the best practices to initialize a Next.js webapp with data from an API and adding it to the state? 从本地 API Next.js 获取数据 - Fetch data from local API Next.js 使用 getServerSideProps 获取内部 API? (下一个.js) - Internal API fetch with getServerSideProps? (Next.js) 部署到 AWS 后无法通过 Next.Js 中的 getInitialProps 获取数据,但可以在开发环境中使用 - Unable to fetch data via getInitialProps in Next.Js after deploying to AWS, but works on development environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM