2021-07-07 09:10:28 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2021-07-22 16:39:10 +02:00
|
|
|
let localStorageAvailable;
|
|
|
|
try {
|
|
|
|
localStorageAvailable = Boolean(window.localStorage);
|
|
|
|
} catch (e) {
|
|
|
|
localStorageAvailable = false;
|
|
|
|
}
|
|
|
|
|
2021-07-07 09:10:28 +02:00
|
|
|
export const lazyImport = (componentImport) =>
|
|
|
|
React.lazy(async () => {
|
2021-07-22 16:39:10 +02:00
|
|
|
const pageHasAlreadyBeenForceRefreshed = localStorageAvailable
|
|
|
|
? JSON.parse(window.localStorage.getItem('page-has-been-force-refreshed') || 'false')
|
|
|
|
: false;
|
2021-07-07 09:10:28 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const component = await componentImport();
|
2021-07-22 16:39:10 +02:00
|
|
|
if (localStorageAvailable) {
|
|
|
|
window.localStorage.setItem('page-has-been-force-refreshed', 'false');
|
|
|
|
}
|
2021-07-07 09:10:28 +02:00
|
|
|
return component;
|
|
|
|
} catch (error) {
|
|
|
|
if (!pageHasAlreadyBeenForceRefreshed) {
|
|
|
|
// It's highly likely that the user's session is old. Try reloading once.
|
2021-07-22 16:39:10 +02:00
|
|
|
if (localStorageAvailable) {
|
|
|
|
window.localStorage.setItem('page-has-been-force-refreshed', 'true');
|
|
|
|
}
|
2021-07-07 09:10:28 +02:00
|
|
|
return window.location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it still didn't work, then relay the error.
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|