Skip to content

Commit 94b79fe

Browse files
committed
fix(web): wrap localStorage in try-catch for restricted contexts
Addresses CodeRabbit review comments on #1713. - index.html: Wrap pre-mount IIFE in try-catch, fallback to dark mode - useTheme.ts: Wrap both getItem and setItem in try-catch with comments matching the established pattern in ProjectContext/Sidebar/WorkflowBuilder
1 parent 907ee15 commit 94b79fe

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

packages/web/index.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
<script>
2626
// Apply saved theme before React renders to prevent flash
2727
(function () {
28-
var theme = localStorage.getItem('archon-theme');
29-
if (theme === 'light') {
30-
document.documentElement.classList.remove('dark');
31-
} else {
28+
try {
29+
var theme = localStorage.getItem('archon-theme');
30+
if (theme === 'light') {
31+
document.documentElement.classList.remove('dark');
32+
} else {
33+
document.documentElement.classList.add('dark');
34+
}
35+
} catch (_) {
3236
document.documentElement.classList.add('dark');
3337
}
3438
})();

packages/web/src/hooks/useTheme.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const STORAGE_KEY = 'archon-theme';
66

77
function getInitialTheme(): Theme {
88
if (typeof window === 'undefined') return 'dark';
9-
const stored = localStorage.getItem(STORAGE_KEY);
10-
if (stored === 'light' || stored === 'dark') return stored;
9+
try {
10+
const stored = localStorage.getItem(STORAGE_KEY);
11+
if (stored === 'light' || stored === 'dark') return stored;
12+
} catch {
13+
// localStorage unavailable (Safari private mode, Firefox privacy settings)
14+
}
1115
return 'dark';
1216
}
1317

@@ -21,7 +25,11 @@ export function useTheme(): { theme: Theme; toggleTheme: () => void } {
2125
} else {
2226
root.classList.remove('dark');
2327
}
24-
localStorage.setItem(STORAGE_KEY, theme);
28+
try {
29+
localStorage.setItem(STORAGE_KEY, theme);
30+
} catch {
31+
// Storage unavailable or quota exceeded — theme state persists in memory
32+
}
2533
}, [theme]);
2634

2735
const toggleTheme = useCallback(() => {

0 commit comments

Comments
 (0)