server.cors
- Type:
boolean | import('cors').CorsOptions - Default:
- Version:
>= 1.1.11
Configure CORS options for the dev server or preview server, based on the cors middleware.
object:Enable CORS with the specified options.true:Enable CORS with default options (allow all origins, not recommended).false:Disable CORS.
Warning
Using cors: true or cors.origin: '*' exposes your dev server to all origins and can compromise your source code security. We recommend using the origin option to specify an allowlist of trusted origins instead.
Example
- Enable CORS for a specific origin:
rsbuild.config.ts
- Keep the default origin config of Rsbuild and add additional origins:
rsbuild.config.ts
- Only enable CORS for the dev server:
rsbuild.config.ts
- Disable CORS:
rsbuild.config.ts
- Enable CORS for all origins (not recommended):
rsbuild.config.ts
Options
The cors option can be an object, which is the same as the cors middleware options.
The default configuration is the equivalent of:
origin
- Type:
The origin option is used to configure the Access-Control-Allow-Origin header:
rsbuild.config.ts
Specify multiple allowed origins using an array:
rsbuild.config.ts
Use a regular expression to allow all matching origins:
rsbuild.config.ts
Setting origin to a function allows you to dynamically determine the allowed origin, the function receives two parameters:
origin:The origin of the incoming request,undefinedif no origin is present.callback:A function to set the allowed origin.
rsbuild.config.ts

