Object{}The config of DevServer can be modified through dev.server.
booleantrueWhether to enable gzip compression for served static assets.
If you want to disable the gzip compression, you can set compress to false:
export default {
dev: {
server: {
compress: false,
},
},
};For more details, please refer to the Rsbuild - server.compress documentation.
Record<string, string>undefinedAdds headers to all responses.
export default {
dev: {
server: {
headers: {
'X-Custom-Foo': 'bar',
},
},
},
};For more details, please refer to the Rsbuild - server.headers documentation.
boolean | ConnectHistoryApiFallbackOptionsfalseThe index.html page will likely have to be served in place of any 404 responses. Enable dev.server.historyApiFallback by setting it to true:
export default {
dev: {
server: {
historyApiFallback: true,
},
},
};For more configuration options, please refer to the Rsbuild - server.historyApiFallback documentation.
booleantrueWhether to watch files change in directories such as mock/, server/, api/.
For more details, please refer to the Rsbuild - dev.watchFiles documentation.
boolean | import('cors').CorsOptionsConfigure CORS (Cross-Origin Resource Sharing) for the development server.
The default configuration for cors in Modern.js follows Rsbuild's defaults:
const defaultAllowedOrigins =
/^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/;
const defaultOptions = {
// Default allowed:
// - localhost
// - 127.0.0.1
// - [::1]
origin: defaultAllowedOrigins
};For more configuration options and detailed usage, please refer to the Rsbuild - server.cors documentation.
Record<string, string> | Record<string, ProxyDetail>undefinedProxy requests to the specified service.
export default {
dev: {
server: {
proxy: {
'/api': 'http://localhost:3000',
},
},
},
};At this point, the /api/users request will be proxied to http://localhost:3000/api/users.
If you don't want to pass /api, you can rewrite the request path through pathRewrite:
export default {
dev: {
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: { '^/api': '' },
},
},
},
},
};DevServer Proxy is implemented based on http-proxy-middleware. You can use all configuration items of http-proxy-middleware, see the documentation for details.
The full type definition of DevServer Proxy is:
import type { Options as HttpProxyOptions } from 'http-proxy-middleware';
type Filter = string | string[] | ((pathname: string, req: Request) => boolean);
type ProxyDetail = HttpProxyOptions & {
bypass?: (
req: IncomingMessage,
res: ServerResponse,
proxyOptions: ProxyOptions,
) => string | undefined | null | false;
context?: Filter;
};
type ProxyOptions =
| Record<string, string>
| Record<string, ProxyDetail>
| ProxyDetail[]
| ProxyDetail;In addition to the http-proxy-middleware options, two configuration items are also supported: bypass and context:
null or undefined will continue to process the request with proxy.false will return a 404 error.// Custom bypass method
export default {
dev: {
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
bypass: function (req, res, proxyOptions) {
if (req.headers.accept.indexOf('html') !== -1) {
console.log('Skipping proxy for browser request.');
return '/index.html';
}
},
},
},
},
},
};// Proxy multiple paths to the same target
export default {
dev: {
server: {
proxy: [
{
context: ['/auth', '/api'],
target: 'http://localhost:3000',
},
],
},
},
};