【问题标题】:Is fetch inside getStaticProps and getServerSideProps the same as the native browser fetch API?getStaticProps 和 getServerSideProps 中的 fetch 是否与本机浏览器 fetch API 相同?
【发布时间】:2021-10-18 02:15:55
【问题描述】:

我已经使用 Next.js 有一段时间了,我怀疑 getStaticPropsgetServerSideProps 内部使用的 fetch API

下面写下我对getStaticPropsgetServerSideProps的理解:

getStaticProps 在构建时和 ISR 期间被调用

getServerSideProps 将在请求时被调用,但两者都无权访问客户端。

这是我对这些异步函数的理解。

所以我怀疑我们只编写服务器端代码而 Node.js 没有本机 fetch API,那么在 getStaticPropsgetServerSideProps 中使用什么 fetch API?是 native fetch 吗?还是一些名为 fetch 的 polyfill

async function getStaticProps(ctx){
  // Which fetch API is this, browser fetch or some polyfill with same name as 
  fetch?
  const data = fetch(.../..) // Some API
  return {
    props: {
      data
    }
  }
}

  async function getServerSideProps(ctx){
  // ** Which fetch API is this, browser fetch or some polyfill with same name as 
  fetch?
  const data = fetch(.../..) // **Some API**
  return {
    props: {
      data
    }
  }
}

真的很想知道使用了哪个 fetch API

【问题讨论】:

    标签: javascript node.js reactjs next.js fetch-api


    【解决方案1】:

    所以我怀疑我们只写服务器端代码,而 node.js 没有原生的 fetch API,那么 getStaticProps 和 getServerSideProps 内部使用什么 fetch API?

    在 Node.js 环境中(getStaticPropsgetServerSideProps 和 API 路由)Next.js 使用 node-fetchpolyfill the fetch API

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2020-11-24
      • 2017-04-29
      • 2020-11-20
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      相关资源
      最近更新 更多