正在加载视频...

视频加载失败

Do not query db in server components

74,112 次观看 • 1 年前 •via X (Twitter)

11 条评论

object Object 的头像
object Object1 年前

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 的头像
Alex Sidorenko1 年前

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 的头像
MazrikBeats - check mes sons bg1 年前

Or use tRPC

freddie 的头像
freddie1 年前

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 的头像
Alex Sidorenko1 年前

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 🇧🇷🇪🇸 的头像
Rafaell Lycan 🇧🇷🇪🇸1 年前

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

TheFullStackSnack 的头像
TheFullStackSnack1 年前

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

Ramon Malcolm 的头像
Ramon Malcolm1 年前

server-only is a package correct?

Mubashir 的头像
Mubashir1 年前

valid point 👍

rohan 的头像
rohan1 年前

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

Alex Sidorenko 的头像
Alex Sidorenko1 年前

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

相关视频