Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP0universalStack: javascript
browserstorageerror-handlingsafari
Guard localStorage by wrapping access in try/catch, not just checking it exists
Checking that window.localStorage exists doesn't prevent throws — Safari private mode throws on read/write — so wrap the actual access.
PR: hegnar-web · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | if (window.localStorage) { |
| 2 | localStorage.setItem('visible', '1'); // still throws SecurityError in Safari private mode |
| 3 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | function safeWrite(key: string, value: string) { |
| 2 | try { |
| 3 | localStorage.setItem(key, value); |
| 4 | } catch { |
| 5 | /* private mode / quota — ignore */ |
| 6 | } |
| 7 | } |
Explanation (EN)
Objašnjenje (HR)