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 上编辑此页
上一页performance.dnsPrefetch
下一页performance.prefetch

#performance.preconnect

  • 类型:
type Preconnect =
  | Array<
      | string
      | {
          href: string;
          crossorigin?: boolean;
        }
    >
  | undefined;
  • 默认值: undefined

注入 <link rel="preconnect"> 标签到 HTML 文件中。

#何时使用

<link> 元素的 rel 属性的 preconnect 关键字是对浏览器的一种提示,即用户很可能需要来自目标来源的资源,因此浏览器很可能通过抢先启动与该源的连接来改善用户体验。通过抢先执行部分或全部握手(HTTP 为 DNS+TCP,HTTPS 为 DNS+TCP+TLS),预连接可加快未来从给定源加载的速度。

<link rel="preconnect"> 将为未来的跨源 HTTP 请求、导航或子资源带来好处。它对同源请求没有好处,因为连接已经打开。

如果一个页面需要与许多第三方域建立连接,将它们全部预连接可能会适得其反。<link rel="preconnect"> 提示最好只用于最关键的连接。对于其他连接,只需使用 <link rel="dns-prefetch">,以节省第一步 DNS 查询的时间。

#示例

export default {
  performance: {
    preconnect: ['https://example.com/'],
  },
};

生成的 HTML 标签如下:

<link ref="preconnect" href="https://example.com" />

#选项

#href

  • 类型: string
  • 默认值: undefined

指定要预连接的 URL。

export default {
  performance: {
    // 等价于 `preconnect: ['https://example.com/'],`
    preconnect: [
      {
        href: 'https://example.com/',
      },
    ],
  },
};

#crossorigin

  • 类型: boolean
  • 默认值: false

指定是否添加 crossorigin 属性。

export default {
  performance: {
    preconnect: [
      {
        href: 'https://example.com/',
        crossorigin: true,
      },
    ],
  },
};

生成的 HTML 标签如下:

<link rel="preconnect" href="https://example.com" crossorigin />
TIP

参考该 链接 了解 crossorigin 属性的使用场景。