server.setup
- Type:
- Default:
undefined - Version:
>= 2.0.0
Run setup logic for both dev and preview servers.
Primarily used to register middleware, and can also run other startup tasks. Similar to onBeforeStartDevServer and onBeforeStartProdServer hooks.
Basic usage
server.setup receives a context object:
context.action:'dev'or'preview'.context.server.middlewares: the connect app instance. Usemiddlewares.use(...)to register middleware.context.environments: environment contexts of all environments.
server.setup is mainly for registering middleware, and can also run other startup tasks.
It is similar to onBeforeStartDevServer and onBeforeStartProdServer, but configured in rsbuild.config.ts.
The setup function runs before Rsbuild registers built-in middlewares, so middlewares added in setup run earlier than built-ins. It can return a callback, and that callback runs after built-in middlewares are registered.
Post callback
Use a returned callback to register middleware after built-in middlewares:
Multiple functions
server.setup also supports passing an array:
Dev-only APIs
In dev mode, you can access dev-only capabilities such as server.sockWrite:

