createServerRpc
Creates a server-side RPC handler that lazily loads and executes a server function. This is used internally by the build system to create efficient server-side endpoints.This is a low-level API typically used by the TanStack Start build plugin. Most users should use
createServerFn instead.Basic Usage
API Reference
ServerFnMeta
required
Metadata about the server function:
Function
required
An async function that dynamically imports and returns the server function implementation. This enables code splitting and lazy loading.
Return Value
Returns a function with the following properties:string
The URL endpoint for this server function, constructed from
TSS_SERVER_FN_BASE environment variable and the function ID.ServerFnMeta
The metadata object passed during creation.
true
Internal marker indicating this is a server function.
How It Works
- Build Time: The build plugin identifies server functions and generates RPC handlers
- Runtime: When called, the RPC handler lazy loads the actual implementation
- Execution: The loaded function is executed and its result returned
Environment Variables
string
Base URL path for server functions. Defaults to
/api/fn/. The final URL is constructed as TSS_SERVER_FN_BASE + functionId.Generated Code Example
When you define a server function:Lazy Loading Benefits
Memory Efficiency
Server functions are only loaded when called, not at startup:Code Splitting
Each server function can be in its own chunk:Internal Usage
The server handler uses this to look up and execute functions by ID:Comparison with createClientRpc
Advanced Example
Performance Considerations
Cold Starts
First call to a function will be slower due to import:Preloading
You can preload functions that you know will be needed:Bundle Size
RPC handlers add minimal overhead:Type Safety
For type-safe server functions, usecreateServerFn which preserves full type information.
Build Plugin Integration
The TanStack Start build plugin automatically:- Scans for
createServerFnusage - Extracts server function implementations
- Generates unique IDs and metadata
- Creates
createServerRpcwrappers - Generates a manifest for server-side lookups
Security
The function ID acts as a secure reference:Error Handling
Errors during import or execution are propagated:Related
- Server Functions - High-level server function API
- Client RPC - Client-side RPC calls
- Build Plugin - Generates RPC handlers