@module-federation/retry-plugin 0.18.4 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +329 -34
- package/dist/CHANGELOG.md +11 -0
- package/dist/README.md +329 -34
- package/dist/esm/index.js +285 -97
- package/dist/index.d.mts +101 -19
- package/dist/index.d.ts +101 -19
- package/dist/index.js +290 -98
- package/dist/package.json +2 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,52 +1,347 @@
|
|
|
1
1
|
# @module-federation/retry-plugin
|
|
2
2
|
|
|
3
|
-
> A plugin for
|
|
3
|
+
> A robust retry plugin for Module Federation that provides automatic retry mechanisms for failed module requests with domain rotation, cache-busting, and comprehensive error handling.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
- 🔄 **Automatic Retry**: Automatically retries failed fetch and script requests
|
|
8
|
+
- 🌐 **Domain Rotation**: Rotate through multiple domains for better reliability
|
|
9
|
+
- ⚡ **Cache Busting**: Add query parameters to bypass cache issues
|
|
10
|
+
- 📊 **Lifecycle Callbacks**: Comprehensive callbacks for retry events
|
|
11
|
+
- 🎯 **Flexible Configuration**: Highly configurable retry strategies
|
|
12
|
+
- 🔧 **TypeScript Support**: Full TypeScript support with type definitions
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @module-federation/retry-plugin
|
|
18
|
+
# or
|
|
19
|
+
yarn add @module-federation/retry-plugin
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @module-federation/retry-plugin
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Basic Usage
|
|
25
|
+
|
|
26
|
+
### Runtime Plugin Registration
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { createInstance } from '@module-federation/enhanced/runtime';
|
|
9
30
|
import { RetryPlugin } from '@module-federation/retry-plugin';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
31
|
+
|
|
32
|
+
const mf = createInstance({
|
|
33
|
+
name: 'host',
|
|
34
|
+
remotes: [
|
|
35
|
+
{
|
|
36
|
+
name: 'remote1',
|
|
37
|
+
entry: 'http://localhost:2001/mf-manifest.json',
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
plugins: [
|
|
41
|
+
RetryPlugin({
|
|
42
|
+
retryTimes: 3,
|
|
43
|
+
retryDelay: 1000,
|
|
44
|
+
domains: ['https://cdn1.example.com', 'https://cdn2.example.com'],
|
|
45
|
+
manifestDomains: ['https://domain1.example.com', 'https://domain2.example.com'],
|
|
46
|
+
addQuery: ({ times, originalQuery }) => `${originalQuery}&retry=${times}`,
|
|
47
|
+
onRetry: ({ times, url }) => console.log('Retrying...', times, url),
|
|
48
|
+
onSuccess: ({ url }) => console.log('Success!', url),
|
|
49
|
+
onError: ({ url }) => console.log('Failed!', url),
|
|
50
|
+
}),
|
|
51
|
+
]
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Build Plugin Registration
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// webpack.config.js
|
|
59
|
+
import { ModuleFederationPlugin } from '@module-federation/webpack';
|
|
60
|
+
import { RetryPlugin } from '@module-federation/retry-plugin';
|
|
61
|
+
|
|
62
|
+
export default {
|
|
63
|
+
plugins: [
|
|
64
|
+
new ModuleFederationPlugin({
|
|
65
|
+
name: 'host',
|
|
66
|
+
remotes: {
|
|
67
|
+
remote1: 'remote1@http://localhost:2001/mf-manifest.json',
|
|
68
|
+
},
|
|
69
|
+
runtimePlugins: [
|
|
70
|
+
path.join(__dirname, './src/runtime-plugin/retry.ts'),
|
|
71
|
+
],
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
// src/runtime-plugin/retry.ts
|
|
79
|
+
import { RetryPlugin } from '@module-federation/retry-plugin';
|
|
80
|
+
|
|
81
|
+
export default () => RetryPlugin({
|
|
82
|
+
retryTimes: 3,
|
|
83
|
+
retryDelay: 1000,
|
|
84
|
+
domains: ['https://cdn1.example.com', 'https://cdn2.example.com'],
|
|
85
|
+
manifestDomains: ['https://domain1.example.com', 'https://domain2.example.com'],
|
|
86
|
+
addQuery: ({ times, originalQuery }) => `${originalQuery}&retry=${times}`,
|
|
87
|
+
onRetry: ({ times, url }) => console.log('Retrying...', times, url),
|
|
88
|
+
onSuccess: ({ url }) => console.log('Success!', url),
|
|
89
|
+
onError: ({ url }) => console.log('Failed!', url),
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Configuration Options
|
|
94
|
+
|
|
95
|
+
### CommonRetryOptions
|
|
96
|
+
|
|
97
|
+
| Option | Type | Default | Description |
|
|
98
|
+
|--------|------|---------|-------------|
|
|
99
|
+
| `retryTimes` | `number` | `3` | Number of retry attempts |
|
|
100
|
+
| `retryDelay` | `number` | `1000` | Delay between retries in milliseconds |
|
|
101
|
+
| `successTimes` | `number` | `0` | Number of successful requests required |
|
|
102
|
+
| `domains` | `string[]` | `[]` | Alternative domains for script resources |
|
|
103
|
+
| `manifestDomains` | `string[]` | `[]` | Alternative domains for manifest files |
|
|
104
|
+
| `addQuery` | `boolean \| function` | `false` | Add query parameters for cache busting |
|
|
105
|
+
| `fetchOptions` | `RequestInit` | `{}` | Additional fetch options |
|
|
106
|
+
| `onRetry` | `function` | `undefined` | Callback when retry occurs |
|
|
107
|
+
| `onSuccess` | `function` | `undefined` | Callback when request succeeds |
|
|
108
|
+
| `onError` | `function` | `undefined` | Callback when all retries fail |
|
|
109
|
+
|
|
110
|
+
### addQuery Function
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
addQuery: ({ times, originalQuery }) => {
|
|
114
|
+
// Add retry count and timestamp for cache busting
|
|
115
|
+
const separator = originalQuery ? '&' : '?';
|
|
116
|
+
return `${originalQuery}${separator}retry=${times}&t=${Date.now()}`;
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Callback Functions
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
onRetry: ({ times, domains, url, tagName }) => {
|
|
124
|
+
console.log(`Retry attempt ${times} for ${url}`);
|
|
125
|
+
console.log(`Available domains: ${domains?.join(', ')}`);
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
onSuccess: ({ domains, url, tagName }) => {
|
|
129
|
+
console.log(`Successfully loaded ${url}`);
|
|
130
|
+
console.log(`Used domain: ${domains?.[0]}`);
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
onError: ({ domains, url, tagName }) => {
|
|
134
|
+
console.error(`Failed to load ${url} after all retries`);
|
|
135
|
+
console.error(`Tried domains: ${domains?.join(', ')}`);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Advanced Examples
|
|
140
|
+
|
|
141
|
+
### Custom Retry Strategy
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
RetryPlugin({
|
|
145
|
+
retryTimes: 5,
|
|
146
|
+
retryDelay: (attempt) => Math.pow(2, attempt) * 1000, // Exponential backoff
|
|
147
|
+
domains: [
|
|
148
|
+
'https://cdn1.example.com',
|
|
149
|
+
'https://cdn2.example.com',
|
|
150
|
+
'https://cdn3.example.com',
|
|
151
|
+
],
|
|
152
|
+
manifestDomains: [
|
|
153
|
+
'https://api1.example.com',
|
|
154
|
+
'https://api2.example.com',
|
|
155
|
+
],
|
|
156
|
+
addQuery: ({ times, originalQuery }) => {
|
|
157
|
+
const params = new URLSearchParams(originalQuery);
|
|
158
|
+
params.set('retry', times.toString());
|
|
159
|
+
params.set('cache_bust', Date.now().toString());
|
|
160
|
+
return params.toString();
|
|
161
|
+
},
|
|
162
|
+
onRetry: ({ times, url, domains }) => {
|
|
163
|
+
console.log(`Retry ${times}/5 for ${url}`);
|
|
164
|
+
console.log(`Trying domain: ${domains?.[times % domains.length]}`);
|
|
165
|
+
},
|
|
166
|
+
onSuccess: ({ url, domains }) => {
|
|
167
|
+
console.log(`✅ Successfully loaded ${url}`);
|
|
168
|
+
console.log(`✅ Used domain: ${domains?.[0]}`);
|
|
169
|
+
},
|
|
170
|
+
onError: ({ url, domains }) => {
|
|
171
|
+
console.error(`❌ Failed to load ${url} after all retries`);
|
|
172
|
+
console.error(`❌ Tried all domains: ${domains?.join(', ')}`);
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Error Handling with Fallback
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
RetryPlugin({
|
|
181
|
+
retryTimes: 3,
|
|
182
|
+
retryDelay: 1000,
|
|
183
|
+
domains: ['https://cdn1.example.com', 'https://cdn2.example.com'],
|
|
184
|
+
onError: ({ url, domains }) => {
|
|
185
|
+
// Log error for monitoring
|
|
186
|
+
console.error('Module loading failed:', { url, domains });
|
|
187
|
+
|
|
188
|
+
// Send error to monitoring service
|
|
189
|
+
if (window.gtag) {
|
|
190
|
+
window.gtag('event', 'module_load_error', {
|
|
191
|
+
event_category: 'module_federation',
|
|
192
|
+
event_label: url,
|
|
193
|
+
value: domains?.length || 0,
|
|
194
|
+
});
|
|
25
195
|
}
|
|
196
|
+
|
|
197
|
+
// Show user-friendly error message
|
|
198
|
+
const errorElement = document.createElement('div');
|
|
199
|
+
errorElement.className = 'module-load-error';
|
|
200
|
+
errorElement.innerHTML = `
|
|
201
|
+
<div style="padding: 16px; border: 1px solid #ffa39e; border-radius: 4px; background: #fff1f0; color: #cf1322;">
|
|
202
|
+
<h4>Module Loading Failed</h4>
|
|
203
|
+
<p>Unable to load module: ${url}</p>
|
|
204
|
+
<p>Please refresh the page to try again.</p>
|
|
205
|
+
</div>
|
|
206
|
+
`;
|
|
207
|
+
document.body.appendChild(errorElement);
|
|
208
|
+
},
|
|
26
209
|
})
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Production Configuration
|
|
27
213
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
214
|
+
```ts
|
|
215
|
+
RetryPlugin({
|
|
216
|
+
retryTimes: 3,
|
|
217
|
+
retryDelay: 1000,
|
|
218
|
+
domains: [
|
|
219
|
+
'https://cdn1.prod.example.com',
|
|
220
|
+
'https://cdn2.prod.example.com',
|
|
221
|
+
'https://cdn3.prod.example.com',
|
|
222
|
+
],
|
|
223
|
+
manifestDomains: [
|
|
224
|
+
'https://api1.prod.example.com',
|
|
225
|
+
'https://api2.prod.example.com',
|
|
226
|
+
],
|
|
227
|
+
addQuery: ({ times, originalQuery }) => {
|
|
228
|
+
const params = new URLSearchParams(originalQuery);
|
|
229
|
+
params.set('retry', times.toString());
|
|
230
|
+
params.set('v', process.env.BUILD_VERSION || '1.0.0');
|
|
231
|
+
return params.toString();
|
|
232
|
+
},
|
|
233
|
+
fetchOptions: {
|
|
234
|
+
cache: 'no-cache',
|
|
235
|
+
headers: {
|
|
236
|
+
'X-Requested-With': 'ModuleFederation',
|
|
39
237
|
},
|
|
40
238
|
},
|
|
41
|
-
|
|
42
|
-
|
|
239
|
+
onRetry: ({ times, url }) => {
|
|
240
|
+
// Only log in development
|
|
241
|
+
if (process.env.NODE_ENV === 'development') {
|
|
242
|
+
console.log(`Retry ${times} for ${url}`);
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
onSuccess: ({ url }) => {
|
|
246
|
+
// Track successful loads
|
|
247
|
+
if (window.analytics) {
|
|
248
|
+
window.analytics.track('module_loaded', { url });
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
onError: ({ url, domains }) => {
|
|
252
|
+
// Send error to monitoring service
|
|
253
|
+
if (window.errorReporting) {
|
|
254
|
+
window.errorReporting.captureException(
|
|
255
|
+
new Error(`Module loading failed: ${url}`),
|
|
256
|
+
{ extra: { domains, url } }
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
})
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## How It Works
|
|
264
|
+
|
|
265
|
+
1. **Fetch Retry**: Intercepts failed fetch requests for manifest files and retries with domain rotation
|
|
266
|
+
2. **Script Retry**: Intercepts failed script loading and retries with alternative domains
|
|
267
|
+
3. **Domain Rotation**: Cycles through provided domains to find working alternatives
|
|
268
|
+
4. **Cache Busting**: Adds query parameters to prevent cache-related issues
|
|
269
|
+
5. **Lifecycle Hooks**: Provides callbacks for monitoring and debugging
|
|
270
|
+
|
|
271
|
+
## Error Scenarios Handled
|
|
43
272
|
|
|
273
|
+
- Network timeouts and connection errors
|
|
274
|
+
- DNS resolution failures
|
|
275
|
+
- Server errors (5xx status codes)
|
|
276
|
+
- CDN failures and regional issues
|
|
277
|
+
- Cache-related loading problems
|
|
278
|
+
- CORS and security policy violations
|
|
279
|
+
|
|
280
|
+
## Browser Support
|
|
281
|
+
|
|
282
|
+
- Chrome 60+
|
|
283
|
+
- Firefox 55+
|
|
284
|
+
- Safari 12+
|
|
285
|
+
- Edge 79+
|
|
286
|
+
|
|
287
|
+
## TypeScript Support
|
|
288
|
+
|
|
289
|
+
The plugin includes full TypeScript definitions:
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
import { RetryPlugin, type CommonRetryOptions } from '@module-federation/retry-plugin';
|
|
293
|
+
|
|
294
|
+
const options: CommonRetryOptions = {
|
|
295
|
+
retryTimes: 3,
|
|
296
|
+
retryDelay: 1000,
|
|
297
|
+
domains: ['https://cdn1.example.com'],
|
|
298
|
+
onRetry: ({ times, url }) => {
|
|
299
|
+
console.log(`Retry ${times} for ${url}`);
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const plugin = RetryPlugin(options);
|
|
44
304
|
```
|
|
45
305
|
|
|
46
|
-
##
|
|
306
|
+
## Migration Guide
|
|
47
307
|
|
|
48
|
-
|
|
308
|
+
### From v0.18.x to v0.19.x
|
|
309
|
+
|
|
310
|
+
The plugin configuration has been simplified. The old `fetch` and `script` configuration objects are deprecated:
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
// ❌ Old way (deprecated)
|
|
314
|
+
RetryPlugin({
|
|
315
|
+
fetch: {
|
|
316
|
+
url: 'http://localhost:2008/not-exist-mf-manifest.json',
|
|
317
|
+
fallback: () => 'http://localhost:2001/mf-manifest.json',
|
|
318
|
+
},
|
|
319
|
+
script: {
|
|
320
|
+
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
|
|
321
|
+
customCreateScript: (url, attrs) => { /* ... */ },
|
|
322
|
+
}
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
// ✅ New way
|
|
326
|
+
RetryPlugin({
|
|
327
|
+
retryTimes: 3,
|
|
328
|
+
retryDelay: 1000,
|
|
329
|
+
domains: ['http://localhost:2001'],
|
|
330
|
+
manifestDomains: ['http://localhost:2001'],
|
|
331
|
+
addQuery: ({ times, originalQuery }) => `${originalQuery}&retry=${times}`,
|
|
332
|
+
})
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## Contributing
|
|
336
|
+
|
|
337
|
+
Contributions are welcome! Please read our [contributing guidelines](https://github.com/module-federation/core/blob/main/CONTRIBUTING.md) and submit pull requests to our [GitHub repository](https://github.com/module-federation/core).
|
|
49
338
|
|
|
50
339
|
## License
|
|
51
340
|
|
|
52
341
|
`@module-federation/retry-plugin` is [MIT licensed](https://github.com/module-federation/core/blob/main/packages/retry-plugin/LICENSE).
|
|
342
|
+
|
|
343
|
+
## Related
|
|
344
|
+
|
|
345
|
+
- [Module Federation Documentation](https://module-federation.io/)
|
|
346
|
+
- [Module Federation Runtime](https://www.npmjs.com/package/@module-federation/runtime)
|
|
347
|
+
- [Module Federation Enhanced](https://www.npmjs.com/package/@module-federation/enhanced)
|
package/dist/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @module-federation/retry-plugin
|
|
2
2
|
|
|
3
|
+
## 0.19.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- eb69021: Refactor(retry-plugin): unify retry behavior and improve resilience
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 81d00e1: fix(retry-plugin): prevent query parameter accumulation across retries
|
|
12
|
+
- @module-federation/sdk@0.19.0
|
|
13
|
+
|
|
3
14
|
## 0.18.4
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|