diff --git a/site/lib/src/client/global_scripts.dart b/site/lib/src/client/global_scripts.dart index 890d91caa26..0bb0b317d63 100644 --- a/site/lib/src/client/global_scripts.dart +++ b/site/lib/src/client/global_scripts.dart @@ -117,10 +117,16 @@ void _setUpTabs() { // If the tab wrapper and this tab have a save key and ID defined, // switch other tabs to the tab with the same ID. _findAndActivateTabsWithSaveId(currentSaveKey, currentSaveId); - web.window.localStorage.setItem( - 'tab-save-$currentSaveKey', - currentSaveId, - ); + try { + web.window.localStorage.setItem( + 'tab-save-$currentSaveKey', + currentSaveId, + ); + } catch (e) { + if (kDebugMode) { + print('Error accessing localStorage $e'); + } + } } else { _clearActiveTabs(tabs); _setActiveTab(tabElement); @@ -129,12 +135,19 @@ void _setUpTabs() { tabElement.addEventListener('click', handleClick.toJS); - // If a tab was previously specified as selected in local storage, - // save a reference to it that can be switched to later. - if (saveId.isNotEmpty && - localStorageKey != null && - web.window.localStorage.getItem(localStorageKey) == saveId) { - tabToChangeTo = tabElement; + try { + // If a tab was previously specified as selected in local storage, + // save a reference to it that can be switched to later. + final tabSaveKey = localStorageKey != null + ? web.window.localStorage.getItem(localStorageKey) + : null; + if (saveId.isNotEmpty && tabSaveKey != null && tabSaveKey == saveId) { + tabToChangeTo = tabElement; + } + } catch (e) { + if (kDebugMode) { + print('Error accessing localStorage $e'); + } } } @@ -165,8 +178,14 @@ void _updateTabsFromQueryParameters() { for (final MapEntry(:key, :value) in originalQueryParameters.entries) { if (key.startsWith('tab-save-')) { - web.window.localStorage.setItem(key, value); - updatedQueryParameters.remove(key); + try { + web.window.localStorage.setItem(key, value); + updatedQueryParameters.remove(key); + } catch (e) { + if (kDebugMode) { + print('Error accessing localStorage $e'); + } + } } } diff --git a/site/lib/src/components/common/client/cookie_notice.dart b/site/lib/src/components/common/client/cookie_notice.dart index d04fbadc746..9a1d77841f0 100644 --- a/site/lib/src/components/common/client/cookie_notice.dart +++ b/site/lib/src/components/common/client/cookie_notice.dart @@ -26,18 +26,28 @@ final class _CookieNoticeState extends State { void initState() { if (kIsWeb) { var shouldShowNotice = true; - if (web.window.localStorage.getItem(_cookieStorageKey) - case final lastConsentedMs?) { - if (int.tryParse(lastConsentedMs) case final msFromEpoch?) { - final consentedDateTime = DateTime.fromMillisecondsSinceEpoch( - msFromEpoch, - ); - final difference = consentedDateTime.difference(DateTime.now()); - if (difference.inDays < 180) { - // If consented less than 180 days ago, don't show the notice. - shouldShowNotice = false; + try { + final storedConsent = web.window.localStorage.getItem( + _cookieStorageKey, + ); + if (storedConsent case final lastConsentedMs?) { + if (int.tryParse(lastConsentedMs) case final msFromEpoch?) { + final consentedDateTime = DateTime.fromMillisecondsSinceEpoch( + msFromEpoch, + ); + final difference = consentedDateTime.difference(DateTime.now()); + if (difference.inDays < 180) { + // If consented less than 180 days ago, don't show the notice. + shouldShowNotice = false; + } } } + } catch (e) { + // If localStorage is unavailable or throws an error, + // keep the `shouldShowNotice` to true. + if (kDebugMode) { + print('Failed to get stored content $e'); + } } showNotice = shouldShowNotice; @@ -69,10 +79,16 @@ final class _CookieNoticeState extends State { content: 'OK, got it', style: ButtonStyle.filled, onClick: () { - web.window.localStorage.setItem( - _cookieStorageKey, - DateTime.now().millisecondsSinceEpoch.toString(), - ); + try { + web.window.localStorage.setItem( + _cookieStorageKey, + DateTime.now().millisecondsSinceEpoch.toString(), + ); + } catch (e) { + if (kDebugMode) { + print('Failed to set stored consent: $e'); + } + } setState(() { showNotice = false; }); diff --git a/site/lib/src/components/layout/theme_switcher.dart b/site/lib/src/components/layout/theme_switcher.dart index 65c53445d67..7006c600a04 100644 --- a/site/lib/src/components/layout/theme_switcher.dart +++ b/site/lib/src/components/layout/theme_switcher.dart @@ -73,7 +73,13 @@ final class _ThemeSwitcherState extends State { ); } - web.window.localStorage.setItem('theme', newTheme.id); + try { + web.window.localStorage.setItem('theme', newTheme.id); + } catch (e) { + if (kDebugMode) { + print('Failed to save theme preference: $e'); + } + } setState(() { _currentTheme = newTheme; diff --git a/site/lib/src/layouts/dash_layout.dart b/site/lib/src/layouts/dash_layout.dart index bbbeabc3f22..327309121ca 100644 --- a/site/lib/src/layouts/dash_layout.dart +++ b/site/lib/src/layouts/dash_layout.dart @@ -171,16 +171,20 @@ ga('send', 'pageview'); // avoid a flash of the initial theme on load. raw(''' '''),