Skip to content

Commit 509199c

Browse files
dixita0607parlough
andauthored
Gracefully handle failures to access localStorage (#12682)
A follow up PR resolving [PR comments](#12673 (comment)) on #12673 by Gemini code assist. --------- Co-authored-by: Parker Lougheed <[email protected]>
1 parent 477e39a commit 509199c

File tree

4 files changed

+81
-37
lines changed

4 files changed

+81
-37
lines changed

site/lib/src/client/global_scripts.dart

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ void _setUpTabs() {
117117
// If the tab wrapper and this tab have a save key and ID defined,
118118
// switch other tabs to the tab with the same ID.
119119
_findAndActivateTabsWithSaveId(currentSaveKey, currentSaveId);
120-
web.window.localStorage.setItem(
121-
'tab-save-$currentSaveKey',
122-
currentSaveId,
123-
);
120+
try {
121+
web.window.localStorage.setItem(
122+
'tab-save-$currentSaveKey',
123+
currentSaveId,
124+
);
125+
} catch (e) {
126+
if (kDebugMode) {
127+
print('Error accessing localStorage: $e');
128+
}
129+
}
124130
} else {
125131
_clearActiveTabs(tabs);
126132
_setActiveTab(tabElement);
@@ -129,12 +135,19 @@ void _setUpTabs() {
129135

130136
tabElement.addEventListener('click', handleClick.toJS);
131137

132-
// If a tab was previously specified as selected in local storage,
133-
// save a reference to it that can be switched to later.
134-
if (saveId.isNotEmpty &&
135-
localStorageKey != null &&
136-
web.window.localStorage.getItem(localStorageKey) == saveId) {
137-
tabToChangeTo = tabElement;
138+
try {
139+
// If a tab was previously specified as selected in local storage,
140+
// save a reference to it that can be switched to later.
141+
final tabSaveKey = localStorageKey != null
142+
? web.window.localStorage.getItem(localStorageKey)
143+
: null;
144+
if (saveId.isNotEmpty && tabSaveKey != null && tabSaveKey == saveId) {
145+
tabToChangeTo = tabElement;
146+
}
147+
} catch (e) {
148+
if (kDebugMode) {
149+
print('Error accessing localStorage: $e');
150+
}
138151
}
139152
}
140153

@@ -165,8 +178,14 @@ void _updateTabsFromQueryParameters() {
165178

166179
for (final MapEntry(:key, :value) in originalQueryParameters.entries) {
167180
if (key.startsWith('tab-save-')) {
168-
web.window.localStorage.setItem(key, value);
169-
updatedQueryParameters.remove(key);
181+
try {
182+
web.window.localStorage.setItem(key, value);
183+
updatedQueryParameters.remove(key);
184+
} catch (e) {
185+
if (kDebugMode) {
186+
print('Error accessing localStorage: $e');
187+
}
188+
}
170189
}
171190
}
172191

site/lib/src/components/common/client/cookie_notice.dart

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,28 @@ final class _CookieNoticeState extends State<CookieNotice> {
2626
void initState() {
2727
if (kIsWeb) {
2828
var shouldShowNotice = true;
29-
if (web.window.localStorage.getItem(_cookieStorageKey)
30-
case final lastConsentedMs?) {
31-
if (int.tryParse(lastConsentedMs) case final msFromEpoch?) {
32-
final consentedDateTime = DateTime.fromMillisecondsSinceEpoch(
33-
msFromEpoch,
34-
);
35-
final difference = consentedDateTime.difference(DateTime.now());
36-
if (difference.inDays < 180) {
37-
// If consented less than 180 days ago, don't show the notice.
38-
shouldShowNotice = false;
29+
try {
30+
final storedConsent = web.window.localStorage.getItem(
31+
_cookieStorageKey,
32+
);
33+
if (storedConsent case final lastConsentedMs?) {
34+
if (int.tryParse(lastConsentedMs) case final msFromEpoch?) {
35+
final consentedDateTime = DateTime.fromMillisecondsSinceEpoch(
36+
msFromEpoch,
37+
);
38+
final difference = consentedDateTime.difference(DateTime.now());
39+
if (difference.inDays < 180) {
40+
// If consented less than 180 days ago, don't show the notice.
41+
shouldShowNotice = false;
42+
}
3943
}
4044
}
45+
} catch (e) {
46+
// If localStorage is unavailable or throws an error,
47+
// keep the `shouldShowNotice` to true.
48+
if (kDebugMode) {
49+
print('Failed to get stored content: $e');
50+
}
4151
}
4252

4353
showNotice = shouldShowNotice;
@@ -69,10 +79,16 @@ final class _CookieNoticeState extends State<CookieNotice> {
6979
content: 'OK, got it',
7080
style: ButtonStyle.filled,
7181
onClick: () {
72-
web.window.localStorage.setItem(
73-
_cookieStorageKey,
74-
DateTime.now().millisecondsSinceEpoch.toString(),
75-
);
82+
try {
83+
web.window.localStorage.setItem(
84+
_cookieStorageKey,
85+
DateTime.now().millisecondsSinceEpoch.toString(),
86+
);
87+
} catch (e) {
88+
if (kDebugMode) {
89+
print('Failed to set stored consent: $e');
90+
}
91+
}
7692
setState(() {
7793
showNotice = false;
7894
});

site/lib/src/components/layout/theme_switcher.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ final class _ThemeSwitcherState extends State<ThemeSwitcher> {
7373
);
7474
}
7575

76-
web.window.localStorage.setItem('theme', newTheme.id);
76+
try {
77+
web.window.localStorage.setItem('theme', newTheme.id);
78+
} catch (e) {
79+
if (kDebugMode) {
80+
print('Failed to save theme preference: $e');
81+
}
82+
}
7783

7884
setState(() {
7985
_currentTheme = newTheme;

site/lib/src/layouts/dash_layout.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,19 @@ ga('send', 'pageview');
171171
// avoid a flash of the initial theme on load.
172172
raw('''
173173
<script>
174-
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
175-
176-
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
177-
if (storedTheme === 'auto-mode') {
178-
document.body.classList.add(
179-
'auto-mode',
180-
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
181-
);
182-
} else {
183-
document.body.classList.add(storedTheme);
174+
try {
175+
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
176+
if (storedTheme === 'auto-mode') {
177+
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
178+
document.body.classList.add(
179+
'auto-mode',
180+
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
181+
);
182+
} else {
183+
document.body.classList.add(storedTheme);
184+
}
185+
} catch (e) {
186+
// localStorage is not available, do nothing and fall back to default.
184187
}
185188
</script>
186189
'''),

0 commit comments

Comments
 (0)