Fix broken embeds in incognito
## 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...
This commit is contained in:
parent
f4559c1cce
commit
ecb14be871
1 changed files with 16 additions and 5 deletions
|
@ -1,19 +1,30 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
|
let localStorageAvailable;
|
||||||
|
try {
|
||||||
|
localStorageAvailable = Boolean(window.localStorage);
|
||||||
|
} catch (e) {
|
||||||
|
localStorageAvailable = false;
|
||||||
|
}
|
||||||
|
|
||||||
export const lazyImport = (componentImport) =>
|
export const lazyImport = (componentImport) =>
|
||||||
React.lazy(async () => {
|
React.lazy(async () => {
|
||||||
const pageHasAlreadyBeenForceRefreshed = JSON.parse(
|
const pageHasAlreadyBeenForceRefreshed = localStorageAvailable
|
||||||
window.localStorage.getItem('page-has-been-force-refreshed') || 'false'
|
? JSON.parse(window.localStorage.getItem('page-has-been-force-refreshed') || 'false')
|
||||||
);
|
: false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const component = await componentImport();
|
const component = await componentImport();
|
||||||
window.localStorage.setItem('page-has-been-force-refreshed', 'false');
|
if (localStorageAvailable) {
|
||||||
|
window.localStorage.setItem('page-has-been-force-refreshed', 'false');
|
||||||
|
}
|
||||||
return component;
|
return component;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!pageHasAlreadyBeenForceRefreshed) {
|
if (!pageHasAlreadyBeenForceRefreshed) {
|
||||||
// It's highly likely that the user's session is old. Try reloading once.
|
// It's highly likely that the user's session is old. Try reloading once.
|
||||||
window.localStorage.setItem('page-has-been-force-refreshed', 'true');
|
if (localStorageAvailable) {
|
||||||
|
window.localStorage.setItem('page-has-been-force-refreshed', 'true');
|
||||||
|
}
|
||||||
return window.location.reload();
|
return window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue