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 ruleP1stack specificStack: javascript
ssrbrowserruntimereact
Defer window/document access to runtime to stay SSR-safe
Don't touch window at module top level (it throws 'window is not defined' during SSR); access it lazily inside functions or effects that run only in the browser.
PR: hegnar-web · org-mining-deep-2026-06Created: Jun 17, 2026
Bad example
Old codejavascript
| 1 | export const lazy = { |
| 2 | mql: window.matchMedia('(min-width: 1024px)'), // runs at import, fails in SSR |
| 3 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codejavascript
| 1 | export const lazy = { |
| 2 | getMql: () => window.matchMedia('(min-width: 1024px)'), // called in browser only |
| 3 | }; |
Explanation (EN)
Objašnjenje (HR)