Browser cache and HTTP caching
A browser keeps copies of files and the server controls this with response headers, mainly Cache-Control. While max-age holds there is no request to the server; then a check runs through ETag and If-None-Match, and 304 means the file is unchanged. no-cache allows storing but requires revalidation, while no-store forbids storing at all. A stale cache is told from a defect through Network and Disable cache.
QA Fundamentals · Web and client-server
After a release a tester sees the old version and a colleague the new one. Who is wrong?
The release went out, the bug was fixed, but on the tester's laptop it still reproduces. On the colleague's laptop everything is fine. The developer says "works on my machine", the tester answers "not on mine", and both are right: each has their own copy of the files. This lesson is about how a browser decides whether to ask the server and how QA tells a stale cache from a real defect.
How it works and why. Downloading a file is expensive, so the browser keeps a copy and reuses it. The server controls this with response headers. Cache-Control: max-age=N says that for N seconds the copy counts as fresh, and during that time the browser does not contact the server. When the time is up, the browser does not throw the copy away but asks "Has the file changed?" and adds If-None-Match with the version tag (ETag). If the file is the same, the server answers 304 Not Modified with no body and the copy stays in use. If it changed, a 200 response brings the new file.
What the browser decides
Here is what it looks like in HTTP:
GET /assets/app.js HTTP/1.1
HTTP/1.1 200 OK
Cache-Control: max-age=31536000
ETag: "a1b2c3"
GET /assets/app.js HTTP/1.1
If-None-Match: "a1b2c3"
HTTP/1.1 304 Not Modified
ETag: "a1b2c3"
The first response allows using the file for a whole year. The second shows validation: the client passed the tag and the server confirmed the file is the same.
The directives QA meets most often:
| Directive | Meaning |
|---|---|
| max-age=N | the copy is fresh for N seconds, the server is not asked |
| no-cache | may be stored, but validate before every use |
| no-store | do not store at all |
| private / public | browser cache only / shared caches too (CDN, proxy) |
The names are confusing: no-cache does not forbid storing, no-store does. So a response with personal data must carry no-store, not no-cache.
Two ways to serve a file
A long max-age and the same URL
The file changed but the address did not. Browsers that hold a copy do not ask the server and show the old version for a long time.
A hash in the file name, HTML without a long cache
A new release gets a new file address. You do not need to "kill" the copy: nobody refers to it any more.
Example: from "the bug is not fixed" to understanding the cause. The first thing QA does when seeing the old behaviour after a release is check whether it comes from the cache. The initial version looks like this:
app.js Cache-Control: max-age=31536000
URL: /assets/app.js (no hash)
Release: a new app.js on the server
Result: open tabs and returning users
keep the old app.js for a year
Here the cause is visible at once: the address did not change and the cache lifetime is long. The fixed version:
app.4f9c2e1a.js Cache-Control: max-age=31536000
index.html Cache-Control: no-cache
Release: the new index.html points to
app.7b30d5c8.js (a new URL)
Result: users get the new file at once,
the old one is left unreferenced
How to check it in three minutes. In DevTools open Network and reload the page. In the Size column the text "disk cache" or "memory cache" says there was no request to the server. Enable Disable cache (it works while DevTools is open) or do a hard reload: if the bug is gone, it was the cache. In the report write what exactly reproduces with a clean cache and add the Cache-Control value from the response.
Limits and alternatives. The browser cache is not the only one. A CDN and proxies may stand between the user and the server, and they cache by their own rules (the s-maxage, public and private directives, the Vary header). So "I cleared the browser cache" does not always help: the stale copy may sit on the CDN. A service worker (a script that intercepts requests, including in a PWA) has its own Cache Storage that ignores HTTP caching headers: the script itself updates it. Mobile apps also often keep their own data cache with rules of their own. It can be worse: a user sees someone else's data when a response with personal data was cached in a shared cache. That is already a serious security defect. Finally, the cache differs from cookies and localStorage, although all of them "store something": the cache stores server responses and is driven by its headers, while app storage is driven by the application itself.
Typical mistakes.
- Filing "the fix does not work" without checking the cache: the developer opens the page with no old copy and cannot reproduce.
- Writing "cleared the cache, it helped" and not saying that this is a file versioning defect: the next release will give the same effect.
- Confusing no-cache and no-store: the first allows storing, so personal data may stay on disk.
- Checking a release in the same browser that held the previous version and seeing no difference: a clean profile window or Disable cache is needed.
- Forgetting the CDN: after clearing the browser, the old copy arrives from an edge server.
Check yourself
After a release the tester has the old page and a colleague the new one. Files are served with a one-year max-age and have the same names between releases. How do you check that it is the cache and not a bug?
Answer
I open Network and look whether there is a request to the server: "disk cache" in the Size column means there is not. I enable Disable cache or do a hard reload: if the old behaviour is gone, it is the cache. A separate defect: file names without a content hash combined with a long max-age.
The browser sent If-None-Match and the server answered 304 with no body. What does it say about the file and why is there no body?
Answer
The file has not changed, the server compared the version tag (ETag). There is no body because the browser already has it in the cache and takes it from there, which saves traffic.