tRPC Delay
How and why an artificial delay is added to tRPC calls in development.
To help test loading states and simulate network latency during local development, an artificial delay is automatically added to all tRPC API calls.
How it Works
- Middleware: A tRPC middleware (
timingMiddleware
insrc/server/api/trpc.ts
) wraps API calls. - Condition: It checks if the environment is development (
NODE_ENV !== 'production'
) AND if theENABLE_ARTIFICIAL_TRPC_DELAY
environment variable is set totrue
. - Delay: If both conditions are met, a random delay between 100ms and 500ms is added using
setTimeout
. - Logging: The actual execution time (including the delay) is logged to the server console.
Configuration
This delay is enabled by default in development.
To disable the artificial delay:
-
Open your
.env
file. -
Set the
ENABLE_ARTIFICIAL_TRPC_DELAY
variable tofalse
: -
Restart your development server (
npm run dev
).
Note: This delay and the environment variable have no effect in production builds.