Implement retry in case of error
See original GitHub issue🚀 Feature Proposal
Implement a new “retry” option in @loadable/component
.
Motivation
We monitor our errors with Sentry and we see quite a lot of errors coming with lazy-loaded components. Indeed, on flaky network, it’s easy to have a request aborted and the component is never loaded. In our applications, we have started to wrap all dynamic imports passed to loadable with a retry function such as:
const Example = loadable(() =>
retry(
() => import('./Example'),
{ retries: 3 }
)
)
where retry is a simple function:
type Options = {
retries?: number
interval?: number
exponentialBackoff?: boolean
}
// taken from https://dev.to/goenning/how-to-retry-when-react-lazy-fails-mb5
export function retry<R>(
fn: () => Promise<R>,
{ retries = 3, interval = 500, exponentialBackoff = true }: Options = {}
) {
return new Promise<R>((resolve, reject) => {
fn()
.then(resolve)
.catch((error) => {
setTimeout(() => {
if (retries === 1) {
reject(error)
return
}
// Passing on "reject" is the important part
retry(fn, {
retries: retries - 1,
interval: exponentialBackoff ? interval * 2 : interval,
}).then(resolve, reject)
}, interval)
})
})
}
Example
const Example = loadable(() => import('./Example'), {retries: 3})
Pitch
I’m wondering if this is something that could be directly implemented as part of loadable
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:21
- Comments:33 (1 by maintainers)
Top Results From Across the Web
How to Implement Retry Logic in C# - Code Maze
In this article, we are going to learn how to implement retry logic in C# to help us handle problems that we can...
Read more >Best Practices for Retry - Denali Balser
A retry is a mechanism that monitors a request, and on the detection of failure automatically fires a repeat of the request.
Read more >How do you implement a re-try-catch? - Stack Overflow
Here's one way to implement these: ... If you want to retry only certain number of times then use a counter ... Let's...
Read more >Retry Pattern: examples & recommendations - Apiumhub
Retry: If the error indicates that it is a temporary failure or an atypical failure, the application can retry the same operation immediately ......
Read more >Transient fault handling - Azure - Microsoft Learn
It is vital to optimize the retry count and the interval to the type of use case. If you do not retry a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Good point. As a matter of fact - our own Sentry is filled with the same reports.
I think this issue is still relevant. My last question was not answered actually.