跳转至主内容
Version: 2.0.0-beta.20

浏览器支持

Docusaurus 允许网站通过 browserslist 配置来定义支持的浏览器列表。

目的

网站需要在向后兼容性和文件大小间做出平衡。 As old browsers do not support modern APIs or syntax, more code is needed to implement the same functionality.

For example, you may use the optional chaining syntax:

const value = obj?.prop?.val;

...which unfortunately is only recognized by browser versions released after 2020. To be compatible with earlier browser versions, when building your site for production, our JS loader will transpile your code to a more verbose syntax:

var _obj, _obj$prop;

const value =
(_obj = obj) === null || _obj === void 0
? void 0
: (_obj$prop = _obj.prop) === null || _obj$prop === void 0
? void 0
: _obj$prop.val;

However, this penalizes all other users with increased site load time because the 29-character line now becomes 168 characters—a 6-fold increase! (In practice, it will be better because the names used will be shorter.) As a tradeoff, the JS loader only transpiles the syntax to the degree that's supported by all browser versions defined in the browser list.

默认情况下,浏览器列表由 package.json 文件作为根browserslist 字段提供。

caution

On old browsers, the compiled output will use unsupported (too recent) JS syntax, causing React to fail to initialize and end up with a static website with only HTML/CSS and no JS.

默认值

使用默认的 classic 模板初始化的网站,会在 package.json 中有以下内容:

package.json
{
"name": "docusaurus",
// ...
"browserslist": {
"production": [">0.5%", "not dead", "not op_mini all"],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
// ...
}

用人话来说,生产环境中所支持的浏览器为:

  • 市场份额超过 0.5 % 的浏览器,并且
  • 在过去 24 个月里有官方支持或更新(与 "dead" 相反),并且
  • 不是 Opera Mini。

而在开发环境中支持的浏览器为:

  • 最新版本的 Chrome Firefox Safari。

You can "evaluate" any config with the browserslist cli to obtain the actual list:

npx browserslist --env="production"

The output is all browsers supported in production. Below is the output in January 2022:

and_chr 96
and_uc 12.12
chrome 96
chrome 95
chrome 94
edge 96
firefox 95
firefox 94
ie 11
ios_saf 15.2
ios_saf 15.0-15.1
ios_saf 14.5-14.8
ios_saf 14.0-14.4
ios_saf 12.2-12.5
opera 82
opera 81
safari 15.1
safari 14.1
safari 13.1

了解更多

要深入了解,您可访问 browserslist 文档,特别是关于有效查询值最佳实践的内容。