Loading video...
Video Failed to Load
Do not query db in server components
74,112 views • 1 year ago •via X (Twitter)
11 Comments

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)

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.

Or use tRPC

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

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.

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

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

server-only is a package correct?

valid point 👍

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

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