Is using server actions in client components faster than calling an API route? And is it recommended
Answered
Polar bear posted this in #help-forum
Polar bearOP
^
Answered by joulev
my answer to a different post asking almost the same question
but most of the times, server actions are the only way to handle mutations, because simple api route calls cannot easily revalidate the router cache
refreshing the component after a mutation only has one single network request (the response payload already includes the updated data for the frontend to use).
if you use react-query then pretty sure it is at least two separate requests, one to mutate and one to re-query.
requests are typically performance bottlenecks so i'd say refreshing the component with an action tends to be slightly faster. but, i don't think there is a significant difference
but most of the times, server actions are the only way to handle mutations, because simple api route calls cannot easily revalidate the router cache
2 Replies
my answer to a different post asking almost the same question
but most of the times, server actions are the only way to handle mutations, because simple api route calls cannot easily revalidate the router cache
refreshing the component after a mutation only has one single network request (the response payload already includes the updated data for the frontend to use).
if you use react-query then pretty sure it is at least two separate requests, one to mutate and one to re-query.
requests are typically performance bottlenecks so i'd say refreshing the component with an action tends to be slightly faster. but, i don't think there is a significant difference
but most of the times, server actions are the only way to handle mutations, because simple api route calls cannot easily revalidate the router cache
Answer
Polar bearOP
thank you!