question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:21
  • Comments:33 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
theKasheycommented, Dec 7, 2020

Good point. As a matter of fact - our own Sentry is filled with the same reports.

5reactions
ValentinHcommented, Feb 9, 2021

I think this issue is still relevant. My last question was not answered actually.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found