Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Drop SSR guards in code that only runs on the client
Omit typeof window === 'undefined' checks in client-only components/hooks where the code can never run on the server.
Bad example
| 1 | // Hook only ever runs inside a client-only component |
| 2 | const connect = useCallback(() => { |
| 3 | if (typeof window === 'undefined') return; // can never be true here |
| 4 | const es = new EventSource(url); |
| 5 | }, []); |
Explanation (EN)
If the surrounding code is guaranteed client-only, the SSR guard is dead code that implies a server path that doesn't exist.
Objašnjenje (HR)
Ako je okolni kod garantirano samo klijentski, SSR guard je mrtvi kod koji sugerira serverski put koji ne postoji.
Good example
| 1 | const connect = useCallback(() => { |
| 2 | const es = new EventSource(url); |
| 3 | }, []); |
Explanation (EN)
Removing the impossible branch keeps the intent clear: this runs in the browser.
Objašnjenje (HR)
Uklanjanje nemoguce grane cuva namjeru jasnom: ovo se izvrsava u pregledniku.
Exceptions / Tradeoffs (EN)
Keep the guard for genuinely shared/isomorphic code, or code that may run during SSR or in non-browser environments.
Iznimke / Tradeoffi (HR)
Zadrzite guard za stvarno dijeljeni/izomorfni kod ili kod koji se moze izvrsavati tijekom SSR-a ili u ne-preglednickim okruzenjima.