
'use server' Doesn't Mark Server Components: What Server Functions Really Are
- كاتب موثّق
- React 19
محتويات المقال
'use client' at the top of interactive files, so to make the page run on the server they add 'use server' to the top of OrdersPage.tsx. The file also exports cancelOrder, a small helper the page hands to its order list. The diff looks harmless and gets approved. A later security review flags it: the directive turned every export into a function the browser can call, including the one that deletes whatever order id it receives.
The common misconception
'use server' is how you mark a Server Component, the mirror image of 'use client'. Put it at the top of a file and everything in it stays on the server.
'use server' does draw a line between server and client code. It's just a different line, drawn around functions the client may call, not around components the server renders.
What actually happens
Server Components have no directive
'use client'. So making a page a Server Component takes no code at all. You only ever mark the other side: the point where client code starts. The page in the opening was a Server Component before anyone touched it; the directive changed only what the browser is allowed to call.
'use server' marks functions the client can call
'use server' goes at the top of an async function's body, or at the top of a file. Either way it creates . The framework passes the client a reference instead of the code, and when client code calls that reference, React sends a network request with a serialized copy of the arguments, runs the function on the server and sends back the serialized result. That's why the directive works only on async functions, and why arguments are limited to serializable values like strings, plain objects and FormData, never JSX or event objects.
"use server"; // meant: "this page is a Server Component"export default async function OrdersPage() { const user = await requireUser(); const orders = await findOrders(user.id); return <OrderList orders={orders} onCancel={cancelOrder} />;}// Now a Server Function too: any client, any orderIdexport async function cancelOrder(orderId: string) { await deleteOrder(orderId);}// A Server Component: no directive neededexport default async function OrdersPage() { const user = await requireUser(); const orders = await findOrders(user.id); async function cancelOrder(orderId: string) { "use server"; // only this function becomes callable const caller = await requireUser(); // check on every call await deleteOrderOwnedBy(caller.id, orderId); } return <OrderList orders={orders} onCancel={cancelOrder} />;}cancelOrder, are now Server Functions. In the right-hand version the page needs nothing to be a Server Component, and only cancelOrder is exposed. It also doesn't trust its caller: it looks up the current user again on the server and deletes the order only if that user owns it.
Calling one from the client
<form action>, the pattern from the form actions article in this path, React calls it in a for you, and with useActionState it even replays submissions made before hydration finishes. Anywhere else, the docs say to call it in a Transition yourself, which gives you a pending state, optimistic updates and error handling:
"use client";import { useTransition } from "react";type Props = { orderId: string; onCancel: (id: string) => Promise<void> };export function CancelButton({ orderId, onCancel }: Props) { const [isPending, startTransition] = useTransition(); return ( <button disabled={isPending} onClick={() => startTransition(() => onCancel(orderId))} > {isPending ? "Cancelling…" : "Cancel"} </button> );}'use server' makes a component a Server Component | |
'use server' keeps a file's code private on the server | |
How to apply it in your project
List what your 'use server' files export Search for 'use server'at the top of files. Every export is a callable endpoint; move anything that shouldn't be into a plain module.Remove the directive from components Pages and components meant to be Server Components need no directive. If one has 'use server'at the top, delete it.Guard every Server Function At the start of each one, validate the arguments and re-check on the server that the current user may do this. Move reads out, keep writes in Load data in Server Components, and call Server Functions for mutations through <form action>,useActionStateorstartTransition.
Key takeaways
There's no directive for Server Components: they're the default, and 'use client'marks where client code starts.'use server'creates Server Functions: async functions the client calls over the network with serialized arguments.At the top of a file it exposes every export, so treat each as a public endpoint: validate and authorize. Use Server Functions for mutations, called from forms or Transitions, and read data in Server Components.
التعليقات (0)