#Server Action vs Route Handler

22 messages · Page 1 of 1 (latest)

mint thicket
#

I'm wondering, when should we use server actions instead of route handler? And when should we use route handler instead of server actions? (In nextjs 13). Thank you.

chrome horizonBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

woeful patio
simple moth
#

Route handlers are used for building normal API endpoints, e.g. GET, POST, PUT.
They are stable and recommended for implementing APIs in your Next.js application.
Also, route handlers support more advanced usage. You can have segment configurations or dynamic segments in the handlers.
Read the docs for further information.

Server actions are currently in beta, and we usually use it in forms.

return <form action={action}>
  ...
</form>

Generally, we will use route handlers when fetching data from the client side (or clients-side revalidation), and use Server Actions for mutations.

Learn about how to configure options for Next.js route segments.

mint thicket
simple moth
#

Actually server actions can return something to the client

#

but anyway it's still recommended to use route handlers with libraries like React Query for client-side data fetching.

mint thicket
simple moth
#

But it's common that people want more advanced revalidation options (as you know there are a lot of issues currently about caching)
In these cases, we will prefer client-side revalidation instead

#

Whenever possible, just pass the data from server components and you won't need to create a route handler at all.
(but it's not client-side fetching anymore)

mint thicket
simple moth
#

just return a value in the server action, and receive it using useTransition

#

If you're not using mutations in the server action, you can even invoke it directly in a client component:

'use client'
// from a file with "use server"
import { myAction } from './actions'

export function HelloWorld() {
  return (
    <button onClick={() => myAction().then(res => console.log(res))}>
      Start
    </button>
  )
}

hidden bronze
#

I’m fairly sure you can do that even when you use mutations in server actions too

#

Because I’m doing exactly that in one of my apps

supple granite
hidden bronze
#

Hmm interesting, because I do mutation + revalidatePath in an action and just call it in an onClick like that and it works well

simple moth
#

Maybe continue to discuss in #discussions?
I think it's likely a docs problem, useTransition doesn't affect how you invoke a server action, and as I know there's nothing prevents you to use it without startTransition.

The library for web and native user interfaces

#

It's like converting a sync function to async instead (making it non-block)

mint thicket
#

okayy, Thank youu!