简体   繁体   English

为什么 NextJS 中的 ApolloGraphQL 示例使用 getStaticProps 而不是 getServerSideProps

[英]Why Does The ApolloGraphQL example in NextJS use getStaticProps and not getServerSideProps

I'm having trouble understanding why the Apollo GraphQL example in the nextjs repo using getStaticProps.我无法理解为什么使用 getStaticProps 的 nextjs 存储库中的 Apollo GraphQL 示例。 The NextJS docs say getStaticProps is for getting data at build time. NextJS 文档说 getStaticProps 用于在构建时获取数据。

Example is here: https://github.com/vercel/next.js/tree/canary/examples/with-apollo示例在这里: https://github.com/vercel/next.js/tree/canary/examples/with-apollo

In the implementation (shown below), it is retrieving data at runtime and not build time.在实现中(如下所示),它在运行时而不是构建时检索数据。

I'm also not understanding what revalidate: 1 does as it's not used anywhere in the example, but when changing the example to use getServerSideProps, it is not a valid parameter to pass in.我也不理解revalidate: 1的作用,因为它没有在示例中的任何地方使用,但是当更改示例以使用 getServerSideProps 时,它不是要传入的有效参数。

export async function getStaticProps() {
  const apolloClient = initializeApollo()

  await apolloClient.query({
    query: ALL_POSTS_QUERY,
    variables: allPostsQueryVars,
  })

  return {
    props: {
      initialApolloState: apolloClient.cache.extract(),
    },
    revalidate: 1,
  }
}

From the doc :文档

If you export an async function called getStaticProps from a page, Next.js will pre-render this page at build time using the props returned by getStaticProps .如果您从页面导出名为 getStaticProps 的异步 function,Next.js 将在构建时使用getStaticProps返回的道具预渲染此页面。

The page is prerendered with the return value of the method at build time.该页面在构建时使用该方法的返回值进行预呈现。 So the query is executed at build time and apolloClient.cache.extract() will be used for a prerender.因此查询在构建时执行, apolloClient.cache.extract()将用于预渲染。

For revalidate :对于revalidate

revalidate - An optional amount in seconds after which a page re-generation can occur. revalidate - 以秒为单位的可选数量,之后可以重新生成页面。 More on Incremental Static Regeneration有关增量 Static 再生的更多信息

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

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