0

I have a task to do that I am having a hard time to do. I need to stop caching of JS files. We have a JSP based website (underdevelopment in LAN) which has some JS code too. If I try to set HTTP headers in JSP or in meta tags the JS files still return 304 status code.

Here's my code:

At the beginning of the JSP file -

<% 
   response.setHeader("Cache-Control","no-cache");                
   response.setDateHeader("Expires", 0);
%>

Inside head tag -

 <meta http-equiv="cache-control" content="max-age=0" />
 <meta http-equiv="cache-control" content="no-cache" />
 <meta http-equiv="expires" content="-1" />
 <meta http-equiv="pragma" content="no-cache" />

 <script type='text/javascript' src='./cachetest.js'></script>

I have found a way to reload the JS everytime by passing a random number or time since epoch as parameter as -

<script type='text/javascript' src='./cachetest3.js?id=<%=timeSinceEpoch>'></script>

But this seems to be a very crude way of doing it, is there something, I had a look at fiddler, the JSP or HTML entry will have the correct header and gets 200 status, but if the JS file is not changed I get 304.

What is a good way to do this?

  • Do you wan't to stop caching continuously, or do you wan't to stop caching in the point of new release (ppl can load your modified js files)? If you answer yes to the former, I think you are doing something wrong. Cache is your friend most of the time. – Mauno Vähä Jul 24 '14 at 10:23
  • Yes Mauno, ideally that should help, but there's an issue, the changed JS files are not getting reflected probably due to proxy caching not getting refreshed after deployments. I am really unaware how we refresh JS files in proxy cache. – Bhabani Sankar Mishra Jul 24 '14 at 10:27
  • 1
    I think you should use "asset fingerprinting", it is almost the same as ?id=<%=timeSinceEpoch but instead of version parameter, you actually name your modified js files to contain randomized number. yourfile13213zzza233.js If I recall correctly, style of ?id= or ?v=1 etc is problematic in some configurations and there is a change that they won't invalidate cache.(?) I think here: http://stackoverflow.com/questions/118884/what-is-an-elegant-way-to-force-browsers-to-reload-cached-css-js-files the answer with most votes discusses the same thing what I am talking about. – Mauno Vähä Jul 24 '14 at 10:32
  • ok, yes that makes sense, I can try that out. But is there any way http headers can solve this? – Bhabani Sankar Mishra Jul 24 '14 at 10:35
  • Maybe this is alternative to stuff I specified http://stackoverflow.com/questions/3055268/add-an-expires-or-a-cache-control-header-in-jsp – Mauno Vähä Jul 24 '14 at 10:37
  • Tried the approach in http://stackoverflow.com/questions/3055268/add-an-expires-or-a-cache-control-header-in-jsp already. Doesn't work for the linked JS files. – Bhabani Sankar Mishra Jul 24 '14 at 10:39

0 Answers0