Web Development
CSS
JavaScript
Browser Cache
Web Optimization

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:

html
<link rel="stylesheet" href="style.css?v=1.2">
<script src="script.js?v=1.2"></script>

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-cache tells 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:

apache
<FilesMatch "\.(js|css)$">
    Header set Cache-Control "no-cache"
</FilesMatch>

Nginx:

nginx
location ~* \.(js|css)$ {
    add_header Cache-Control "no-cache";
}

3. Meta tags

HTML meta tags can also influence caching, but they are generally less reliable than the methods above:

html
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>

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:

apache
FileETag All

Comparing the methods

MethodDescriptionProsCons
Versioning/Cache BustingAppend a query parameter mostly linked with versioning info to the file URL.Highly effective; easy to implement.Manual update of version numbers needed.
Server HeadersModify 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 TagsUse HTML meta tags to control caching.Easy to implement.Not as reliable; limited control over external files.
ETagUtilize 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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design