Learn Next.js Dashboard - Why is 'use server' not used in data.tsx?
Unanswered
Forest yellowjacket posted this in #help-forum
Forest yellowjacketOP
I'm going through the Learn Next.js Dashboard exercise and I'm trying to understand the usage of 'use server'. I understand the purpose of 'use server' (to keep the exported functions in that file only on the server). But in this file here:
https://github.com/vercel/next-learn/blob/main/dashboard/final-example/app/lib/data.ts
Why does it not have 'use server' in it? It's a file that exports functions that make SQL database calls. Later in the exercise you create this file:
https://github.com/vercel/next-learn/blob/main/dashboard/final-example/app/lib/actions.ts
In this actions.tsx it handles form submissions. So 'use server' is added here.
What is the difference in these 2 files? Both are for server-side operations. I just don't understand why one has 'use server' and one doesn't.
https://github.com/vercel/next-learn/blob/main/dashboard/final-example/app/lib/data.ts
Why does it not have 'use server' in it? It's a file that exports functions that make SQL database calls. Later in the exercise you create this file:
https://github.com/vercel/next-learn/blob/main/dashboard/final-example/app/lib/actions.ts
In this actions.tsx it handles form submissions. So 'use server' is added here.
What is the difference in these 2 files? Both are for server-side operations. I just don't understand why one has 'use server' and one doesn't.
13 Replies
American Crow
This confuses a lot of people because the naming is not good.
TL;DR: Think of
'use server' is not the opposite of 'use client'
If you do not have any notation at the top of your file the default is a server component which runs on the server.
You have to differentiate between server components and server actions.
Server actions are used for mutations (update, delete, create).
Server components are mainly used to get (read) data.
You are right both, server actions and server components run on the server but that's pretty much all they have in common.
If you have a file which holds a couple of (async) functions (no declration at the top) and you import a function into a server component you just import a function. It's just code splitting / having reusable functions
If a file does have 'use server' at the top it actually contains a collection of server actions (mutations) which can be imported from a client component.
TL;DR: Think of
'use server' as 'use server action''use server' is not the opposite of 'use client'
If you do not have any notation at the top of your file the default is a server component which runs on the server.
You have to differentiate between server components and server actions.
Server actions are used for mutations (update, delete, create).
Server components are mainly used to get (read) data.
You are right both, server actions and server components run on the server but that's pretty much all they have in common.
If you have a file which holds a couple of (async) functions (no declration at the top) and you import a function into a server component you just import a function. It's just code splitting / having reusable functions
If a file does have 'use server' at the top it actually contains a collection of server actions (mutations) which can be imported from a client component.
Forest yellowjacketOP
Hmmm okay, so if those server actions are imported from a client component, is it safe to assume that the code in those server actions remains hidden on the server? I think I'm just confused on what the point is of the differentiation
For example, you have the data.ts file that contains functions that call SQL to fetch data. These functions are imported into server components and used there.
So no 'use server' is necessary
yes code in those server actions remains hidden.
if you want to guarantee that a piece of code is never imported to the client, use
if you want to guarantee that a piece of code is never imported to the client, use
import "server-only" instead of "use server"Forest yellowjacketOP
But with the forms, you import methods from the 'actions.tsx', which are "server actions". And I guess if a component includes a form that needs to be submitted does that mean that component has to be a client component? And therefore if you want to use a function that is going to submit the data and make a database call it has to be a server action that does it?
How come a client component like a form can't import and execute methods from a file like data.ts?
is it because they would be exposed?
yes.
when there is a
* an implicit api route is created
* that api route contains the logic of the function
* on the client, it goes from running that function into
so the logic inside the
without the
when there is a
use server, what happens is this:* an implicit api route is created
* that api route contains the logic of the function
* on the client, it goes from running that function into
fetch() that api routeso the logic inside the
use server functions remain hidden on the server.without the
use server directive, none of this happens and the logic inside the functions are imported directly into the clientForest yellowjacketOP
Okay so is the reason that the code in
data.ts is safe is because it's functions are only ever imported into server-side components? And the moment I import anything from that file into a use client that code is exposed? I guess trying to run SQL on the client side wouldn't do anything anyways...@Forest yellowjacket Okay so is the reason that the code in `data.ts` is safe is because it's functions are only ever imported into server-side components? And the moment I import anything from that file into a `use client` that code is exposed? I guess trying to run SQL on the client side wouldn't do anything anyways...
And the moment I import anything from that file into a use client that code is exposed?yes. that is why, as i said above,
if you want to guarantee that a piece of code is never imported to the client, use import "server-only" instead of "use server"
Forest yellowjacketOP
Okay thanks. When did it make sense to use server instead of server only?
they are for completely different purposes.
use server is for server actions. import "server-only" is simply a build-time guard to prevent you from accidentally importing files to client components