user14455879
user14455879

Reputation:

Why shouldn't I make api calls in getStaticProps?

I read in the nextJs docs that I shouldn't make api calls in the getStaticProps function. Can anyone explain with an example why? According to the docs, it's because server-sided code can be directly written in getStaticProps. I don't quite get what that means. Something with an example would help.

Upvotes: 1

Views: 1429

Answers (1)

Mohsin Ali
Mohsin Ali

Reputation: 391

I believe you are referring to this NextJS Data Fetching

Note: You should not use fetch() to call an API route in getServerSideProps. 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!

This note is explicitly for API endpoints built into your NextJS app that would reside under the pages/api directory as described here NextJS API Routes

They suggest moving the API logic into getServerSideProps instead of calling the API endpoint.

Upvotes: 7

Related Questions