1

I have set a CacheControl declaration in my .htaccess for browsers to cache static files.

Upon successive deployments, how can I define an instruction to invalidate the client's cache?

<Files *.js>
Header add "Cache-Control" "max-age=604800"
</Files>
Atticus
  • 6,585
  • 10
  • 35
  • 57

1 Answers1

0

how can I define an instruction to invalidate the client's cache?

The short answer is that you can't - HTTP protocol doesn't support it.

But there is a sneaky way to do it: create a manifest html page listing all the assets which are cacheable, load it in an iframe then use Javascript to do a refresh on the iframe. This will create conditional requests for the content to the server - and if your server responds with a 2xx status the new content supplied will overwrite the cache.

However just because it's possible DOES NOT MAKE IT A GOOD IDEA (for lots of reasons - don't ask unless you want to pay me to come and provide consultancy and training for a couple of weeks); Do not try this at home!

The general consensus is that the best way to manage caching is to serve up content with a very long TTL but to embed versioning information in the URL.

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94