Skip to main content

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

  1. Build Time: The build plugin identifies server functions and generates RPC handlers
  2. Runtime: When called, the RPC handler lazy loads the actual implementation
  3. 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:
The build plugin generates:

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

createServerRpc does not provide type safety for the function signature. The splitImportFn parameter accepts any arguments and returns any value. This is by design for maximum flexibility at the build level.
For type-safe server functions, use createServerFn which preserves full type information.

Build Plugin Integration

The TanStack Start build plugin automatically:
  1. Scans for createServerFn usage
  2. Extracts server function implementations
  3. Generates unique IDs and metadata
  4. Creates createServerRpc wrappers
  5. 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: