简体   繁体   English

为什么在Next.js中调用getStaticProps会生成JSON文件?

[英]Why is a JSON file generated when calling getStaticProps in Next.js?

Next.js newbie here. Next.js 新手在这里。 I've been reading through the Next.js docs and came across this:我一直在阅读Next.js 文档并遇到了这个:

Next.js generates a JSON file holding the result of running getStaticProps . Next.js 生成一个 JSON 文件,其中包含运行getStaticProps的结果。 The JSON file will be used in client-side routing through next/link or next/router . JSON 文件将用于通过next/linknext/router进行客户端路由。 When you navigate to a page that's pre-rendered using getStaticProps , Next.js fetches this JSON file (pre-computed at build time) and uses it as the props for the page component.当您导航到使用getStaticProps预渲染的页面时,Next.js 会获取此 JSON 文件(在构建时预先计算)并将其用作页面组件的道具。

My question is, why is this JSON file needed?我的问题是,为什么需要这个 JSON 文件? If the page is rendered into static HTML at build time using the props provided by getStaticProps , why does the page component exist at runtime and needs access to those props?如果页面在构建时使用getStaticProps提供的道具呈现为 static HTML ,为什么页面组件在运行时存在并且需要访问这些道具?

JSON file is more lightweight and faster than html file. JSON 文件比 html 文件更轻量和更快。 At the same time when getStaticProps fetch data from a backend the backed give response in a json formatted data.同时,当getStaticProps从后端获取数据时,后端会以 json 格式的数据给出响应。 It's hard to decode the data every time and make a html file for that.每次都很难解码数据并为此制作 html 文件。

Another reason why getStaticProps generates a JSON file is getStaticProps gives you another kind of rendering where you can use revalidate property is the amount in seconds after which a page re-generation. getStaticProps生成 JSON 文件的另一个原因是getStaticProps 为您提供了另一种渲染方式,您可以在其中使用revalidate属性,即重新生成页面后的秒数。 It means the JSON file will re-generate after that second.这意味着 JSON 文件将在那一秒后重新生成。 And JSON file generate is more easy than the html file cause backend gives you data response in a JSON format and you don't need to decode it.并且 JSON 文件生成比 html 文件更容易,因为后端以 JSON 格式为您提供数据响应,您不需要对其进行解码。

Suppose you have a array of product and you want to show in a page and every product in a single product page.假设您有一系列产品,并且希望在一个页面中显示,并且每个产品都显示在单个产品页面中。 In this scenario if you have a JSON formatted data you can fetch the data for all product page and single product page.在这种情况下,如果您有 JSON 格式的数据,您可以获取所有产品页面和单个产品页面的数据。 But if you think the html scenario, you have to make a html file for all product then you have to make particularly html file for every single product.但是,如果您认为 html 方案,您必须为所有产品制作 html 文件,那么您必须为每个产品制作特别的 html 文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM