Skip to content

Commit ba87e26

Browse files
authored
Merge pull request #3232 from plotly/localstorage-handling
Add error handling for when localStorage is disabled
2 parents 2cdbb83 + ee9e10d commit ba87e26

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed

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

+69-31
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,20 @@ 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+
// If version check is disabled, return false to avoid
92+
// checking localStorage unnecessarily
93+
if (config.disable_version_check) {
94+
return false;
95+
}
96+
let showNotifications, lastFetched;
97+
try {
98+
showNotifications =
99+
localStorage.getItem('showNotifications') !== 'false';
100+
lastFetched = localStorage.getItem('lastFetched');
101+
} catch (e) {
102+
// If localStorage is not available, return false
103+
return false;
104+
}
84105
return (
85106
showNotifications &&
86107
(!lastFetched || Date.now() - Number(lastFetched) > DAY_IN_MS)
@@ -92,13 +113,21 @@ function shouldShowUpgradeNotification(
92113
newDashVersion,
93114
config
94115
) {
95-
const showNotificationsLocalStorage =
96-
localStorage.getItem('showNotifications');
97-
const showNotifications = config.disable_version_check
98-
? false
99-
: showNotificationsLocalStorage !== 'false';
100-
const lastDismissed = localStorage.getItem('lastDismissed');
101-
const lastDismissedVersion = localStorage.getItem('lastDismissedVersion');
116+
// If version check is disabled, return false to avoid
117+
// checking localStorage unnecessarily
118+
if (config.disable_version_check) {
119+
return false;
120+
}
121+
let showNotifications, lastDismissed, lastDismissedVersion;
122+
try {
123+
showNotifications =
124+
localStorage.getItem('showNotifications') !== 'false';
125+
lastDismissed = localStorage.getItem('lastDismissed');
126+
lastDismissedVersion = localStorage.getItem('lastDismissedVersion');
127+
} catch (e) {
128+
// If localStorage is not available, return false
129+
return false;
130+
}
102131
if (
103132
newDashVersion === undefined ||
104133
compareVersions(currentDashVersion, newDashVersion) >= 0 ||
@@ -113,10 +142,7 @@ function shouldShowUpgradeNotification(
113142
} else if (
114143
lastDismissedVersion &&
115144
!lastDismissed &&
116-
compareVersions(
117-
localStorage.getItem('lastDismissedVersion'),
118-
newDashVersion
119-
) < 0
145+
compareVersions(lastDismissedVersion, newDashVersion) < 0
120146
) {
121147
return true;
122148
} else {
@@ -131,19 +157,31 @@ export const VersionInfo = ({config}) => {
131157

132158
const setDontShowAgain = () => {
133159
// Set local storage to record the last dismissed notification
134-
localStorage.setItem('showNotifications', false);
160+
try {
161+
localStorage.setItem('showNotifications', false);
162+
} catch (e) {
163+
// Ignore errors if localStorage is not available
164+
}
135165
setUpgradeTooltipOpened(false);
136166
};
137167

138168
const setRemindMeLater = () => {
139169
// Set local storage to record the last dismissed notification
140-
localStorage.setItem('lastDismissed', Date.now());
170+
try {
171+
localStorage.setItem('lastDismissed', Date.now());
172+
} catch (e) {
173+
// Ignore errors if localStorage is not available
174+
}
141175
setUpgradeTooltipOpened(false);
142176
};
143177

144178
const setSkipThisVersion = () => {
145179
// Set local storage to record the last dismissed version
146-
localStorage.setItem('lastDismissedVersion', newDashVersion);
180+
try {
181+
localStorage.setItem('lastDismissedVersion', newDashVersion);
182+
} catch (e) {
183+
// Ignore errors if localStorage is not available
184+
}
147185
setUpgradeTooltipOpened(false);
148186
};
149187

0 commit comments

Comments
 (0)