文件名即是路由

未匹配的标注

动态路由

pages/posts/[id].js => /posts/*

渲染

getStaticProps:需要动态数据

getStaticPaths(与 getStaticProps 配合):路径和数据都是动态

getStaticProps

export async function getStaticProps() {
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  return {
    props: {
      posts,
    },
  }
}

getStaticPaths

export async function getStaticPaths() {
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  const paths = posts.map((post) => ({
    params: { id: post.id },
  }))

  // { fallback: false } means other routes should 404.
  return { paths, fallback: false }
}

数据会传入 getStaticProps

getServerSideProps

跟 getStaticProps 格式一样

错误页面

正式环境,错误时,会显示 500.js 的内容

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~