4

So frustrating. :P

Really would like these to be cached on users' browsers but it's setting this.

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0

How do I stop this?

I've tried looking in our settings for anything relating to caching. All default values are used which according to the documentation, that means it's allowed. Are static files like JS and CSS different?

Edit: I've noticed that some JS files are being allowed to cache as Chrome is saying they were "retrieved from cache". No CSS files are however.

bobber205
  • 12,948
  • 27
  • 74
  • 100

3 Answers3

2

You could write your own cache filter, and configure it in your web xml.

Here you find a basic, yet great example of how to implement it.

in your web.xml you declare your filter:

<filter>
    <description>Set HTTP headers for a mapping.</description>
    <filter-name>CacheFilter</filter-name>
    <filter-class>your.package.CacheFilter</filter-class>
    <init-param>
        <description>Adds an expires header to the response</description>
        <param-name>header</param-name>
        <param-value>Expires: Thu, 26 Apr 2012 20:00:00 GMT</param-value>
    </init-param>
</filter>

then map it (apply it to responses):

<filter-mapping>
    <filter-name>CacheFilter</filter-name>
    <url-pattern>*.js</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
    <filter-name>CacheFilter</filter-name>
    <url-pattern>*.css</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

You might also want to use a compress filter (this same way), to reduce the load of data sent from the server. This implementation of a gzip filter works for me for years now (along the cache filter), and never had any problem with them.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • Tomcat ships with a compression filter; I'd recommending using that. And a cache filter? Are you worried about thread safety? I would be. – duffymo Apr 23 '11 at 00:22
1
<FilesMatch "\.(js|css)$">
    ExpiresDefault "now plus 1 week"
</Files>

in your Apache configuration should do the trick, as long as it's somewhere after the Tomcat configuration stuff.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Or you can have a servlet set the cache expiration and last modified headers on the response.

I'd also recommend GZIP compressing JavaScript and CSS. Tomcat ships with a compression filter in its /examples directory that you can wire in for appropriate URLs.

You should combine and minify your JavaScript and CSS for better performance.

All these are recommendations from the YSlow plug-in for Firefox. You can see how effective these measures are using the Firebug plug-in.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • We are already gzipping CSS and Javascript files. We're also considering minifying our JS as well. – bobber205 Apr 21 '11 at 22:43
  • Turns out it was the most obvious place as described here. :D Don't know how I could have missed it hehe. We were setting the options I wanted gone manually ourselves . – bobber205 Apr 22 '11 at 23:48