Server API
Rsbuild provides server APIs for both dev and preview servers, available through configuration, plugin hooks, and JavaScript API.
How to use
Configuration
Rsbuild provides the server.setup option to access dev and preview server instances.
Plugin hooks
Plugin authors can access dev and preview server instances through the onBeforeStartDevServer and onBeforeStartPreviewServer hooks.
JavaScript API
- Create a dev server instance via rsbuild.createDevServer:
- Get the dev server instance via rsbuild.startDevServer:
- Get the preview server instance via rsbuild.preview:
Example
Integrate with custom server
Here is an example of integrating express with Rsbuild dev server:
For detailed usage, see:
Shared API
Common methods and properties that are available in both dev and preview servers.
close
- Type:
() => Promise<void>
Calling the close() method to perform necessary cleanup operations.
In the dev server, this will also trigger the onCloseDevServer hook.
httpServer
- Type:
import('node:http').Server | import('node:http2').Http2SecureServer | null
The Node.js HTTP server instance.
- If server.https is enabled, this is an
Http2SecureServer. - If server.middlewareMode is enabled, this is
null.
middlewares
- Type:
Connect.Server
The connect instance. Can be used to attach custom middleware to the server.
See Middleware to learn more.
open
- Type:
() => Promise<void>
Open URL in the browser after starting the server.
port
- Type:
number
The resolved port number.
It starts from server.port by default, and automatically increments to an available port when occupied.
printUrls
- Type:
() => void
Print the server URLs.
Dev server API
Additional methods and properties that are only available in dev servers.
afterListen
- Type:
() => Promise<void>
Notifies Rsbuild that the custom server has successfully started. Rsbuild will trigger the onAfterStartDevServer hook at this stage.
For example:
connectWebSocket
- Type:
Activates the WebSocket connection. This ensures that HMR works properly.
Rsbuild has a built-in WebSocket handler to support HMR:
- When a user accesses a page through browser, a WebSocket connection request is automatically initiated to the server.
- After the Rsbuild dev server detects the connection request, it instructs the built-in WebSocket handler to process it.
- After the browser successfully establishes a connection with the Rsbuild WebSocket handler, real-time communication is possible.
- The Rsbuild WebSocket handler notifies the browser after each recompilation is complete. The browser then sends a
hot-update.(js|json)request to the dev server to load the new compiled module.
When you use a custom server, you may encounter HMR connection error problems. This is because the custom server does not forward WebSocket connection requests to Rsbuild's WebSocket handler.
At this time, you need to use the connectWebSocket method to enable Rsbuild to sense and process the WebSocket connection request from the browser.
environments
- Type: EnvironmentAPI
Provides Rsbuild's environment API, which allows you to get the build outputs information for a specific environment in the server side.
listen
- Type:
() => Promise<{ port: number; urls: string[]; server: RsbuildDevServer }>
Starts the server and returns the listening result.
If you are using server.middlewareMode, you usually don't need to call this method.
sockWrite
- Type:
Sends some message to HMR client, and then the HMR client will take different actions depending on the message type.
static-changed
If you send a 'static-changed' message, the page will reload.
custom
You can also send custom messages via custom type with optional data to the browser and handle them via HMR events:
Rsbuild extends the on() method on Rspack’s import.meta.webpackHot object. It allows you to listen for custom events in the browser and handle the associated data:

