close
logologo
指南
配置
插件
API
社区
版本
更新日志
Rsbuild 0.x 文档
English
简体中文
指南
配置
插件
API
社区
更新日志
Rsbuild 0.x 文档
English
简体中文
logologo
Overview
root
mode
plugins
logLevel
environments

dev

dev.assetPrefix
dev.browserLogs
dev.cliShortcuts
dev.client
dev.hmr
dev.lazyCompilation
dev.liveReload
dev.progressBar
dev.setupMiddlewares
dev.watchFiles
dev.writeToDisk

resolve

resolve.aliasStrategy
resolve.alias
resolve.conditionNames
resolve.dedupe
resolve.extensions
resolve.mainFields

source

source.assetsInclude
source.decorators
source.define
source.entry
source.exclude
source.include
source.preEntry
source.transformImport
source.tsconfigPath

output

output.assetPrefix
output.charset
output.cleanDistPath
output.copy
output.cssModules
output.dataUriLimit
output.distPath
output.emitAssets
output.emitCss
output.externals
output.filenameHash
output.filename
output.injectStyles
output.inlineScripts
output.inlineStyles
output.legalComments
output.manifest
output.minify
output.module
output.overrideBrowserslist
output.polyfill
output.sourceMap
output.target

html

html.appIcon
html.crossorigin
html.favicon
html.inject
html.meta
html.mountId
html.outputStructure
html.scriptLoading
html.tags
html.templateParameters
html.template
html.title

server

server.base
server.compress
server.cors
server.headers
server.historyApiFallback
server.host
server.htmlFallback
server.https
server.middlewareMode
server.open
server.port
server.printUrls
server.proxy
server.publicDir
server.strictPort

security

security.nonce
security.sri

tools

tools.bundlerChain
tools.cssExtract
tools.cssLoader
tools.htmlPlugin
tools.lightningcssLoader
tools.postcss
tools.rspack
tools.styleLoader
tools.swc

performance

performance.buildCache
performance.bundleAnalyze
performance.chunkSplit
performance.dnsPrefetch
performance.preconnect
performance.prefetch
performance.preload
performance.printFileSize
performance.profile
performance.removeConsole
performance.removeMomentLocale

moduleFederation

moduleFederation.options
📝 在 GitHub 上编辑此页
上一页server.headers
下一页server.host

#server.historyApiFallback

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

historyApiFallback 用于支持基于 history API 的路由,当用户访问不存在的路径时,自动返回指定的 HTML 文件,避免出现 404 错误。

当 Rsbuild 默认的 页面路由 行为无法满足你的需求时,例如,希望在访问 / 时可以访问 main.html ,你可以通过 server.historyApiFallback 配置项来实现这个功能。

TIP

server.historyApiFallback 的优先级高于 server.htmlFallback。

#示例

将 server.historyApiFallback 设置为 true 时,所有未匹配到实际资源的 HTML GET 请求都会返回 index.html,从而保证单页应用的路由能够正常工作。

rsbuild.config.ts
export default {
  server: {
    historyApiFallback: true,
  },
};

当满足以下条件时,Rsbuild 会将请求的路径重定向到你指定的 index 文件:

  • 请求方式为 GET 或 HEAD
  • 请求头包含 text/html
  • 请求路径中不包含 .,即不是直接的文件请求
  • 请求路径未匹配到 rewrites 中定义的任何模式

#选项

server.historyApiFallback 也支持传入一个对象来自定义行为。

#index

  • 类型: string
  • 默认值: 'index.html'

指定当启用 History API fallback 时返回的默认 HTML 文件。

例如,设置 historyApiFallback.index 为 main.html 后,当用户访问未命中的路由时,服务器会自动返回 main.html 作为回退页面。

rsbuild.config.ts
export default {
  source: {
    entry: {
      main: './src/index.ts',
    },
  },
  server: {
    historyApiFallback: {
      index: '/main.html',
    },
  },
};

#rewrites

  • 类型:
type Rewrites = Array<{
  from: RegExp;
  to: string | ((context: HistoryApiFallbackContext) => string);
}>;
  • 默认值: []

rewrites 用于在 History API fallback 发生时,自定义请求路径与页面文件之间的映射。

仅当请求未命中任何静态资源(即进入 fallback 阶段)时,这些规则才会被应用。所有规则会按照数组中的顺序依次匹配并执行。

rsbuild.config.ts
export default {
  server: {
    historyApiFallback: {
      rewrites: [
        // 将根路径重定向到 landing 页面
        { from: /^\/$/, to: '/views/landing.html' },
        // 当访问 /subpage 开头的路径时,返回 subpage.html
        { from: /^\/subpage/, to: '/views/subpage.html' },
        // 其他所有路径返回自定义的 404 页面
        { from: /./, to: '/views/404.html' },
      ],
    },
  },
};
TIP

如果你希望在非 fallback 场景下修改或转发请求,可以使用 server.proxy 配置代理规则。

#htmlAcceptHeaders

  • 类型: string[]
  • 默认值: ['text/html', '*/*']

用于覆盖匹配 HTML 内容请求时默认查询的 Accepts: 请求头。

rsbuild.config.ts
export default {
  server: {
    historyApiFallback: {
      htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
    },
  },
};

#disableDotRule

  • 类型: boolean
  • 默认值: false

默认情况下,路径中包含点(.)的请求会被视为直接的文件请求,不会被重定向。

将 disableDotRule 设置为 true 后,这一行为会被关闭,此类请求也会被重定向。

rsbuild.config.ts
export default {
  server: {
    historyApiFallback: {
      disableDotRule: true,
    },
  },
};