logo
  • 指南
  • 配置
  • 插件
  • API
  • 示例
  • 社区
  • Modern.js 2.x 文档
  • 简体中文
    • 简体中文
    • English
    • 配置使用
      dev
      assetPrefix
      beforeStartUrl
      client
      hmr
      host
      https
      lazyCompilation
      liveReload
      port
      progressBar
      setupMiddlewares
      startUrl
      watchFiles
      writeToDisk
      bff
      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
      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
      介绍
      plugins
      router
      performance
      buildCache
      bundleAnalyze
      chunkSplit
      dnsPrefetch
      preconnect
      prefetch
      preload
      printFileSize
      profile
      removeConsole
      removeMomentLocale
      experiments
      sourceBuild
      builderPlugins
      📝 编辑此页面
      上一页assetPrefix下一页charset

      #output.assetsRetry

      • 类型: Object

      output.assetsRetry 用于配置资源加载失败时的重试逻辑。配置类型如下:

      export type AssetsRetryHookContext = {
        times: number;
        domain: string;
        url: string;
        tagName: string;
      };
      
      export type AssetsRetryOptions = {
        type?: string[];
        domain?: string[];
        max?: number;
        test?: string | ((url: string) => boolean);
        crossOrigin?: boolean | 'anonymous' | 'use-credentials';
        inlineScript?: boolean;
        onRetry?: (options: AssetsRetryHookContext) => void;
        onSuccess?: (options: AssetsRetryHookContext) => void;
        onFail?: (options: AssetsRetryHookContext) => void;
      };
      • 默认值: undefined

      由于该能力会往 HTML 中和 webpack/Rspack Runtime 注入额外的一些运行时代码,因此我们默认关闭了该能力,如果需要开启该能力,你可以添加以下配置:

      export default {
        output: {
          assetsRetry: {},
        },
      };

      当你开启该能力后,assetsRetry 的默认配置如下:

      export const defaultAssetsRetryOptions: AssetsRetryOptions = {
        type: ['script', 'link', 'img'],
        domain: [],
        max: 3,
        test: '',
        crossOrigin: false,
        inlineScript: false,
        onRetry: () => {},
        onSuccess: () => {},
        onFail: () => {},
      };

      #示例

      你可以通过 assetsRetry 配置项,来定制你的重试逻辑。

      如,通过 assetsRetry.domain 指定资源加载失败时的重试域名列表,第一个域名应该与 assetsPrefix 配置中的域名保持一致:

      export default {
        output: {
          assetsRetry: {
            domain: ['cdn1.com', 'cdn2.com', 'cdn3.com'],
          },
          assetsPrefix: 'https://cdn1.com', // 或 '//cdn1.com'
        },
      };

      添加以上配置后,当 cdn1.com 域名的资源加载失败时,请求域名会自动降级到 cdn2.com。

      如果 cdn2.com 的资源也请求失败,则会继续请求 cdn3.com。

      assetsRetry 是基于 Rsbuild 的 Assets Retry 插件实现的,并提供相同的配置项。你可以参考 Rsbuild - Assets Retry 插件 来了解所有可用的配置项。