How to force browsers to reload cached CSS and JS files
Codemia Knowledge Hub ·
The most reliable way to make browsers load a new version of a CSS or JS file is cache busting: change the file's URL whenever its content changes, for example by adding a version query string like style.css?v=1.2. You can also control caching with HTTP headers such as Cache-Control, and less reliably with meta tags or ETags.
Browsers cache files to cut server load, bandwidth and latency, which normally makes sites faster. The downside is that after you update a site, users may keep getting the old files.
1. Versioning (cache busting)
Append a version number or unique query string to the file URL, and change it every time you update the file:
Because the URL changed, the browser treats it as a new file and skips the cached copy.
On larger projects, automate this with build tools like Webpack, Gulp or Grunt, which can add content hashes to filenames for you.
2. Cache headers on the server
HTTP headers tell browsers how to cache files. The two that matter here:
- Cache-Control: sets how long the browser may keep the file before requesting a new copy.
Cache-Control: no-cachetells the browser to revalidate with the server before using the cached version. - Expires: gives an exact date and time after which the response is stale. An earlier date makes the browser fetch a new version sooner.
Apache, in .htaccess:
Nginx:
3. Meta tags
HTML meta tags can also influence caching, but they are generally less reliable than the methods above:
4. ETags
An ETag (Entity Tag) is a validator the server generates from the file content. When the file changes, its ETag changes, and the browser fetches a fresh copy. In Apache:
Comparing the methods
| Method | Description | Pros | Cons |
| Versioning/Cache Busting | Append a query parameter mostly linked with versioning info to the file URL. | Highly effective; easy to implement. | Manual update of version numbers needed. |
| Server Headers | Modify Cache-Control and Expires headers for CSS/JS files. | Fine-grained control over caching. | Requires server access and can be complex to set up correctly. |
| Meta Tags | Use HTML meta tags to control caching. | Easy to implement. | Not as reliable; limited control over external files. |
| ETag | Utilize server-generated ETag for validation. | Validates files based on content changes. | Configuration can be complex; potentially overkill for small projects. |
Test your caching setup in several browsers to confirm it behaves as expected. Frequent cache invalidation suits development, but in production keep caching tuned for performance, which is why changing URLs on each release usually beats disabling the cache.
Related reading
- How to get InputStream via Spring-Feign?
- How to get largest number of consecutive integers in a substantially large array (spread across multiple machines)
- How to get Memory cost of Distributed Map across node in hazelcast
- How to handle consensus in a decentralized event sourced database?
- How to force garbage collection in Java?
- How to force log compaction of a Kafka topic?
- How to force Sequential Javascript Execution?
- How to get a Color from hexadecimal Color String
System design interview problems on this topic
- Design a Key Value Store: solution and walkthrough
- Design a Distributed Unique Id Generator: solution and walkthrough
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.