我已经使用 Next.js 一段时间了,我怀疑 获取 API 用于getStaticProps
和 getServerSideProps
.
下面我写下我对getStaticProps
的理解和 getServerSideProps
:
获取静态属性 在构建时间和 ISR 期间被调用
获取服务器端属性 将在请求时被调用,但两者都无权访问客户端。
这是我对这些异步函数的理解。
所以我怀疑我们只写服务器端代码,Node.js 没有原生的 fetch API,那么 getStaticProps
里面使用了什么 fetch API和 getServerSideProps
?是原生获取 ?或者一些 填充 取名字?
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
}
}
}
真的很想知道是哪个获取 API 用来。
最佳答案
So my doubt is we only write server-side code and node.js doesn't have a native fetch API, so what fetch API is used inside getStaticProps and getServerSideProps?
在 Node.js 环境中(
getStaticProps
、getServerSideProps
和 API 路由)Next.js 使用 node-fetch
至polyfill the fetch
API .
关于javascript - getStaticProps 和 getServerSideProps 中的 fetch 是否与 native 浏览器 fetch API 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68795872/