在 Next.JS documentation 中,我阅读了以下内容。
Note: You should not use fetch() to call an API route in getStaticProps. Instead, directly import the logic used inside your API route. You may need to slightly refactor your code for this approach.
Fetching from an external API is fine!
但如果我不应该使用内部 API,那么就会出现两个问题。
编辑:
嗯。我想原因之一是在使用 getStaticProps 并编译站点的静态版本时,@ 编译时 API 可能未运行。但这可以通过同时运行 API 轻松解决。 (因为 GetStaticProps 与交互式页面并不真正相关,所以 POST 等)
编辑2:
这里有人也想明白了。 Next.js - Error: only absolute urls are supported 分别运行 export 和 sever ,然后您可以在需要时在静态道具中进行提取。那么至少所有的东西都在一个地方。
答案 0 :(得分:0)
getStaticProps 函数用于在加载页面时在服务器端生成数据。
答案 1:您应该在 API 路由中处理 POST/PUT/GET 等。
答案 2:getStaticProps 和 API 的主要区别在于它们运行的时间。 getStaticProps 在服务器上生成页面时运行。 API 可以在您需要时随时调用。