Next.js Discord

Discord Forum

Benefits of server actions

Answered
Siamese Crocodile posted this in #help-forum
Open in Discord
Siamese CrocodileOP
What are the benefits when I am using an external api in comparison to client side mutations?

So let's say we have a use client form that can call either server action or normal fetch POST from the browser.
Answered by Asian black bear
For anything more conventional actions aren't truly worth it as it's a second hop.
View full answer

13 Replies

Asian black bear
Server actions are more convenient.
You just call the function directly rather than manually fetching the respective endpoint.
Siamese CrocodileOP
I dont think you understood me
I cant do

const onSubmit = () => {
    fetch('xxx').....
    vs
    someServerAction()
}

'use server'

const someServerAction = () => {
    fetch('xxx')...
}
I still have an external api hosted somewhere else
So either I run fetch inside the server action - seems redundant
or i just run fetch on the client
Asian black bear
Oh, if you would fetch an external API the only convenience you have is proxying to work around possible CORS issues.
Also, if the external API needs secrets such as API keys you can use actions for that.
Siamese CrocodileOP
yeah secrets seem a fine reason but otherwise I don't see the benefit
Asian black bear
For anything more conventional actions aren't truly worth it as it's a second hop.
Answer
Siamese CrocodileOP
sure thanks
thats what I wanted to hear