Tuesday, December 20, 2016

Overwrite Cache-Control headers in Liferay Document Library files

Manage Cache of Liferay document library files.
By default Liferay disable cache for all documents and images in its document library. To enable cache control follow the steps.

DLFileEntry from Document library are downloaded from WebServerServlet, that is adding Cache-Control: private header but no other caching instruction (like max-age or Expires) is appended.

ServletResponseUtil will override the Cache-Control header, no matter what was set before. In this particular case, when trying to modify the header using the HeaderFilter will fail because the portal will override it with a hard-coded value later on.

Overwrite Cache-Control headers settings for tomcat.


  1. Shutdown the Liferay portal serer (Tomcat).
  2. Edit the <tomcat_home>/webapps/ROOT/WEB-INF/liferay-web.xml
  3. Add the following filter mapping:    
        <filter-mapping>
            <filter-name>Header Filter</filter-name>
            <url-pattern>/documents/*</url-pattern>
        </filter-mapping>
       
    I've added it after the following block:
       
        <filter-mapping>
            <filter-name>Header Filter</filter-name>
            <url-pattern>*.png</url-pattern>
        </filter-mapping>
      
                 <filter-mapping>
                    <filter-name>Header Filter</filter-name>
                    <url-pattern>*.jpg</url-pattern>
                 </filter-mapping>

  4. Start the portal again.

Check Header detail in the browser.

Before configuration change 

Open the browser with cache cleaned. Also open network tool using (F12).
Try open the URL which has document library file. 
URL: https://localhost/documents/20623/76583/resw-fty.jpg/6134e354-56b9-43b1-7650-9cc5230e7654?t=1234200776548
Result:  This should have "Cache-Control: private"

After configuration change

Open the browser with cache cleaned. Also open network tool using (F12).
Try open the URL which has document library file. 
URL: https://localhost/documents/20623/76583/resw-fty.jpg/6134e354-56b9-43b1-7650-9cc5230e7654?t=1234200776548
Result: This should have "Cache-Control:max-age=315360000, public"