Skip to content

Add error handling for when localStorage is disabled #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 69 additions & 31 deletions dash/dash-renderer/src/components/error/menu/VersionInfo.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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 {};
Expand All @@ -75,12 +88,20 @@ 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');
// If version check is disabled, return false to avoid
// checking localStorage unnecessarily
if (config.disable_version_check) {
return false;
}
let showNotifications, lastFetched;
try {
showNotifications =
localStorage.getItem('showNotifications') !== '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)
Expand All @@ -92,13 +113,21 @@ function shouldShowUpgradeNotification(
newDashVersion,
config
) {
const showNotificationsLocalStorage =
localStorage.getItem('showNotifications');
const showNotifications = config.disable_version_check
? false
: showNotificationsLocalStorage !== 'false';
const lastDismissed = localStorage.getItem('lastDismissed');
const lastDismissedVersion = localStorage.getItem('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 {
showNotifications =
localStorage.getItem('showNotifications') !== 'false';
lastDismissed = localStorage.getItem('lastDismissed');
lastDismissedVersion = localStorage.getItem('lastDismissedVersion');
} catch (e) {
// If localStorage is not available, return false
return false;
}
if (
newDashVersion === undefined ||
compareVersions(currentDashVersion, newDashVersion) >= 0 ||
Expand All @@ -113,10 +142,7 @@ function shouldShowUpgradeNotification(
} else if (
lastDismissedVersion &&
!lastDismissed &&
compareVersions(
localStorage.getItem('lastDismissedVersion'),
newDashVersion
) < 0
compareVersions(lastDismissedVersion, newDashVersion) < 0
) {
return true;
} else {
Expand All @@ -131,19 +157,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);
};

Expand Down