Improving page load with mod_expires and ExpiresFilter in JSF applications

Monday June 24, 2013 ()

Apache 2 mod_expires and Tomcat 7 ExpiresFilter add Cache-Control HTTP header in server responses. Caching allows resources to be saved, or cached by the browser. Once a resource is cached, the browser refer to the locally cached copy instead of having to download it again on subsequent visits to the web page. This reduces round trip time by not having to request for these resources, thus improve page load.

Module mod_expires and ExpiresFilter (a Servlet API port of mod_expires) add Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses and can set to be relative to either the time the source file was last modified, or to the time of the client access.

Tomcat 7 Servlet Filter

Use the Filter if your JSF pages are served directly by Tomcat through the port it is listening to (http://domain.com:8080 for example). It can be configured per application through the web.xml of the application or container wide through the container web.xml. The following web.xml entry adds "Expires" and "Cache-Control: max-age=" headers to images, css and javascript.

<filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <init-param>
        <param-name>ExpiresByType image</param-name>
        <param-value>access plus 1 month</param-value>
    </init-param>     
    <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>modification  plus 3 weeks</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>access plus 1 month 15 days 2 hours</param-value>
    </init-param>
</filter> 

<filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

You can also use the alternate syntax ExpiresDefault. The following directive can be used to make documents expire 1 month after being accessed, by default. Replace all init-param directive from the previous example with below.

<init-param>
    <param-name>ExpiresDefault</param-name>
    <param-value>access plus 1 month</param-value>
</init-param>

Please refer to this Tomcat 7 documentation http://tomcat.apache.org/tomcat-7.0- doc/api/org/apache/catalina/filters/ExpiresFilter.html for more Filter options.

Note that ExpiresFilter may override your com.sun.faces.defaultResourceMaxAge settings depending on the url-pattern of the Filter.

Apache 2 mod_expires module

The Servlet Filter does not work if your JSF pages and servlets are served by Apache. This is the case if you are using mod_jk connector. In this case use mod_expires.

In Debian systems enable mod_expires like below

a2enmod expires

Configuration format of the module is the same as the Servlet Filter. It can be configured in the virtual host, universally in the server configuration, or in your .htaccess.

<IfModule mod_expires.c>
     ExpiresActive on
     ExpiresByType image/gif "access plus 1 month"
     ExpiresByType css/text "modification  plus 1 month 15 days 2 hours"
</IfModule>

or, the alternate syntax,

<IfModule mod_expires.c>
     ExpiresActive on
     ExpiresDefault  "access plus 1 month"
</IfModule>

Restart Apache 2.

Please check the Apache 2 mod_expires documentation here http://httpd.apache.org/docs/current/mod/mod_expires.html for more options.

In the above samples, both in the ExpiryFilter and in mod_expires, references to access may be replaced with modification and vice versa depending on your requirements. See the documentation for more information.

That's it. Good luck.


5,474

Comments (Improving page load with mod_expires and ExpiresFilter in JSF applications)