The solid/valid-use-server rule ensures that "use server" directives are placed in positions where the Solid 2.0 compiler actually honors them. Because the compiler ignores misplaced directives silently, this rule prevents sensitive code (like database access or secrets) from accidentally being shipped to the client browser.
This rule catches several types of mistakes:
1. Misplaced Directives
- Not in the prologue: The directive must be part of the directive prologue (the very top of a function body or module). It will not work if it follows any other statement or is placed inside a block like
if or for. - Template literals: Only plain string literals (
"use server") are valid; template literals (`use server`) are ignored by the compiler.
2. Ineligible Positions
The compiler cannot extract server functions from:
- Object-literal methods
- Getters or setters
- Class methods
Valid positions include standalone functions, function-valued properties in objects, and block-bodied arrow functions.
3. Module-level Directive File Errors
In files starting with a module-level "use server" directive, every export is treated as a server function. The rule validates:
- Non-function exports: Exporting constants (e.g.,
export const LIMIT = 10) will fail because the value is expected to be a callable server function. - Client wrapper calls: Using wrappers like
GET, live, withMeta (from @solidjs/web), or query, action, and liveQuery (from @solidjs/router) inside a directive module is invalid. The client build replaces these exports with bare server references, causing the client-side behavior of the wrapper to never run. To fix this, wrap the server function in a shared (non-directive) module instead, or use a function-level directive inside the wrapper call.