Dasto
Dasto

Reputation: 200

call api or getServerSideProps?

let's assume I have a blog-details screen and I will populate that using getServerSideProps. Should I send a request to my /api/ route and then in the API route connect to the database and return the datas? Or just connect to the database from the getServerSideProps?

I have seen very popular developers send the request to the api from getServerSideProps function but that doesn't make sense to me! why not just access the database from the getServerSideProps function and do everything their that an API route can do?

Upvotes: 0

Views: 2261

Answers (1)

Nghiệp
Nghiệp

Reputation: 4718

I quote document from Next.js: https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering

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!

Upvotes: 2

Related Questions