Skip to content

Commit d2e732f

Browse files
committed
Add error handling for when localStorage is disabled
1 parent e0a2e16 commit d2e732f

File tree

1 file changed

+63
-28
lines changed

1 file changed

+63
-28
lines changed

dash/dash-renderer/src/components/error/menu/VersionInfo.react.js

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ async function requestDashVersionInfo(config) {
3232
ddk_version: ddkVersion,
3333
plotly_version: plotlyVersion
3434
} = config;
35-
const cachedVersionInfo = localStorage.getItem('cachedNewDashVersion');
36-
const cachedNewDashVersionLink = localStorage.getItem(
37-
'cachedNewDashVersionLink'
38-
);
39-
const lastFetched = localStorage.getItem('lastFetched');
35+
let cachedVersionInfo, cachedNewDashVersionLink, lastFetched;
36+
try {
37+
cachedVersionInfo = localStorage.getItem('cachedNewDashVersion');
38+
cachedNewDashVersionLink = localStorage.getItem(
39+
'cachedNewDashVersionLink'
40+
);
41+
lastFetched = localStorage.getItem('lastFetched');
42+
} catch (e) {
43+
// If localStorage is not available, return an empty object
44+
return {};
45+
}
4046
if (
4147
lastFetched &&
4248
Date.now() - Number(lastFetched) < DAY_IN_MS &&
@@ -57,12 +63,19 @@ async function requestDashVersionInfo(config) {
5763
.then(response => response.json())
5864
.then(body => {
5965
if (body && body.version && body.link) {
60-
localStorage.setItem(
61-
'cachedNewDashVersion',
62-
JSON.stringify(body.version)
63-
);
64-
localStorage.setItem('cachedNewDashVersionLink', body.link);
65-
localStorage.setItem('lastFetched', Date.now());
66+
try {
67+
localStorage.setItem(
68+
'cachedNewDashVersion',
69+
JSON.stringify(body.version)
70+
);
71+
localStorage.setItem(
72+
'cachedNewDashVersionLink',
73+
body.link
74+
);
75+
localStorage.setItem('lastFetched', Date.now());
76+
} catch (e) {
77+
// Ignore errors if localStorage is not available
78+
}
6679
return body;
6780
} else {
6881
return {};
@@ -75,12 +88,19 @@ async function requestDashVersionInfo(config) {
7588
}
7689

7790
function shouldRequestDashVersion(config) {
78-
const showNotificationsLocalStorage =
79-
localStorage.getItem('showNotifications');
80-
const showNotifications = config.disable_version_check
81-
? false
82-
: showNotificationsLocalStorage !== 'false';
83-
const lastFetched = localStorage.getItem('lastFetched');
91+
let showNotificationsLocalStorage, showNotifications, lastFetched;
92+
try {
93+
showNotificationsLocalStorage =
94+
localStorage.getItem('showNotifications');
95+
96+
showNotifications = config.disable_version_check
97+
? false
98+
: showNotificationsLocalStorage !== 'false';
99+
lastFetched = localStorage.getItem('lastFetched');
100+
} catch (e) {
101+
// If localStorage is not available, return false
102+
return false;
103+
}
84104
return (
85105
showNotifications &&
86106
(!lastFetched || Date.now() - Number(lastFetched) > DAY_IN_MS)
@@ -92,13 +112,19 @@ function shouldShowUpgradeNotification(
92112
newDashVersion,
93113
config
94114
) {
95-
const showNotificationsLocalStorage =
96-
localStorage.getItem('showNotifications');
115+
let showNotificationsLocalStorage, lastDismissed, lastDismissedVersion;
116+
try {
117+
showNotificationsLocalStorage =
118+
localStorage.getItem('showNotifications');
119+
lastDismissed = localStorage.getItem('lastDismissed');
120+
lastDismissedVersion = localStorage.getItem('lastDismissedVersion');
121+
} catch (e) {
122+
// If localStorage is not available, return false
123+
return false;
124+
}
97125
const showNotifications = config.disable_version_check
98126
? false
99127
: showNotificationsLocalStorage !== 'false';
100-
const lastDismissed = localStorage.getItem('lastDismissed');
101-
const lastDismissedVersion = localStorage.getItem('lastDismissedVersion');
102128
if (
103129
newDashVersion === undefined ||
104130
compareVersions(currentDashVersion, newDashVersion) >= 0 ||
@@ -113,10 +139,7 @@ function shouldShowUpgradeNotification(
113139
} else if (
114140
lastDismissedVersion &&
115141
!lastDismissed &&
116-
compareVersions(
117-
localStorage.getItem('lastDismissedVersion'),
118-
newDashVersion
119-
) < 0
142+
compareVersions(lastDismissedVersion, newDashVersion) < 0
120143
) {
121144
return true;
122145
} else {
@@ -131,19 +154,31 @@ export const VersionInfo = ({config}) => {
131154

132155
const setDontShowAgain = () => {
133156
// Set local storage to record the last dismissed notification
134-
localStorage.setItem('showNotifications', false);
157+
try {
158+
localStorage.setItem('showNotifications', false);
159+
} catch (e) {
160+
// Ignore errors if localStorage is not available
161+
}
135162
setUpgradeTooltipOpened(false);
136163
};
137164

138165
const setRemindMeLater = () => {
139166
// Set local storage to record the last dismissed notification
140-
localStorage.setItem('lastDismissed', Date.now());
167+
try {
168+
localStorage.setItem('lastDismissed', Date.now());
169+
} catch (e) {
170+
// Ignore errors if localStorage is not available
171+
}
141172
setUpgradeTooltipOpened(false);
142173
};
143174

144175
const setSkipThisVersion = () => {
145176
// Set local storage to record the last dismissed version
146-
localStorage.setItem('lastDismissedVersion', newDashVersion);
177+
try {
178+
localStorage.setItem('lastDismissedVersion', newDashVersion);
179+
} catch (e) {
180+
// Ignore errors if localStorage is not available
181+
}
147182
setUpgradeTooltipOpened(false);
148183
};
149184

0 commit comments

Comments
 (0)