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).
Set Cache-Control headers on cacheable GET endpoints
GET endpoints serving reference or slow-changing data should set explicit Cache-Control headers instead of leaving caching implicit.
Bad example
| 1 | @Get('dividends') |
| 2 | async getDividends(@Query() query: DividendsQueryDto) { |
| 3 | // No Cache-Control: every client hit goes to origin, no CDN caching |
| 4 | return this.service.getDividends(query); |
| 5 | } |
Explanation (EN)
Without Cache-Control, clients and CDNs can't cache the response, so every request hits the origin even though the data changes slowly.
Objašnjenje (HR)
Bez Cache-Control zaglavlja klijenti i CDN ne mogu kesirati odgovor, pa svaki zahtjev pogada izvor iako se podaci rijetko mijenjaju.
Good example
| 1 | @Get('dividends') |
| 2 | async getDividends(@Query() query: DividendsQueryDto, @Res({ passthrough: true }) res: Response) { |
| 3 | res.setHeader('Cache-Control', 'public, max-age=300'); |
| 4 | return this.service.getDividends(query); |
| 5 | } |
Explanation (EN)
An explicit Cache-Control with a per-endpoint TTL lets clients and CDNs cache cacheable responses, cutting load and latency. Choose the TTL based on how fresh the data needs to be.
Objašnjenje (HR)
Eksplicitni Cache-Control s TTL-om po endpointu omogucuje klijentima i CDN-u da kesiraju kesirajuce odgovore, smanjujuci opterecenje i latenciju. TTL biraj prema tome koliko svjezi podaci moraju biti.
Notes (EN)
For user-specific or sensitive data use 'private' or 'no-store'; reserve 'public' for shared, non-personalized responses.
Bilješke (HR)
Za korisnicki specificne ili osjetljive podatke koristi 'private' ili 'no-store'; 'public' ostavi za zajednicke, nepersonalizirane odgovore.