lbry-desktop/ui/util/lazyImport.js
infinite-persistence 247ee757d1
ChunkLoadError: ask user to reload instead of automatically reloading (#139)
## Issue
We previously automatically reload when there is a chunk error. This works fine if it's the case of new code was pushed recently while the user was active. But if the failure was caused by other things like network problems or the file IS actually missing, we end up in an infinite loop of refreshes.

## New approach
Tell the user to reload instead of automatically doing it.
2021-10-27 11:07:06 -04:00

26 lines
835 B
JavaScript

import React from 'react';
import * as ACTIONS from 'constants/action_types';
const RETRY_DELAY_MS = 2000;
const RETRY_ATTEMPTS = 2;
function componentLoader(lazyComponent, attemptsLeft) {
return new Promise((resolve, reject) => {
lazyComponent()
.then(resolve)
.catch((error) => {
setTimeout(() => {
if (attemptsLeft === 1) {
window.store.dispatch({ type: ACTIONS.RELOAD_REQUIRED });
console.error(error.message); // Spew the error so users can report to us if reloading doesn't help.
} else {
componentLoader(lazyComponent, attemptsLeft - 1).then(resolve, reject);
}
}, RETRY_DELAY_MS);
});
});
}
export function lazyImport(componentImport) {
return React.lazy(() => componentLoader(componentImport, RETRY_ATTEMPTS));
}