Skip navigation
React Server Components allow you to call your database directly from a component.
0:00 / 1:25

React Server Components allow you to call your database directly from a component.

Lee Robinson

@leerob307K followers

Visit on X
835 likesDownloadOriginal post

177,102 views

React Server Components allow you to call your database directly from a component. And that's great... but how do we make sure this code never ever runs on the client?

10 Comments

Comments from the original post on X (Twitter)

  • Lee Robinson's profile picture

    @leeerob

    "Wait, is this a Next.js feature?" Nope — this is for usage with any React server framework.

    27 likes

  • David K 🎹's profile picture

    @DavidKPiano

    Love the video Don't love the fact that you have to install another package and opt-in to this major footgun defender

    28 likes

  • Alex Moldovan is on 🦋's profile picture

    @alexnmoldovan

    This makes sense as a protection layer, but isn't it just a self-inflicted problem in the first place? Shouldn't the framework protect the developers from mistakes instead of requiring you to know this workaround?

    19 likes

  • Lee Robinson's profile picture

    @leeerob

    It helps to work backwards from the ideal DX: what if you didn't have to write anything, and the framework could just magically figure everything out for you? And then look at reality. We need some sort of signal to tell the compiler the environment the code runs in. Ideally we don't want to add these checks in every single file. And ideally it wouldn't be something specific to one framework. Instead, you want a standard for the ecosystem (whether Next or other frameworks). A single line to enforce either "server-only" or "client-only". It's not auto-magical but some explicitness is a feature and not a bug. It's an adjustment though because most apps have completely separate frontend and backend words, and this model pairs well when building a "data access layer" inside of a fullstack app.

    9 likes

  • Lee Robinson's profile picture

    @leeerob

    If you want to learn more, this post goes in much more detail on security in Next.js.

    18 likes

  • Dailos R. DÍAZ LARA 🦋's profile picture

    @ddialar

    IMHO a component never should call a database directly. I don’t mind if it’s a server component. Why we don’t call the database endpoint directly from a client? Cos it’s a security breach. Technology go ahead fast but it doesn’t mean that we should forget foundations.

    10 likes

  • Lee Robinson's profile picture

    @leeerob

    More often, you're likely calling a "data access layer" where you co-locate your backend logic.

    6 likes

  • Daniel Nagy's profile picture

    @danielnagydotme

    Decent workaround but importing a module to guard against code running in the wrong environment is not great.

    5 likes

  • Anthony Holmes's profile picture

    @imanthonyholmes

    This is great but now we have use client, use server, and this? I can see this being very confusing

    4 likes

  • Lee Robinson's profile picture

    @leeerob

    I like the framing here personally!

    11 likes