For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/configure/app/dev/server.md.
  • 简体中文
  • dev.server

    • 类型: Object
    • 默认值: {}

    通过 dev.server 可以修改开发环境服务器的配置。

    compress

    • 类型: boolean
    • 默认值: true

    是否对静态资源启用 gzip 压缩。

    如果你需要禁用 gzip 压缩,可以将 compress 设置为 false:

    export default {
      dev: {
        server: {
          compress: false,
        },
      },
    };

    更多详细信息请参考 Rsbuild - server.compress 文档。

    headers

    • 类型: Record<string, string>
    • 默认值: undefined

    设置自定义响应头。

    export default {
      dev: {
        server: {
          headers: {
            'X-Custom-Foo': 'bar',
          },
        },
      },
    };

    更多详细信息请参考 Rsbuild - server.headers 文档。

    historyApiFallback

    • 类型: boolean | ConnectHistoryApiFallbackOptions
    • 默认值: false

    在需要对一些 404 响应或其他请求提供替代页面的场景,可通过 dev.server.historyApiFallback 进行设置:

    export default {
      dev: {
        server: {
          historyApiFallback: true,
        },
      },
    };

    更多配置选项请参考 Rsbuild - server.historyApiFallback 文档。

    watch

    • 类型: boolean
    • 默认值: true

    是否监听 mock/、server/、api/ 等目录的文件变化。

    更多详细信息请参考 Rsbuild - dev.watchFiles 文档。

    cors

    • 类型: boolean | import('cors').CorsOptions

    为开发服务器配置 CORS(跨域资源共享)。

    Modern.js 中 cors 的默认配置遵循 Rsbuild 的默认值:

    const defaultAllowedOrigins =
      /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/;
    
    const defaultOptions = {
      // 默认允许:
      // - localhost
      // - 127.0.0.1
      // - [::1]
      origin: defaultAllowedOrigins,
    };

    更多配置选项和详细用法请参考 Rsbuild - server.cors 文档。

    proxy

    • 类型: ProxyOptions[] | Record<string, string | ProxyOptions>
    • 默认值: undefined

    为开发服务器配置代理规则,把请求转发到指定服务。

    export default {
      dev: {
        server: {
          proxy: {
            // http://localhost:8080/api -> https://example.com/api
            // http://localhost:8080/api/foo -> https://example.com/api/foo
            '/api': 'https://example.com',
          },
        },
      },
    };
    Tip

    该选项与 Rsbuild 的 server.proxy 选项一致,更多用法请参考 Rsbuild - server.proxy。