Assets Retry Plugin

The Assets Retry plugin is used to automatically resend requests when static assets fail to load.

Quick Start

Install Plugin

You can install the plugin using the following command:

npm add @rsbuild/plugin-assets-retry -D

Register Plugin

You can register the plugin in the rsbuild.config.ts file:

import { pluginAssetsRetry } from '@rsbuild/plugin-assets-retry';

export default {
  plugins: [pluginAssetsRetry()],


You can configure the retry behaviour for assets retry through the options.

  • Type:
type AssetsRetryHookContext = {
  times: number;
  domain: string;
  url: string;
  tagName: string;

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;
  • Default:
const defaultAssetsRetryOptions = {
  type: ['script', 'link', 'img'],
  domain: [],
  max: 3,
  test: '',
  crossOrigin: false,
  onRetry: () => {},
  onSuccess: () => {},
  onFail: () => {},


  • Type: string[]
  • Default: []

Specifies the retry domain when assets fail to load. In the domain array, the first item is the currently used domain, and the following items are backup domains. When a asset request for a domain fails, Rsbuild will find that domain in the array and replace it with the next domain in the array.

For example:

  domain: ['', '', ''],

After adding the above configuration, when assets fail to load from the domain, the request domain will automatically fallback to

If the assets request for also fails, the request will fallback to


  • Type: string[]
  • Default: ['script', 'link', 'img']

Used to specify the HTML tag types that need to be retried. By default, script tags, link tags, and img tags are processed, corresponding to JS code, CSS code, and images.

For example, only script tags and link tags are processed:

  type: ['script', 'link'],


  • Type: number
  • Default: 3

The maximum number of retries for a single asset. For example:

  max: 5,


  • Type: string | ((url: string) => boolean) | undefined
  • Default: undefined

The test function of the asset to be retried. For example:

  test: /cdn\.example\.com/,


  • Type: undefined | boolean | 'anonymous' | 'use-credentials'
  • Default: same as html.crossorigin

When initiating a retry for assets, Rsbuild will recreate the <script> tags. This option allows you to set the crossorigin attribute for these tags.

By default, the value of crossOrigin will be consistent with the html.crossorigin configuration, so no additional configuration is required. If you need to configure the recreated tags separately, you can use this option, for example:

  crossOrigin: true,


  • Type: undefined | (options: AssetsRetryHookContext) => void

The callback function when the asset is being retried. For example:

  onRetry: ({ times, domain, url, tagName }) => {
      `Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}`,


  • Type: undefined | (options: AssetsRetryHookContext) => void

The callback function when the asset is successfully retried. For example:

  onSuccess: ({ times, domain, url, tagName }) => {
      `Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}`,


  • Type: undefined | (options: AssetsRetryHookContext) => void

The callback function when the asset is failed to be retried. For example:

  onFail: ({ times, domain, url, tagName }) => {
      `Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}`,


  • Type: boolean
  • Default: true

Whether to inline the runtime JavaScript code of Assets Retry plugin into the HTML file.

If you don't want to insert the code in the HTML file, you can set inlineScript to false:

  inlineScript: false,

After adding the above configuration, the runtime code of Assets Retry plugin will be extracted into a separate assets-retry.[version].js file and output to the dist directory.

The downside is that assets-retry.[version].js itself may fail to load. If this happens, the assets retry will not work. Therefore, we prefer to inline the runtime code into the HTML file.


When you use Assets Retry plugin, the Rsbuild injects some runtime code into the HTML and serializes the Assets Retry plugin config, inserting it into the runtime code. Therefore, you need to be aware of the following:

  • Avoid configuring sensitive information in Assets Retry plugin, such as internal tokens.
  • Avoid referencing variables or methods outside of onRetry, onSuccess, and onFail.
  • Avoid using syntax with compatibility issues in onRetry, onSuccess and onFail as these functions are inlined directly into the HTML.

Here's an example of incorrect usage:

import { someMethod } from 'utils';

  onRetry() {
    // Incorrect usage, includes sensitive information
    const privateToken = 'a-private-token';

    // Incorrect usage, uses an external method


Assets Retry plugin may not work in the following scenarios:


If your project is a micro-frontend application (such as a Garfish sub-application), the assets retry may not work because micro-frontend sub-applications are typically not loaded directly based on the <script> tag.

If you need to retry assets in micro-frontend scenarios, please contact the developers of the micro-frontend framework to find a solution.

Assets in custom templates

Assets Retry plugin listens to the page error event to know whether the current resource fails to load and needs to be retried. Therefore, if the resource in the custom template is executed earlier than Assets Retry plugin, then Assets Retry plugin cannot listen to the event that the resource fails to load, so it will not be retried.

If you want Assets Retry plugin to work on resources in custom templates, you can refer to Custom Insertion Example to modify html.inject configuration and custom template.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>custom template</title>
+   <%= htmlWebpackPlugin.tags.headTags %>
    <script src="//"></script>
    <div id="root" />
+    <%= htmlWebpackPlugin.tags.bodyTags %>