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).
Build URLs robustly against trailing slashes in configurable base URLs
Use the URL constructor or strip trailing slashes instead of string-concatenating a base URL with a path.
Bad example
| 1 | // If baseUrl ends in '/', this yields '...//api/resource' |
| 2 | const url = `${baseUrl}/api/resource?id=${id}`; |
| 3 | const res = await fetch(url); |
Explanation (EN)
Operators often configure base URLs with a trailing slash, producing a double slash that strict proxies reject with 404, silently degrading the feature.
Objašnjenje (HR)
Operatori često konfiguriraju bazne URL-ove sa završnom kosom crtom, čime nastaje dvostruka kosa crta koju strogi proxyji odbijaju s 404, tiho degradirajući funkcionalnost.
Good example
| 1 | const url = new URL('/api/resource', baseUrl); |
| 2 | url.searchParams.set('id', id); |
| 3 | const res = await fetch(url); |
| 4 | // or: const base = baseUrl.replace(/\/+$/, ''); |
Explanation (EN)
The URL constructor (or stripping trailing slashes) produces a correct path regardless of how the base URL is configured.
Objašnjenje (HR)
URL konstruktor (ili uklanjanje završnih kosih crta) proizvodi ispravnu putanju bez obzira na to kako je bazni URL konfiguriran.