67324aca21
https://raphael-leger.medium.com/react-webpack-chunkloaderror-loading-chunk-x-failed-ac385bd110e0 #codesplit
23 lines
771 B
JavaScript
23 lines
771 B
JavaScript
import React from 'react';
|
|
|
|
export const lazyImport = (componentImport) =>
|
|
React.lazy(async () => {
|
|
const pageHasAlreadyBeenForceRefreshed = JSON.parse(
|
|
window.localStorage.getItem('page-has-been-force-refreshed') || 'false'
|
|
);
|
|
|
|
try {
|
|
const component = await componentImport();
|
|
window.localStorage.setItem('page-has-been-force-refreshed', 'false');
|
|
return component;
|
|
} catch (error) {
|
|
if (!pageHasAlreadyBeenForceRefreshed) {
|
|
// It's highly likely that the user's session is old. Try reloading once.
|
|
window.localStorage.setItem('page-has-been-force-refreshed', 'true');
|
|
return window.location.reload();
|
|
}
|
|
|
|
// If it still didn't work, then relay the error.
|
|
throw error;
|
|
}
|
|
});
|