Modules marked with "use server" are transformed to create a secure RPC (Remote Procedure Call) boundary between the client and the server.
The client and ssr Environments
To prevent leaking server-side implementation to the browser, the entire module implementation is stripped and replaced with an RPC stub created by createServerReference.
The worker Environment
In the worker environment, the "use server" directive is removed so the code can execute. Additionally, registerServerReference is called for each export to attach metadata, allowing the framework's RPC layer to route incoming client calls to the correct function.
Summary of Transformation Logic
| Environment | "use client" Behavior | "use server" Behavior |
|---|
worker | Uses registerClientReference to provide both placeholders (for RSC) and real objects (for SSR/Server logic). | Removes directive and calls registerServerReference to enable RPC routing. |
client / ssr | Removes directive; uses full implementation. | Strips implementation; replaces with createServerReference RPC stubs. |
// Example of a transformed "use server" module in the `client` environment
import { createServerReference } from "rwsdk/client";
export let sendMessage = createServerReference("/src/actions/sendMessage.ts", "sendMessage");
// Example of a transformed "use server" module in the `worker` environment
import { registerServerReference } from "rwsdk/worker";
export async function sendMessage(message: string) {
// ... database logic
return { success: true };
}
registerServerReference(sendMessage, "/src/actions/sendMessage.ts", "sendMessage");