From ecb14be8712adfc91a561de9622bda2e51d8fd2d Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Thu, 22 Jul 2021 22:39:10 +0800 Subject: [PATCH] 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... --- ui/util/lazyImport.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ui/util/lazyImport.js b/ui/util/lazyImport.js index d5f05f60a..da3e64b04 100644 --- a/ui/util/lazyImport.js +++ b/ui/util/lazyImport.js @@ -1,19 +1,30 @@ import React from 'react'; +let localStorageAvailable; +try { + localStorageAvailable = Boolean(window.localStorage); +} catch (e) { + localStorageAvailable = false; +} + export const lazyImport = (componentImport) => React.lazy(async () => { - const pageHasAlreadyBeenForceRefreshed = JSON.parse( - window.localStorage.getItem('page-has-been-force-refreshed') || 'false' - ); + const pageHasAlreadyBeenForceRefreshed = localStorageAvailable + ? JSON.parse(window.localStorage.getItem('page-has-been-force-refreshed') || 'false') + : false; try { 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; } 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'); + if (localStorageAvailable) { + window.localStorage.setItem('page-has-been-force-refreshed', 'true'); + } return window.location.reload(); }