retryableImport.tsx 510 B

123456789101112131415161718192021222324
  1. import {isWebpackChunkLoadingError} from 'sentry/utils';
  2. const MAX_RETRIES = 2;
  3. export default async function retryableImport<T>(
  4. fn: () => Promise<{default: T}>
  5. ): Promise<T> {
  6. let retries = 0;
  7. const tryLoad = async () => {
  8. try {
  9. const module = await fn();
  10. return module.default ?? module;
  11. } catch (err) {
  12. if (isWebpackChunkLoadingError(err) && retries < MAX_RETRIES) {
  13. retries++;
  14. return tryLoad();
  15. }
  16. throw err;
  17. }
  18. };
  19. return tryLoad();
  20. }