From d2e732f89ff08768fcaebec0d38f68c93f0ad21f Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Thu, 20 Mar 2025 11:45:31 -0500 Subject: [PATCH 1/2] Add error handling for when localStorage is disabled --- .../error/menu/VersionInfo.react.js | 91 +++++++++++++------ 1 file changed, 63 insertions(+), 28 deletions(-) diff --git a/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js b/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js index 2aa3744e6a..04f16ae6d2 100644 --- a/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js +++ b/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js @@ -32,11 +32,17 @@ async function requestDashVersionInfo(config) { ddk_version: ddkVersion, plotly_version: plotlyVersion } = config; - const cachedVersionInfo = localStorage.getItem('cachedNewDashVersion'); - const cachedNewDashVersionLink = localStorage.getItem( - 'cachedNewDashVersionLink' - ); - const lastFetched = localStorage.getItem('lastFetched'); + let cachedVersionInfo, cachedNewDashVersionLink, lastFetched; + try { + cachedVersionInfo = localStorage.getItem('cachedNewDashVersion'); + cachedNewDashVersionLink = localStorage.getItem( + 'cachedNewDashVersionLink' + ); + lastFetched = localStorage.getItem('lastFetched'); + } catch (e) { + // If localStorage is not available, return an empty object + return {}; + } if ( lastFetched && Date.now() - Number(lastFetched) < DAY_IN_MS && @@ -57,12 +63,19 @@ async function requestDashVersionInfo(config) { .then(response => response.json()) .then(body => { if (body && body.version && body.link) { - localStorage.setItem( - 'cachedNewDashVersion', - JSON.stringify(body.version) - ); - localStorage.setItem('cachedNewDashVersionLink', body.link); - localStorage.setItem('lastFetched', Date.now()); + try { + localStorage.setItem( + 'cachedNewDashVersion', + JSON.stringify(body.version) + ); + localStorage.setItem( + 'cachedNewDashVersionLink', + body.link + ); + localStorage.setItem('lastFetched', Date.now()); + } catch (e) { + // Ignore errors if localStorage is not available + } return body; } else { return {}; @@ -75,12 +88,19 @@ async function requestDashVersionInfo(config) { } function shouldRequestDashVersion(config) { - const showNotificationsLocalStorage = - localStorage.getItem('showNotifications'); - const showNotifications = config.disable_version_check - ? false - : showNotificationsLocalStorage !== 'false'; - const lastFetched = localStorage.getItem('lastFetched'); + let showNotificationsLocalStorage, showNotifications, lastFetched; + try { + showNotificationsLocalStorage = + localStorage.getItem('showNotifications'); + + showNotifications = config.disable_version_check + ? false + : showNotificationsLocalStorage !== 'false'; + lastFetched = localStorage.getItem('lastFetched'); + } catch (e) { + // If localStorage is not available, return false + return false; + } return ( showNotifications && (!lastFetched || Date.now() - Number(lastFetched) > DAY_IN_MS) @@ -92,13 +112,19 @@ function shouldShowUpgradeNotification( newDashVersion, config ) { - const showNotificationsLocalStorage = - localStorage.getItem('showNotifications'); + let showNotificationsLocalStorage, lastDismissed, lastDismissedVersion; + try { + showNotificationsLocalStorage = + localStorage.getItem('showNotifications'); + lastDismissed = localStorage.getItem('lastDismissed'); + lastDismissedVersion = localStorage.getItem('lastDismissedVersion'); + } catch (e) { + // If localStorage is not available, return false + return false; + } const showNotifications = config.disable_version_check ? false : showNotificationsLocalStorage !== 'false'; - const lastDismissed = localStorage.getItem('lastDismissed'); - const lastDismissedVersion = localStorage.getItem('lastDismissedVersion'); if ( newDashVersion === undefined || compareVersions(currentDashVersion, newDashVersion) >= 0 || @@ -113,10 +139,7 @@ function shouldShowUpgradeNotification( } else if ( lastDismissedVersion && !lastDismissed && - compareVersions( - localStorage.getItem('lastDismissedVersion'), - newDashVersion - ) < 0 + compareVersions(lastDismissedVersion, newDashVersion) < 0 ) { return true; } else { @@ -131,19 +154,31 @@ export const VersionInfo = ({config}) => { const setDontShowAgain = () => { // Set local storage to record the last dismissed notification - localStorage.setItem('showNotifications', false); + try { + localStorage.setItem('showNotifications', false); + } catch (e) { + // Ignore errors if localStorage is not available + } setUpgradeTooltipOpened(false); }; const setRemindMeLater = () => { // Set local storage to record the last dismissed notification - localStorage.setItem('lastDismissed', Date.now()); + try { + localStorage.setItem('lastDismissed', Date.now()); + } catch (e) { + // Ignore errors if localStorage is not available + } setUpgradeTooltipOpened(false); }; const setSkipThisVersion = () => { // Set local storage to record the last dismissed version - localStorage.setItem('lastDismissedVersion', newDashVersion); + try { + localStorage.setItem('lastDismissedVersion', newDashVersion); + } catch (e) { + // Ignore errors if localStorage is not available + } setUpgradeTooltipOpened(false); }; From 551d96e07339c5eb34050a3512f689ba5fff775e Mon Sep 17 00:00:00 2001 From: Martha Cryan Date: Mon, 24 Mar 2025 13:52:00 -0500 Subject: [PATCH 2/2] Add logic to avoid checking localStorage when version check is disabled --- .../error/menu/VersionInfo.react.js | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js b/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js index 04f16ae6d2..eef0a2953e 100644 --- a/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js +++ b/dash/dash-renderer/src/components/error/menu/VersionInfo.react.js @@ -88,14 +88,15 @@ async function requestDashVersionInfo(config) { } function shouldRequestDashVersion(config) { - let showNotificationsLocalStorage, showNotifications, lastFetched; + // If version check is disabled, return false to avoid + // checking localStorage unnecessarily + if (config.disable_version_check) { + return false; + } + let showNotifications, lastFetched; try { - showNotificationsLocalStorage = - localStorage.getItem('showNotifications'); - - showNotifications = config.disable_version_check - ? false - : showNotificationsLocalStorage !== 'false'; + showNotifications = + localStorage.getItem('showNotifications') !== 'false'; lastFetched = localStorage.getItem('lastFetched'); } catch (e) { // If localStorage is not available, return false @@ -112,19 +113,21 @@ function shouldShowUpgradeNotification( newDashVersion, config ) { - let showNotificationsLocalStorage, lastDismissed, lastDismissedVersion; + // If version check is disabled, return false to avoid + // checking localStorage unnecessarily + if (config.disable_version_check) { + return false; + } + let showNotifications, lastDismissed, lastDismissedVersion; try { - showNotificationsLocalStorage = - localStorage.getItem('showNotifications'); + showNotifications = + localStorage.getItem('showNotifications') !== 'false'; lastDismissed = localStorage.getItem('lastDismissed'); lastDismissedVersion = localStorage.getItem('lastDismissedVersion'); } catch (e) { // If localStorage is not available, return false return false; } - const showNotifications = config.disable_version_check - ? false - : showNotificationsLocalStorage !== 'false'; if ( newDashVersion === undefined || compareVersions(currentDashVersion, newDashVersion) >= 0 ||