← Back to Frontend, page 1

frontend-0019 · Easy fresher

What is the difference between Cache-Control no-cache and no-store, and how does an ETag lead to a 304 response?

no-store tells caches not to keep the response at all. no-cache lets a cache keep it but forces a check with the server before each reuse. The check sends the stored ETag in If-None-Match, and an unchanged resource gets a 304 with no body.

Full answer

A response can be reused without asking the server only while it is fresh. The Cache-Control header sets the rules.

  • max-age=N says the response is fresh for N seconds. During that time the browser does not contact the server.
  • no-cache does not mean "do not cache". The response may be stored, but it must be revalidated with the server before each reuse.
  • no-store means do not keep a copy in any cache. Use it for private data such as bank statements, where a stored copy would be a risk.
  • public allows shared caches such as a CDN. private limits storage to the user's own browser.

Revalidation uses a validator. The server sends an ETag, which is an identifier of the content, for example a hash. The browser stores it. At the next use it sends If-None-Match with the stored value. If the content has not changed, the server answers 304 Not Modified with no body, and the browser reuses its copy. This saves the download but not the round trip. Last-Modified with If-Modified-Since does the same job with a date.

A usual design:

  • HTML: no-cache, so users get the new page, and the 304 makes it cheap.
  • Hashed static files: long max-age with immutable. This is covered in a separate question.
  • Private API data: private, no-store or no-cache, depending on how fresh it must be.

The Node script below sets no-cache and an ETag on one route, requests it twice, and shows that the second request gets 304. A second route uses no-store. Node's fetch does not have an HTTP cache, so the script sends the validator by hand, as a browser would.

javascript
import http from 'node:http';
import { createHash } from 'node:crypto';

const body = JSON.stringify({ courses: ['HTML', 'CSS', 'JavaScript'] });
const etag = '"' + createHash('sha1').update(body).digest('hex').slice(0, 8) + '"';

const server = http.createServer((req, res) => {
  if (req.url === '/api/courses') {
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('ETag', etag);
    if (req.headers['if-none-match'] === etag) { res.statusCode = 304; return res.end(); }
    return res.end(body);
  }
  res.setHeader('Cache-Control', 'no-store');
  res.end('statement');
});
await new Promise((resolve) => server.listen(0, resolve));
const base = `http://localhost:${server.address().port}`;

const first = await fetch(base + '/api/courses');
console.log(first.status, first.headers.get('cache-control'), 'etag set:', !!first.headers.get('etag'), 'bytes:', (await first.text()).length);
const second = await fetch(base + '/api/courses', { headers: { 'If-None-Match': first.headers.get('etag') } });
console.log(second.status, 'bytes:', (await second.text()).length);
const secret = await fetch(base + '/account');
console.log(secret.status, secret.headers.get('cache-control'));
server.close();

Run with Node 24.22.1. The output matched the text in the answer in two runs. Run with Node and the output was identical in two runs. Node fetch has no HTTP cache, so the conditional request is sent by hand. Real browser cache behaviour was not run.

Key points

  • no-cache means store but revalidate before use. no-store means do not keep it.
  • max-age sets how long a response is fresh without asking the server.
  • ETag with If-None-Match lets the server answer 304 without a body.
  • private is for the user's browser only. public allows shared caches.
  • A 304 saves bytes, not the round trip.

Common mistakes

  • Using no-cache when the intention is no-store, for sensitive data.
  • Thinking that 304 means the request never reached the server.
  • Forgetting to send an ETag, so revalidation always downloads the full body.
  • Setting a long max-age on an HTML page that links to hashed files.

Follow-ups

  • How does a CDN treat private responses?
  • What is the difference between a strong and a weak ETag?
  • Why do some teams prefer ETag over Last-Modified?

Tips

  • Say that the name no-cache is misleading, then explain what it does.
  • Link caching rules to the file type: HTML, hashed assets and private data.