Skip to content

Commit ec31539

Browse files
authored
fix(retry-plugin): error-boundary should render until script retry finished (#3127)
1 parent 662a820 commit ec31539

File tree

14 files changed

+225
-405
lines changed

14 files changed

+225
-405
lines changed

.changeset/blue-poets-play.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/retry-plugin': patch
3+
'@module-federation/runtime': patch
4+
---
5+
6+
fix(retry-plugin): fix script retry logic to make error-boundary should render until script retry finished.

apps/router-demo/router-host-2000/src/App.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ init({
1818
fallback: () => 'http://localhost:2001/mf-manifest.json',
1919
},
2020
script: {
21-
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
22-
customCreateScript: (url: string, attrs: Record<string, string>) => {
23-
let script = document.createElement('script');
24-
script.src = `http://localhost:2011/static/js/async/src_App_tsx.js`;
25-
script.setAttribute('loader-hoos', 'isTrue');
26-
script.setAttribute('crossorigin', 'anonymous');
27-
script.onload = (event) => {
28-
console.log('--------custom script onload--------', event);
29-
};
30-
script.onerror = (event) => {
31-
console.log('--------custom script onerror--------', event);
32-
};
33-
return script;
21+
retryTimes: 3,
22+
retryDelay: 1000,
23+
moduleName: ['remote1'],
24+
cb: (resolve, error) => {
25+
return setTimeout(() => {
26+
resolve(error);
27+
}, 1000);
3428
},
3529
},
3630
}),

apps/router-demo/router-host-2000/src/runtime-plugin/retry.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { RetryPlugin } from '@module-federation/retry-plugin';
33
const retryPlugin = () =>
44
RetryPlugin({
55
fetch: {
6-
url: 'http://localhost:2008/not-exist-mf-manifest.json',
6+
url: 'http://localhost:2001/mf-manifest.json',
77
fallback: () => 'http://localhost:2001/mf-manifest.json',
88
},
99
script: {
10-
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
11-
customCreateScript: (url: string, attrs: Record<string, string>) => {
12-
let script = document.createElement('script');
13-
script.src = `http://localhost:2011/static/js/async/src_App_tsx.js`;
14-
script.setAttribute('loader-hoos', 'isTrue');
15-
script.setAttribute('crossorigin', 'anonymous');
16-
return script;
10+
retryTimes: 3,
11+
retryDelay: 1000,
12+
moduleName: ['remote1'],
13+
cb: (resolve, error) => {
14+
return setTimeout(() => {
15+
resolve(error);
16+
}, 1000);
1717
},
1818
},
1919
});
Lines changed: 88 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { PackageManagerTabs, Tabs, Tab } from '@theme';
22

3-
# RetryPlugin
3+
# Resource Retry Plugin
44

5-
## Introduction
6-
7-
Resource retry plugin is used to retry resources when they fail to load, thereby increasing the success rate of resource loading. In Module Federation, we provide the retry plugin `RetryPlugin` to increase the retry mechanism, which can improve the stability of the application.
5+
resource retry plugin is used to retry resources when they fail to load, thereby increasing the success rate of resource loading. In Module Federation, we provide the retry plugin `RetryPlugin` to increase the retry mechanism for remote applications, which can improve the stability of the application.
86

97
## Installation
10-
11-
This `RetryPlugin` is Provided by the `@module-federation/retry-plugin` package
8+
resource retry plugin is provided by the `@module-federation/retry-plugin` package. Let's install it first.
129

1310
<PackageManagerTabs
1411
command={{
@@ -21,16 +18,19 @@ This `RetryPlugin` is Provided by the `@module-federation/retry-plugin` package
2118

2219
## Usage
2320

24-
`RetryPlugin` is a runtime plugin, we can register this plugin through the `runtimePlugin` of the build plugin or register it at runtime and configure retry parameters and retry logic, etc.:
25-
21+
`RetryPlugin` is a runtime plugin, and we can register this plugin through the `runtimePlugin` of the build plugin or register it at runtime and configure retry parameters and retry logic, etc.:
2622

2723
:::note
28-
Note: Choose either of the two ways to register the build plugin and do not register it repeatedly
24+
25+
Attention
26+
27+
- build plugin registration and runtime registration are optional. You can choose either one, but not both.
28+
- It is recommended to register the RetryPlugin as early as possible in the build plugin to avoid the failure of resource requests, which may cause the project to fail to start and cannot enter the runtime to register the retry plugin, thus failing to retry resources.
2929

3030
:::
3131

3232

33-
### Method1: Register in the build plugin
33+
### The first way: register in the build plugin
3434

3535
```diff
3636
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
@@ -47,67 +47,39 @@ export default defineConfig({
4747
}),
4848
],
4949
});
50+
5051
```
5152

53+
5254
```ts
5355
// ./src/runtime-plugin/retry.ts
5456
import { RetryPlugin } from '@module-federation/retry-plugin';
5557
const retryPlugin = () => RetryPlugin({
56-
fetch: {
57-
url: 'http://localhost:2008/not-exist-mf-manifest.json',
58-
fallback: () => 'http://localhost:2001/mf-manifest.json',
59-
},
60-
script: {
61-
// url: 'http://localhost:2008/not-exist-mf-manifest.json',
62-
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
63-
customCreateScript: (url: string, attrs: Record<string, string>) => {
64-
let script = document.createElement('script');
65-
script.src = `http://localhost:2011/static/js/async/src_App_tsx.js`;
66-
script.setAttribute('loader-hoos', 'isTrue');
67-
script.setAttribute('crossorigin', 'anonymous');
68-
return script;
69-
},
70-
}
58+
fetch: {},
59+
script: {}
7160
})
7261
export default retryPlugin;
7362
```
7463

75-
### Method2: Register it at runtime
64+
### The Second way: register at runtime
7665

7766
```ts
78-
import {
79-
RetryPlugin,
80-
} from '@module-federation/retry-plugin';
67+
import { init, loadRemote } from '@module-federation/enhanced/runtime';
68+
import { RetryPlugin } from '@module-federation/retry-plugin';
8169

8270
init({
8371
name: 'federation_consumer',
8472
remotes: [],
8573
plugins: [
8674
RetryPlugin({
87-
fetch: {
88-
// the retry resource url
89-
url: 'http://localhost:2008/not-exist-mf-manifest.json',
90-
// after all retried failed, set a fallback function to guarantee a fallback resource
91-
fallback: () => 'http://localhost:2001/mf-manifest.json',
92-
},
93-
script: {
94-
// the retry resource url
95-
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
96-
// if you need to custom create script element, pass `customCreateScript` and plugin will use customCreateScript to create script element
97-
customCreateScript: (url: string, attrs: Record<string, string>) => {
98-
let script = document.createElement('script');
99-
script.src = `http://localhost:2011/static/js/async/src_App_tsx.js`;
100-
script.setAttribute('loader-hoos', 'isTrue');
101-
script.setAttribute('crossorigin', 'anonymous');
102-
return script;
103-
},
104-
},
75+
fetch: {},
76+
script: {},
10577
}),
10678
],
10779
});
10880
```
10981

110-
the resource that needs to be retried is divided into two types: the `fetch` type and the `script` type, and we control the retry logic of these two resource types through the `fetch` parameter and the `script` parameter, respectively.
82+
the resources that need to be retried are divided into two types: fetch type and script type. We control the retry logic of these two resource types respectively through the `fetch` parameter and the `script` parameter.
11183

11284
## Type
11385

@@ -128,87 +100,107 @@ type FetchWithRetryOptions = {
128100
}
129101

130102
type ScriptWithRetryOptions = {
131-
url?: string;
132-
attrs?: Record<string, string>;
133103
retryTimes?: number;
134-
customCreateScript?: CreateScriptFunc;
104+
retryDelay?: number;
105+
moduleName?: Array<string>;
106+
cb?: (resolve: (value: unknown) => void, error: any) => void;
135107
}
136108

137109
```
138110

111+
## RetryPluginParams Type Description
139112

140-
## RetryPluginParams type description
141-
142-
`RetryPluginParams` is the parameter type used to configure `RetryPlugin`, which contains retry options for two types of resources: `fetch` and `script`.
113+
`RetryPluginParams` is the parameter type used to configure the `RetryPlugin`, which contains retry options for two types of resources: `fetch` and `script`.
143114

144115
### Properties
145116

146-
- **fetch**: `FetchWithRetryOptions` (optional)
147-
- Used to configure the retry options for fetch type resources.
148-
149117
- **script**: `ScriptWithRetryOptions` (optional)
150-
- Used to configure the retry options for script type resources.
118+
- used to configure the retry options for fetch type resources.
151119

120+
- **script**: `ScriptWithRetryOptions` (optional)
121+
- `ScriptWithRetryOptions` is the type used to configure the retry options for script type resources.
152122

153-
## FetchWithRetryOptions type description
154-
155-
`FetchWithRetryOptions` is the type used to configure the retry options for fetch type resources.
156-
157-
### 属性
158-
### Properties
123+
### FetchWithRetryOptions Type Description
159124

160125
- **url**:
161126
- `string`
162-
- Optional. When not set, all failed resources will default to retry logic.
163-
- The URL of the resource to be retried.
127+
- optional, when not set, all failed URLs will enter the retry logic by default
128+
- The URL of the resource that needs to be retried.
164129

165130
- **options**:
166131
- `RequestInit`
167-
- Optional.
168-
- The request options for the fetch request.
132+
- optional
133+
- Options passed to the fetch request.
169134

170135
- **retryTimes**:
171136
- `number`
172-
- Optional.
173-
- The number of retry attempts. Defaults to 3.
137+
- Options
138+
- The number of retries, default is 3.
174139

175140
- **retryDelay**:
176141
- `number`
177-
- Optional.
178-
- The delay between retry attempts in milliseconds. Defaults to 1000.
142+
- optional
143+
- The delay time between each retry (in milliseconds).
179144

180145
- **fallback**:
181146
- `() => string`
182-
- Optional. A function that returns a Promise that resolves to a Response object. This function is called when all retry attempts have failed.
183-
147+
- optional
148+
- Returns the URL of a backup resource after all retries fail.
184149

185-
### ScriptWithRetryOptions Type Description
150+
### ScriptWithRetryOptions 类型说明
186151

187-
`ScriptWithRetryOptions` is the type used to configure the retry options for script type resources.
152+
- **retryTimes**:
153+
- `number`
154+
- optional
155+
- The number of retries, default is 3.
188156

189-
### Properties
157+
- **retryDelay**:
158+
- `number`
159+
- optional
160+
- The delay time between each retry (in milliseconds).
190161

191-
- **url**:
162+
- **moduleName**:
192163
- `string`
193-
- Optional. When not set, all failed resources will default to retry logic.
194-
- The URL of the resource to be retried.
164+
- optional
165+
- The module name, used to identify the module that needs to be retried.
195166

196-
- **attrs**:
197-
- `Record<string, string>`
198-
- Optional
199-
- The attributes to be passed to the script element.
167+
- **cb**:
168+
- `(resolve: (value: unknown) => void, error: any) => void`
169+
- optional
170+
- Callback function, callback after retry failure
200171

201-
- **retryTimes**:
202-
- `number`
203-
- Optional
204-
- The number of retry attempts. Defaults to 3.
172+
the complete configuration can be referred to as follows:
205173

206-
- **retryDelay**:
207-
- `number`
208-
- Optional
209-
- The delay between retry attempts in milliseconds. Defaults to 1000.
174+
```ts
175+
import { init, loadRemote } from '@module-federation/enhanced/runtime';
176+
import { RetryPlugin } from '@module-federation/retry-plugin';
210177

211-
- **customCreateScript**:
212-
- `CreateScriptFunc`
213-
- Optional.
214-
- A custom function to create the script element.
178+
init({
179+
name: 'federation_consumer',
180+
remotes: [],
181+
plugins: [
182+
RetryPlugin({
183+
fetch: {
184+
// the retry resource url
185+
url: 'http://localhost:2001/-mf-manifest.json',
186+
// after all retried failed, set a fallback function to guarantee a fallback resource
187+
fallback: () => 'http://localhost:2002/mf-manifest.json',
188+
},
189+
script: {
190+
// the retry times
191+
retryTimes: 3,
192+
// the retry delay
193+
retryDelay: 1000,
194+
// the module name list that need to be retried, defualt behavior is to retry all modules
195+
moduleName: ['remote1'],
196+
// the callback function that will be called after all retried failed
197+
cb: (resolve, error) => {
198+
return setTimeout(() => {
199+
resolve(error);
200+
}, 2000);
201+
},
202+
},
203+
}),
204+
],
205+
});
206+
```

0 commit comments

Comments
 (0)