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).
Substitute env-dependent URLs at build time, not via runtime injection
Use build-time constant/env replacement to bake host/URL values into static HTML and use a static preconnect, instead of branching on hostname and injecting scripts at runtime.
Bad example
| 1 | <script> |
| 2 | var host = ['localhost', '127.0.0.1'].includes(location.hostname) |
| 3 | ? 'stage.cdn.example.com' |
| 4 | : 'cdn.example.com'; |
| 5 | var s = document.createElement('script'); |
| 6 | s.src = 'https://' + host + '/sdk.js'; |
| 7 | document.head.appendChild(s); |
| 8 | </script> |
Explanation (EN)
Picking the host at runtime and injecting the script means the browser can't discover the resource early, and a static preconnect can't target a host that's only known at runtime.
Objašnjenje (HR)
Odabir hosta u runtimeu i injektiranje skripte znaci da preglednik ne moze rano otkriti resurs, a staticki preconnect ne moze ciljati host poznat tek u runtimeu.
Good example
| 1 | <!-- .env: VITE_CDN_HOST=stage.cdn.example.com --> |
| 2 | <!-- .env.production: VITE_CDN_HOST=cdn.example.com --> |
| 3 | <link rel="preconnect" href="https://%VITE_CDN_HOST%" crossorigin /> |
| 4 | <script async src="https://%VITE_CDN_HOST%/sdk.js"></script> |
Explanation (EN)
The build tool substitutes the env value into static HTML, so the URL is known up front, the browser can preconnect, and there's no runtime branching.
Objašnjenje (HR)
Alat za build umetne vrijednost env varijable u staticki HTML, pa je URL poznat unaprijed, preglednik se moze preconnectati i nema runtime grananja.
Exceptions / Tradeoffs (EN)
Runtime injection is acceptable when the resource genuinely depends on runtime state that the build cannot know (e.g. a value derived from the logged-in user). Balance against use-runtime-server-config-for-deploy-specific-values: bake values in at build time only when each environment ships its own build and no runtime variance exists.
Iznimke / Tradeoffi (HR)
Runtime injektiranje je prihvatljivo kad resurs stvarno ovisi o runtime stanju koje build ne moze znati (npr. vrijednost izvedena iz prijavljenog korisnika).