Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
A cache/comparison key must include the same fields on every call site, including defaulted ones
When building a request/cache key from an object of optional-but-defaulted fields, make sure every call site that builds that key for the 'same' request includes the same set of fields — a field that defaults to a truthy value rather than being fully omitted still has to appear identically on both sides, or the keys can never compare equal.
Bad example
| 1 | // preload — omits `order` |
| 2 | const preloadKey = buildRequestKey({ id, page, pageSize }); |
| 3 |
|
| 4 | // active-tab fetch — always includes `order` (defaults to DESC) |
| 5 | const fetchKey = buildRequestKey({ id, page, pageSize, order: sortOrder }); |
| 6 |
|
| 7 | if (preloadKey === fetchKey) { /* never true, since order defaults truthy */ } |
Explanation (EN)
Omitted defaulted fields make logically equal requests produce different cache identities.
Objašnjenje (HR)
Izostavljene zadane vrijednosti mogu logički jednakim zahtjevima dati različit cache identitet.
Good example
| 1 | // both call sites include the same fields, including order |
| 2 | const preloadKey = buildRequestKey({ id, page, pageSize, order: sortOrder }); |
| 3 | const fetchKey = buildRequestKey({ id, page, pageSize, order: sortOrder }); |
| 4 |
|
| 5 | if (preloadKey === fetchKey) { /* correctly hits when nothing relevant changed */ } |
Explanation (EN)
Both keys are built from the exact same field set, so they compare equal whenever the underlying request would genuinely be the same, letting the cache hit as intended.
Objašnjenje (HR)
Oba ključa grade se od potpuno istog skupa polja, pa se poklapaju kad god bi zahtjev zaista bio isti, omogućujući kešu da pogodi kako je namijenjeno.
Notes (EN)
When two callers identify equivalent requests, compare their normalized parameters, defaults and ordering before constructing keys.
Exceptions / Tradeoffs (EN)
Do not force equal keys for requests whose sorting, authorization or result-shaping inputs differ.