[]previous and next page

The "use server" directive

The "use server" directive in the Server functions guide.

For agentsUpdated

The directive is the second spelling. It needs no import, and it marks plain async functions.

  • File level. "use server" as the first statement makes every export a server function. Each export must be an async function (a declaration, an async arrow or function expression bound to a const, a default export, or a listed export { f as g }). Type exports are allowed; values, classes and re-exports are not.
  • Function level. "use server" as the first statement of an async function. An exported function becomes a server function under its export name. A function defined elsewhere (inside a component, say) is hoisted to a module-level export named $$server_<n> in source order, so reordering such functions changes their ids.

Directive functions are always POST, take no schema and no middleware, and cannot accept forms. The server calls them as handler(input, event); the stub sends exactly one argument, so a second parameter is the event, and the first is whatever the client sent: validate it yourself.