简体   繁体   English

如何使用 next.js 静态呈现动态页面,使得动态页面数据在构建期间不可用?

[英]How to statically render dynamic pages using next.js, such that the dynamic pages data is not available during build time?

According to the current docs of next.js, dynamic routes are handled by providing the route data to getStaticProps and getStaticPaths .根据 next.js 的当前文档,通过向getStaticPropsgetStaticPaths提供路由数据来处理动态路由。

What concept will enable me to implement dynamic routes with or without using getStaticProps() and getStaticPaths() , when the route data is being fetched from a database during build time?当在构建期间从数据库中获取路由数据时,什么概念将使我能够使用或不使用getStaticProps()getStaticPaths()来实现动态路由?

You are correct.你是对的。 You'll use getStaticPaths / getStaticProps to fetch data from your database.您将使用getStaticPaths / getStaticProps从数据库中获取数据。 Then, you forward props to your React component (with getStaticPaths ) and generate the routes you want (with getStaticProps ).然后,您将 props 转发到您的 React 组件(使用getStaticPaths )并生成您想要的路由(使用getStaticProps )。

For example, let's say you have a file blog/[slug].js .例如,假设您有一个文件blog/[slug].js

If you visit /blog/first , that will forward the variable first as a parameter to getStaticPaths / getStaticProps as slug .如果你访问/blog/first ,将转发可变first作为参数传递给getStaticPaths / getStaticPropsslug You can then use that value to fetch information about the given post (or anything else in your database).然后,您可以使用该值来获取有关给定帖子(或数据库中的任何其他内容)的信息。

Example:例子:

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

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