Liferay Theme Files Without a Cache-Control Header?!

I ran into an interesting situation with our web application as we started to do some testing where our portal server (Liferay 5.1.2 on tomcat 6) was on the west coast and the testers on the east coast. The application was slower than we expected, compared to testing with a local server. (We did expect it to be slower, just not as slow as it was.) Some analysis with Fiddler quickly pointed out that our application was requesting a bunch of static resources (CSS, JS, images) from the server which were returning HTTP 304 (not modified) codes.  Just making those requests across the country was adding up to about 2.5 second on our page load time!! (BTW, this is with IE8) Those files were not in our application, but in the Liferay theme that we deployed. Since we have a custom theme, I looked at the source code to see what it was serving up. The web.xml file for our theme looks like this:

    <filter-name>Header Filter</filter-name>
        <param-value>max-age=172801, public</param-value>


    <filter-name>Header Filter</filter-name>

What’s shown here should cause both the Cache-Control and Expires HTTP headers to be added to all png images. This is not what I was seeing. This is what I did see, with no Cache-Control header:


A quick internet searched showed me the source code for HeaderFilter, and another quick search found this logged issue: To resolve LEP-5895, this code was added to the filter:

// LEP-5895

boolean addHeader = true;

if (name.equalsIgnoreCase(HttpHeaders.CACHE_CONTROL)) {
    HttpServletRequest httpReq = (HttpServletRequest)req;

    HttpSession ses = httpReq.getSession(false);

    if ((ses == null) || ses.isNew()) {
        addHeader = false;

if (addHeader) {
    httpRes.addHeader(name, value);

Well, for a theme, addHeader will always be false, meaning that the Cache-Control header will never get added. Our application will always make requests for these static resources, even if they’re in the cache.

The Solution?

Since Liferay is now at version 6, I’m not expecting that there will be a change to Liferay 5 to address this. And, since this is actually our own theme, we plan to ship our own servlet filter with the theme. Our filter will add the Cache-Control header, and hopefully we can save those 2.5 seconds!

This entry was posted in Java Development, Miscellaneous, Web Development. Bookmark the permalink.

2 Responses to Liferay Theme Files Without a Cache-Control Header?!

  1. Marcin says:

    Hi. Did you find solution for liferay 5.2.x?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s