Search code examples
javascripthtmlcache-control

Prevent cache for scripts from another domain


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.


Solution

  • 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.