Loading video...

Video Failed to Load

Go Home

Do not query db in server components

74,112 views • 1 year ago •via X (Twitter)

11 Comments

object Object's profile picture
object Object1 year ago

or use Server Actions, which are only executed on the server, but can be called from both client and server components. Only downfall is you can't call them from a route handler (IIRC)

Alex Sidorenko's profile picture
Alex Sidorenko1 year ago

The video applies to server function mutations as well. You can benefit from using data access layer in your server actions. P.S. Don't use server actions for data fetching as they execute sequentially.

MazrikBeats - check mes sons bg's profile picture
MazrikBeats - check mes sons bg1 year ago

Or use tRPC

freddie's profile picture
freddie1 year ago

so move backend code to a different file so it won’t leak, now that react allows you to call the backend in your components kind of preferred getServerSideProps then

Alex Sidorenko's profile picture
Alex Sidorenko1 year ago

React didn't come up with the concept of a data access layer. It's a general architecture pattern in software design. It's just the more React bridges the gap between client and server, the more we get exposed to backend practices that were hidden by the BE/FE split before.

Rafaell Lycan 🇧🇷🇪🇸's profile picture
Rafaell Lycan 🇧🇷🇪🇸1 year ago

Hey mate 👋 Isn't that what tRPC does under the hood? Valid points btw 👏 cc @alexdotjs

TheFullStackSnack's profile picture
TheFullStackSnack1 year ago

You don't query db in server components if you don't have server components. Problem solved.

Ramon Malcolm's profile picture
Ramon Malcolm1 year ago

server-only is a package correct?

Mubashir's profile picture
Mubashir1 year ago

valid point 👍

rohan's profile picture
rohan1 year ago

Won’t the build fail if you query the DB in a client component

Alex Sidorenko's profile picture
Alex Sidorenko1 year ago

Not if it's a dynamic route (app/note/[id]/page.tsx), as those are not generated during the build time

Related Videos