0

We are running a Java/Spring MVC application with Tomcat 6 as the servlet container and Apache 2.2 as the web server.

We recently deployed a new rebrand that has completely overhauled the look and feel of the site.

We appended the ?v=1 parameter to the resources in the head section but we are still getting some issues with old cached resources causing problems.

Is there something we can set in tomcat or apache that will ensure fresh copies get pulled or do we need to use the revision number to force a client to grab a new version of resources?

anataliocs
  • 9,595
  • 6
  • 50
  • 63
  • If you want to guarantee the resources get reloaded, your only real option is to add a revision number to the filename. Query strings generally work, but sometimes they don't. Apache is irrelevant because the caching is a client side issue. – Christian Jan 11 '13 at 16:17
  • Is there a quick and dirty solution which I can implement immediately and then a longer term more ideal solution that would allow caching but still ensure fresh resources are pulled when needed? – anataliocs Jan 11 '13 at 16:23
  • Quick and dirty, change the filename. Long term - write a deployment script that generates new filenames on changed resources, or something like that. [This article](http://www.particletree.com/notebook/automatically-version-your-css-and-javascript-files/) documents one such process in PHP, you should be able to write something similar. – Christian Jan 11 '13 at 16:26
  • Maybe this will be also useful to you: http://stackoverflow.com/a/2068407/814702 – informatik01 Jan 11 '13 at 21:15

2 Answers2

1

You can try to use meta tags, like this:

<meta http-equiv="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT" />
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">

But, I realized that IE9 seems to be ignoring these meta tags in one of my web projects. So, instead of using the meta tags, I appended some random number to the url, like ?v=1235324324... you could use either Math.random() or Date.getTime(), basically something unique. You want to make sure the value for v is different every time instead of using value 1 all the time.

limc
  • 36,786
  • 19
  • 95
  • 142
  • well, it's ok to use 1 when something else is used with the next update of the file - e.g. when the cached version "1" of the page should not be requested any more. In fact, I'd advise against Math.random(). And if it's the timestamp then it's rather the timestamp of the current file - e.g. it will change when the file is updated. – Olaf Kock Jan 11 '13 at 22:16
0

AFAIK there is no way to disable a browser cache without developer tools.

Although, you can disabling browser caching is by HTTP headers from the webapp itself. The HTTP 1.1 "Cache-Control" header alone should suffice for any post-year-2000 browser. But for extra protection the server can emit both HTTP 1.1 "Cache-Control" and HTTP 1.0 "Expires" headers together.

enkor
  • 7,389
  • 2
  • 29
  • 49
  • is this the correct meta tag I should be appending? – anataliocs Jan 11 '13 at 16:22
  • The file is already cached though, so any changes made to the file will be ignored. Also, disabling the cache altogether is a poor decision, the cache is there for a reason. – Christian Jan 11 '13 at 16:24
  • I agree, the cache is there for performance. Users will need to refresh manually as they are at the mercy of the client/ browser. – enkor Jan 11 '13 at 16:47