Retry Plugin
Retry Plugin
The Retry Plugin provides a robust retry mechanism for Module Federation. When remote modules or resources fail to load, it retries automatically to keep your app stable.
Features
- Automatic retries: Improve stability by retrying failed resource loads
- Domain rotation: Switch across multiple backup domains automatically
- Cache-busting: Add query parameters to avoid cache interference on retries
- Flexible config: Customize retry times, delay, and callbacks
Install
Migration Guide
From v0.18.x to v0.19.x
The plugin configuration has been simplified. The old fetch and script configuration objects are deprecated:
Usage
Method1: Used in the build plugin
Method2: Used in the pure runtime
Configuration
Basic
retryTimes
- Type:
number - Optional
- Number of retries, default is 3
retryDelay
- Type:
number - Optional
- Delay between retries in milliseconds, default is 1000
Advanced
domains
- Type:
string[] - Optional
- Backup domains for rotation. Default is an empty array
addQuery
- Type:
boolean | ((context: { times: number; originalQuery: string }) => string) - Optional
- Whether to append a query parameter when retrying, default is false
- If a function is provided, it receives retry count and the original query string, and should return the new query string
fetchOptions
- Type:
RequestInit - Optional
- Custom fetch options, default is an empty object
manifestDomains
- Type:
string[] - Optional
- Domain rotation list used when fetching the manifest (e.g.
mf-manifest.json). Takes precedence overdomainsfor manifest fetch retries. Other resources still usedomains.
Callbacks
onRetry
- Type:
({ times, domains, url, tagName }: { times?: number; domains?: string[]; url?: string; tagName?: string }) => void - Optional
- Triggered on each retry
- Params:
timescurrent retry number,domainsdomain list,urlrequest URL,tagNameresource type
onSuccess
- Type:
({ domains, url, tagName }: { domains?: string[]; url?: string; tagName?: string; }) => void - Optional
- Triggered when a retry finally succeeds
- Params:
domainsdomain list,urlrequest URL,tagNameresource type
onError
- Type:
({ domains, url, tagName }: { domains?: string[]; url?: string; tagName?: string; }) => void - Optional
- Triggered when all retries fail
- Params:
domainsdomain list,urlrequest URL,tagNameresource type
Details
Retry logic
The plugin retries automatically when a resource fails to load. The number of retries is controlled by retryTimes. For example:
retryTimes: 3means up to 3 retries (after the first attempt)- A delay of
retryDelayms is applied before each retry
Domain rotation
When domains is configured, the plugin rotates the host on each retry:
Order of attempts:
- Initial attempt: original URL
- 1st retry: switch to
cdn2.example.com - 2nd retry: switch to
cdn3.example.com - 3rd retry: switch to
cdn1.example.com
Cache-busting
Use addQuery to add query parameters during retries to avoid cache interference:
You can also provide a function:
Callbacks
You can monitor the retry lifecycle with callbacks:
Callback params:
times: current retry count (starts from 1)domains: the current domain listurl: current request URLtagName: resource type ('fetch' or 'script')
Use cases
1. CDN failover
2. Unstable networks
3. Monitoring and logging
Notes
- Performance: High retry counts increase loading time; tune for your environment
- Domains: Ensure all domains in
domainsserve the same resource - Caching: If
addQueryis enabled, consider CDN caching strategy - Error handling: After all retries fail, the original error is thrown; handle it upstream
Error codes
RUNTIME_008: Resource load failure that triggers the retry mechanism