0

My application uses a javascript file that is hosted on a sub-domain and its used in pages across many other sub-domains. In the pages that use this js file, I have asked the developers to use no-cache metas so that any updates from my end are reflected on those pages. The below are the tags they use in their page,

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

Ref: https://stackoverflow.com/a/1341133/1787599

These meta tags are placed right below title tag inside the head. When I updated the JS file, the changes were not reflected when the pages which use the file are viewed. In Chrome developer tools->Network tab, I can only see 304 (Not Modified) for most of the files including the JS. While this question is answered a lot of times, I still could not figure out where the problem is.

Note: This https://stackoverflow.com/a/11724596/1787599 answer is working. It doesn't matter what meta is used in html, the .htaccess file seems to override that.

Community
  • 1
  • 1
NEO
  • 1,961
  • 8
  • 34
  • 53

2 Answers2

2

The meta tags only apply to the html page, not loaded resources. Instead, respond with the header Cache-Control: no-store for the js files.

Further reading: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Avoiding_caching

Note that the script itself will have to be updated so that the browser requests it, along with the above header.

Mooseman
  • 18,763
  • 14
  • 70
  • 93
0

When you edit the file don't forget to include a parameter at the end of the URL, like this:

<script src="myscripts.js?1"></script>

Then you can increment the number each time you make changes.

This way you'll force to update the cache and you'll be sure that every user will load the most recent version.

PD: The solution proposed above is not optimal because users will load the file every time they load the site –you are disabling the cache, and cache is very useful–.

fran.tr
  • 1
  • 1
  • I tried that and it does not work and as Mooseman pointed out its not an elegant solution. The best way to force no cache is to modify .htaccess – NEO Jul 24 '14 at 19:16
  • @fran.tr In most cases, it would be better to add a url parameter, but since he's not administrating the other sites that load the script, it's not feasible. – Mooseman Jul 24 '14 at 19:30