type Open =
| boolean
| string
| string[]
| {
target?: string | string[];
before?: () => Promise<void> | void;
};
undefined
server.open
configures which page URLs Rsbuild should automatically open in the browser after starting the server.
You can also use the --open option of Rsbuild CLI to open the pages. When using
server.open
and--open
at the same time,--open
takes precedence.
server.open
can be set to the following values.
http://localhost:<port>
. If server.host is configured, it defaults to http://<host>:<port>
.export default {
server: {
open: true,
},
};
export default {
server: {
open: 'http://localhost:3000',
},
};
http://localhost:<port>/home
:export default {
server: {
open: '/home',
},
};
export default {
server: {
open: ['/', '/about'],
},
};
export default {
server: {
open: 'http://www.example.com',
},
};
The port number that Rsbuild server listens on may change. For example, if the port is in use, Rsbuild will automatically increment the port number until it finds an available port.
To avoid server.open
becoming invalid due to port changes, you can use one of the following methods:
<port>
placeholder to refer to the current port number. Rsbuild will replace the placeholder with the actual port number it is listening on.export default {
server: {
open: 'http://localhost:<port>/home',
},
};
By default, Rsbuild opens pages in the system's default browser. You can specify which browser to use via the BROWSER
environment variable.
Rsbuild uses the open library to open browsers, and supports opening Chrome, Edge, and Firefox:
# Chrome
BROWSER=chrome npx rsbuild dev
# Edge
BROWSER=edge npx rsbuild dev
# Firefox
BROWSER=firefox npx rsbuild dev
On Windows, use cross-env to set environment variables:
npm i cross-env -D
cross-env BROWSER=chrome npx rsbuild dev
You can also refer to the app option of open
for additional BROWSER
values, such as OS-specific browser names:
# macOS
BROWSER="google chrome" npx rsbuild dev
# Linux
BROWSER="google-chrome" npx rsbuild dev
Pass browser arguments through BROWSER_ARGS
, with multiple arguments separated by spaces:
BROWSER=chrome BROWSER_ARGS="--incognito" npx rsbuild dev
On macOS, Rsbuild also supports opening the browser through AppleScript, which allows you to reuse existing browser tabs to open pages.
The following are the browser names supported by AppleScript:
For example:
BROWSER="Google Chrome Canary" npx rsbuild dev
You can set the BROWSER
environment variable in your local .env.local file. This eliminates the need to manually set it each time you start the dev server and prevents affecting other developers on the project.
# .env.local
BROWSER=chrome
By using open.before
, you can trigger a callback function before opening the page.
export default {
server: {
open: {
before: async () => {
await doSomeThing();
},
},
},
};
When using open.before
, the page URLs can be configured via open.target
.
export default {
server: {
open: {
target: ['/', '/about'],
before: async () => {
await doSomeThing();
},
},
},
};