Next.js Discord

Discord Forum

When to use 'use server'?

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Avatar
Spectacled bearOP
Hey guys,

I am usnig NextJS 13 with Supabase. I've been wrapping my head around server components and actions today, got some nice auth going.

I've now abstracted out the createServerClient from the supabase SSR package into its own function in my lib.

My question is, do I need to add 'use server' to the top of this file?

Is it just so if I go to import it into a client component it won't let me or something?
Answered by Risky did start the fire 🙃
'use server' is for server actions, the thing you may be intrested in is import "server-only" where it errors if imported into client component
View full answer

8 Replies

Avatar
Risky did start the fire 🙃
'use server' is for server actions, the thing you may be intrested in is import "server-only" where it errors if imported into client component
Answer
Avatar
Spectacled bearOP
okay, so adding 'use server' to my lib functions does nothing?
Avatar
Risky did start the fire 🙃
It could open you to vulnerabilitys as server actions expose an API effectively
Avatar
Spectacled bearOP
and thanks. i used the 'use server' for the server actions for forms
ahh i see
Avatar
Risky did start the fire 🙃
It's kinda badly named imo;(
Avatar
Spectacled bearOP
yeah ive just come back to nextjs after a long time and everything is really confusing
but it looks like it will be better i think in the long run where you opt into stuff which you want rendered on the client