Tomcat is setting no-cache for JS and CSS file

17,451

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

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

Solution 3

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

Share:
17,451

Related videos on Youtube

bobber205
Author by

bobber205

Graduated from a tech college with a BS in Software Engineering.

Updated on May 30, 2022

Comments

  • bobber205
    bobber205 almost 2 years

    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
    bobber205 about 13 years
    We are already gzipping CSS and Javascript files. We're also considering minifying our JS as well.
  • bobber205
    bobber205 about 13 years
    We are not using Apache or at least I don't have access to do on production machines.
  • bobber205
    bobber205 about 13 years
    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 .
  • duffymo
    duffymo about 13 years
    Tomcat ships with a compression filter; I'd recommending using that. And a cache filter? Are you worried about thread safety? I would be.