📜  如何在 Next.js 中使用 getStaticPaths?

📅  最后修改于: 2022-05-13 01:56:14.633000             🧑  作者: Mango

如何在 Next.js 中使用 getStaticPaths?

我们可以通过表单交互在 Next.js 中预渲染一个页面。我们尽早制作所有的 HTML 代码和信息。然后,服务器在那时保留数据。

这种技术对于静态路径来说是非同寻常的。尽管如此,它在以动态方式传递页面时会爆炸。这也是合法的。我们应该想象有一个博客,上面有几篇文章。我们在 next.js 中描述了像 [blogId].js 这样的动态方式。正如我们肯定知道的那样,这种方式对于博客 ID 1、2、3、4 等来说非常重要。 Next.js 没有技术可以知道它必须交付的页面数量。

我们使用 getStaticPaths() 来实现这一点,在这篇博客中进一步研究。

getStaticPath 的特性:假设页面使用 getStaticProps 并具有动态路由,它应该描述静态创建的方式的概要。

当您从使用动态课程的页面发送 getStaticPaths(静态站点生成)工作时,Next.js 会静态预交付 getStaticPaths 提供的每一种方式。

  • 信息从没有提前的 CMS 开始。
  • 数据取自信息库。
  • 信息取自文件系统。
  • 信息可以直接存储(不是客户端明确的)。

句法:

export async function getStaticPaths() {
    return {
        Paths: [
            // See path selection below
            { params: { ...} }
        ],

        // See the fallback section below 
        fallback: true, false, or blogging
    };
}

回退:错误

假的,它会在那个时候只制造 getStaticPaths 返回的方式。如果您有几种方法可以做到这一点,或者没有定期添加新页面信息,则此选择很有价值。假设您观察到您确实想添加更多方式,并且您有一个备用计划:假的,您应该再次运行下一个表单,以便可以创建新方式。

后备:真

对于其余的。当有人要求此时未生成的页面时,客户端将看到带有堆栈指针或骨架部分的页面。

后备:博客

回退是“阻塞”,getStaticPaths 未返回的新方法将相信 HTML 将被生成,与 SSR 无法区分(此后为什么阻塞),然后存储以备将来的需求,因此每次只发生一次。

示例 1:

Javascript
work Post({ post }) {
    // Render post...
}
 
// This capacity gets called at fabricate time
trade async work getStaticPaths() {
 
    // Call an outside API endpoint to get posts
    const res = anticipate fetch('https://.../posts')
 
    const posts = anticipate res.json()
 
    // Get the ways we need to pre-render in light of posts
    const ways = posts.map((post) => ({
        params: { id: post.id },
    }))
 
    // We'll pre-render just these ways at assemble time.
    return { ways, backup plan: bogus }
 
}
 
// This likewise gets called at construct time  
trade async work getStaticProps({ params }) {
 
    // params contains the post 'id'.
 
    // On the off chance that the course
    // resembles/posts/1, params.id is 1
    const res = anticipate fetch('https://.../posts/${params.id}')
 
    const post = anticipate res.json()
 
    // Pass present information on the page through props
    return { props: { post } }
}


Javascript
import { useRouter } from 'next/switch'
 
work Post({ post }) {
    const switch = useRouter()
     
    // On the off chance that the page isn't
    // yet produced, this will be shown at
    // first until getStaticProps() gets
    // done with running
 
     if (router.isFallback) {
        return 
Loading...
    }       // Render post... }   // This capacity gets called at assemble time send out async work getStaticPaths() {     return {           // Just '/posts/1' and '/posts/2' are         // produced at assemble time         ways: [{ params: { id: '1' } }, { params: { id: '2' } }],           // Empower statically creating extra pages         // For instance: '/posts/3'         contingency plan: valid,     } }   // This likewise gets called at assemble time send out async work getStaticProps({ params }) {       // params contains the post 'id'. On the off chance that     // the course resembles/posts/1, params.id is 1       const res = anticipate fetch('https://.../posts/${params.id}')       const post = anticipate res.json()       // Pass present information on the page through props     return {           props: { post },           // Re-create the post all things considered         // one time each second         // assuming a solicitation comes in         revalidate: 1,     } }


输出:

Post1

示例 2:

Javascript

import { useRouter } from 'next/switch'
 
work Post({ post }) {
    const switch = useRouter()
     
    // On the off chance that the page isn't
    // yet produced, this will be shown at
    // first until getStaticProps() gets
    // done with running
 
     if (router.isFallback) {
        return 
Loading...
    }       // Render post... }   // This capacity gets called at assemble time send out async work getStaticPaths() {     return {           // Just '/posts/1' and '/posts/2' are         // produced at assemble time         ways: [{ params: { id: '1' } }, { params: { id: '2' } }],           // Empower statically creating extra pages         // For instance: '/posts/3'         contingency plan: valid,     } }   // This likewise gets called at assemble time send out async work getStaticProps({ params }) {       // params contains the post 'id'. On the off chance that     // the course resembles/posts/1, params.id is 1       const res = anticipate fetch('https://.../posts/${params.id}')       const post = anticipate res.json()       // Pass present information on the page through props     return {           props: { post },           // Re-create the post all things considered         // one time each second         // assuming a solicitation comes in         revalidate: 1,     } }

输出:

Render post 
1 2 3