Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 360x 360x 360x 360x 360x 360x 360x 360x | import { ComponentType, LazyExoticComponent, lazy } from "react";
const pageForceRefreshedKey = "page-has-been-force-refreshed";
// https://raphael-leger.medium.com/react-webpack-chunkloaderror-loading-chunk-x-failed-ac385bd110e0
export const lazyWithRetry = (
importComponent: () => Promise<{ default: ComponentType }>,
): LazyExoticComponent<ComponentType> =>
lazy(async () => {
const pageForceRefreshed =
localStorage.getItem(pageForceRefreshedKey) === "true";
try {
const component = await importComponent();
localStorage.setItem(pageForceRefreshedKey, "false");
return component;
} catch (error) {
if (!pageForceRefreshed) {
localStorage.setItem(pageForceRefreshedKey, "true");
location.reload();
// Return a never-resolving promise to prevent the error from
// reaching Sentry while the page reloads
// eslint-disable-next-line @typescript-eslint/no-empty-function
return new Promise(() => {});
}
// eslint-disable-next-line no-restricted-syntax -- Okay to throw if module loading fails after page reload
throw error;
}
});
|