ecb14be871
## Issue Closes 6490 Embeds in https://lbry.com/news/2002dtf was crashing in incognito ## Change Ensure localStorage is available before using it. Jessop warned me before...
34 lines
1 KiB
JavaScript
34 lines
1 KiB
JavaScript
import React from 'react';
|
|
|
|
let localStorageAvailable;
|
|
try {
|
|
localStorageAvailable = Boolean(window.localStorage);
|
|
} catch (e) {
|
|
localStorageAvailable = false;
|
|
}
|
|
|
|
export const lazyImport = (componentImport) =>
|
|
React.lazy(async () => {
|
|
const pageHasAlreadyBeenForceRefreshed = localStorageAvailable
|
|
? JSON.parse(window.localStorage.getItem('page-has-been-force-refreshed') || 'false')
|
|
: false;
|
|
|
|
try {
|
|
const component = await componentImport();
|
|
if (localStorageAvailable) {
|
|
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.
|
|
if (localStorageAvailable) {
|
|
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;
|
|
}
|
|
});
|