HTTP Caching Headers yaj
performance problems CDN pagh caching layer solve try law' actually wej HTTP headers (set correctly) free solve - apps law' accident wrong set (choice pagh). Cache-Control, ETag, Last-Modified browser resource re-fetch, cheaply re-validate, disk vo' instantly serve control - wej outcomes Dung difference configure Daq.
Cache-Control: qatlh header
Cache-Control browser (proxy CDN je) jatlh: response server contact pagh chay' long reuse laH.
Cache-Control: max-age=3600
meaning: 3600 seconds Dung server ask Qo' - cached copy reuse. window Dung request pagh, fastest possible outcome, server-side optimization law' nom (round trip pagh).
common directives:
max-age=<seconds>- response fresh long (browser cache perspective).public- shared caches (CDNs, proxies) cache laH (browser neH pagh).private- browser neH cache; shared CDN app qaSpa' Qo'.no-cache- name 'ach caching allow, use Hoch server revalidation force (ETagbIngDaq). "never cache" vaDno-store.no-store- cache pagh, nuqDaq. sensitive per-user data responses vaD right choice.
Cache-Control: private, no-store # a user's account settings page
Cache-Control: public, max-age=31536000 # a hashed, versioned JS bundle
Cache-Control: no-cache # an HTML page you want revalidated every load
static assets max-age very long qatlh
build pipeline filenames content hash rap (app.a3f9c1.js, app.js pagh) - content choH vaj filename choH. specific file forever cache safe (new version new URL):
Cache-Control: public, max-age=31536000, immutable
immutable max-age law' signal: browser hard refresh Daq je revalidate pagh (content URL Daq choH laHbe'). hashed versioned filenames neH Dun - plain app.js overwritten in place Daq Qo' (deploy Hoch invisible already-loaded browsers).
revalidation: full re-download law' cheaper
resources choH 'ach cached poQ: max-age neH pagh - browser "actually choH'a'?" cheap check (blind re-download pagh). ETag 'ej Last-Modified 'oH.
app.get('/api/report/:id', async (req, res) => {
const report = await getReport(req.params.id);
const etag = `"${hashOf(report)}"`;
res.set('ETag', etag);
res.set('Cache-Control', 'no-cache');
if (req.headers['if-none-match'] === etag) {
return res.status(304).end(); // not modified — no body sent
}
res.json(report);
});
browser ETag nob If-None-Match header Daq next request Daq nob. match vaj server 304 Not Modified body pagh - browser cached copy reuse. no-cache "don't cache" pagh: "cache, check first" - check full response resend law' cheaper.
Last-Modified same, timestamp (hash pagh), If-Modified-Since:
res.set('Last-Modified', report.updatedAt.toUTCString());
if (req.headers['if-modified-since'] === report.updatedAt.toUTCString()) {
return res.status(304).end();
}
ETag prefer (cheap hash response compute laH): precise (content change Hoch invalidate); Last-Modified second-level granularity neH, data modification times correctly track depend.
stack: realistic API response
Cache-Control: private, max-age=60, must-revalidate
ETag: "9f8c3a"
meaning: 60 seconds Dung cached copy reuse (ask pagh), window qaSDI' If-None-Match revalidate (304 pagh fresh data). authenticated API responses (occasionally choH, request Hoch pagh) default Dun - client server hammer pagh, minutes-old data stuck pagh.
"update pagh" bugs mistake
plain non-hashed filename Cache-Control: public, max-age=31536000 set: "fix deploy 'ach users old version legh" most common cause. browser content choH Sov laHbe' - URL identical, year ask Qo' jatlh. fix shorter max-age pagh: file filename content hash (real change real new URL) 'ej long max-age URLs neH reserve.
yaj
Cache-Control browser request send'a' decide. ETag/Last-Modified ask vaj server nearly-free 304 (body resend pagh) decide. long immutable caching content-hashed URLs neH; short max-age + revalidation stable URL Daq choH laH Hoch; no-store sensitive (cache Daq write pagh).