Next.js Discord

Discord Forum

Is it a good architecture using db queries in server components?

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
So i want to know if calling the db directly is a good practise for production, im using nextjs but on server components i still call my api route (in nextjs) but i might be the one who is wrong because i see a lot of people basically making db calls directly on the server components, what should you use for production?
Answered by Rafael Almeida
You shouldn't be making fetch calls to your own API Routes because it introduces an unnecessary round trip to fetch your own server. You can do the same logic of the API Route directly in the component without issues
View full answer

8 Replies

You shouldn't be making fetch calls to your own API Routes because it introduces an unnecessary round trip to fetch your own server. You can do the same logic of the API Route directly in the component without issues
Answer
@Giant panda fyi, we have a FAQ for exactly that: https://nextjs-faq.com/fetch-api-in-rsc
Pacific herring
Basically is only good to fetch in clientside components?
(or fetch to other apps)
@Pacific herring (or fetch to other apps)
Giant panda
The other way round.
But yeah route handlers only for client-side fetches and 3rd party access to your public API
Pacific herring
Alright
Masai LionOP
thanks guys!