Server Components and Server Actions

Server Components render UI on the server and can access server-only resources without sending their implementation to the browser. Server Actions, now generally discussed as Server Functions in newer React terminology, expose async server-side functions that can be invoked from client-side interactions, commonly for mutations.

Server Components are components whose rendering happens on the server. In a React framework that supports them, they can read data or use server-only modules during rendering, and the browser receives the resulting UI representation rather than the component's server-side implementation. This can reduce client JavaScript and keep secrets or backend credentials out of the browser.

Server Components are different from Server Actions. A Server Component primarily answers “where should this component render and obtain its data?” A Server Action is an asynchronous server-side function that can be invoked as part of an interaction, such as submitting a form or updating a record.

In frameworks such as Next.js, a module can use the `"use server"` directive to mark exported functions as server-side functions. Client Components can call these functions through supported mechanisms, while the framework handles the network boundary. The function should still validate authorization and input because crossing the client/server boundary does not make caller-supplied data trustworthy.

A common architecture combines the two: a Server Component loads initial data, while a client interaction invokes a Server Action to mutate data and then refreshes or revalidates the affected UI. This avoids building a separate API endpoint for every simple mutation, although traditional APIs remain useful for external clients and broader service boundaries.

The exact APIs and naming depend on the React framework and version. When discussing an interview implementation, distinguish React's Server Components model from framework-specific features such as Next.js Server Actions or Server Functions.

Key points

Interview questions

Is `"use server"` enough to make an operation secure?

No. The directive establishes server-side execution behavior, but it does not replace authentication, authorization, or input validation. Data arriving from a client must be treated as untrusted, even when the receiving function executes on the server. The function should verify who is making the request, whether that user can perform the operation, and whether the supplied data is valid.

What is a Server Component, and why would you use one?

A Server Component is a React component that renders on the server in a framework supporting React Server Components. It can access server-side resources such as a database or filesystem without putting that implementation into the browser bundle. This can reduce client-side JavaScript and keep server-only dependencies away from the client. It is especially useful for UI whose data can be obtained during server rendering.

How are Server Actions different from Server Components?

Server Components are primarily about rendering components on the server and obtaining their data there. Server Actions are callable asynchronous server-side functions, commonly used when a user interaction needs to perform a mutation. A page might therefore use a Server Component to load existing records and a Server Action to create or update one. They address different sides of the application flow rather than being two names for the same mechanism.

When would you use an API route instead of a Server Action?

An API route is often preferable when the endpoint is a deliberate integration boundary or must be consumed by clients outside the framework's component tree. It also makes an HTTP contract explicit for mobile applications, third-party consumers, webhooks, or independently deployed services. A Server Action is attractive for tightly coupled application mutations where the framework can manage the client/server invocation. The decision should follow the required consumers and system boundary rather than treating one mechanism as universally superior.

What happens conceptually when a Client Component invokes a Server Action?

The client initiates an invocation across the framework-managed client/server boundary rather than executing the server function in the browser. The framework transports the necessary invocation data to the server, where the function executes and can perform server-side work. The result or resulting UI state is then communicated back according to the framework's protocol. Developers still need to handle validation, authorization, errors, and consistency around the mutation.