logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Configuration
      dev
      assetPrefix
      beforeStartUrl
      client
      hmr
      host
      https
      lazyCompilation
      liveReload
      progressBar
      server
      setupMiddlewares
      startUrl
      watchFiles
      writeToDisk
      bff
      crossProject
      prefix
      html
      appIcon
      crossorigin
      favicon
      inject
      meta
      mountId
      outputStructure
      scriptLoading
      tags
      templateParameters
      template
      title
      tools
      autoprefixer
      babel
      bundlerChain
      cssExtract
      cssLoader
      devServer
      htmlPlugin
      less
      lightningcssLoader
      minifyCss
      postcss
      rspack
      sass
      styleLoader
      swc
      tsChecker
      source
      aliasStrategy
      alias
      configDir
      decorators
      define
      disableDefaultEntries
      enableAsyncEntry
      entriesDir
      entries
      exclude
      globalVars
      include
      mainEntryName
      preEntry
      transformImport
      resolve
      aliasStrategy
      alias
      conditionNames
      dedupe
      extensions
      server
      baseUrl
      port
      publicRoutes
      routes
      rsc
      ssrByEntries
      ssr
      output
      assetPrefix
      assetsRetry
      charset
      cleanDistPath
      convertToRem
      copy
      cssModules
      dataUriLimit
      disableCssModuleExtension
      disableInlineRuntimeChunk
      disableSvgr
      disableTsChecker
      distPath
      enableAssetManifest
      enableCssModuleTSDeclaration
      disableInlineRouteManifests
      externals
      filenameHash
      filename
      injectStyles
      inlineScripts
      inlineStyles
      legalComments
      minify
      overrideBrowserslist
      polyfill
      sourceMap
      splitRouteChunks
      ssg
      ssgByEntries
      svgDefaultExport
      tempDir
      plugins
      security
      checkSyntax
      nonce
      sri
      runtime
      Introduce
      plugins
      router
      performance
      buildCache
      chunkSplit
      dnsPrefetch
      preconnect
      prefetch
      preload
      printFileSize
      profile
      removeConsole
      removeMomentLocale
      experiments
      sourceBuild
      builderPlugins
      📝 Edit this page
      Previous pageprogressBarNext pagesetupMiddlewares

      #dev.server

      • Type: Object
      • Default: {}

      The config of DevServer can be modified through dev.server.

      #compress

      • Type: boolean
      • Default: true

      Whether 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.

      #headers

      • Type: Record<string, string>
      • Default: undefined

      Adds headers to all responses.

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

      For more details, please refer to the Rsbuild - server.headers documentation.

      #historyApiFallback

      • Type: boolean | ConnectHistoryApiFallbackOptions
      • Default: false

      The 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.

      #watch

      • Type: boolean
      • Default: true

      Whether to watch files change in directories such as mock/, server/, api/.

      For more details, please refer to the Rsbuild - dev.watchFiles documentation.

      #cors

      • Type: boolean | import('cors').CorsOptions

      Configure 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.

      #proxy

      • Type: Record<string, string> | Record<string, ProxyDetail>
      • Default: undefined

      Proxy 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:

      • bypass: Bypass the proxy based on the return value of the function.
        • Returning null or undefined will continue to process the request with proxy.
        • Returning false will return a 404 error.
        • Returning a specific service path will use this path to replace the original request path.
      • context: If you want to proxy multiple specific paths to the same target, you can use the context configuration item.
      // 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',
              },
            ],
          },
        },
      };