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>
    <filter-name>Header Filter</filter-name>
    <filter-class>com.liferay.portal.kernel.servlet.PortalClassLoaderFilter</filter-class>
    <init-param>
        <param-name>filter-class</param-name>
        <param-value>com.liferay.portal.servlet.filters.header.HeaderFilter</param-value>
    </init-param>
    <init-param>
        <param-name>Cache-Control</param-name>
        <param-value>max-age=172801, public</param-value>
    </init-param>
    <init-param>
        <param-name>Expires</param-name>
        <param-value>172801</param-value>
    </init-param>
</filter>

….

<filter-mapping>
    <filter-name>Header Filter</filter-name>
    <url-pattern>*.png</url-pattern>
</filter-mapping>

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:

image

A quick internet searched showed me the source code for HeaderFilter, and another quick search found this logged issue: http://issues.liferay.com/browse/LEP-5895. 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!

Advertisements
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s