Wednesday, July 30, 2014

How to Remove White Space From Generated HTML In CQ (Or In general)

Use Case: There are a lot of white spaces in generated Output of CQ increasing size of page and decreasing load time.

Solutions:

Option 1: You can use trimDirectiveWhitespaces directive in jsp response. something like

<%@page  trimDirectiveWhitespaces="true"%>

Problem: Using this directive can cause white space to be removed from taglibs for same property. To avoid this issue make sure that you manually add space there. For example if you have tag lib like <Something class="${test1} ${test2}" class2="test"> replace it with <Something class="${test1} ${space} ${test2}" class2="test"> where ${space} is actual space " "
This approach might not work with Slighly framework.

Option 2: Use %><% tags to start and end scriplets tag and in between html tag

Problem: Code very hard to read and not pretty.

Option 3: Create your own tag library and using html parser remove white spaces during run time. for that check http://www.cqblueprints.com/tipsandtricks/jsp-custom-tag-lib.html

And code to remove White space would be



Problem: Maintenance of your own tag library. Can Miss Some condition. Have to wrap up your code with tag lib.

Option 4 (Preferred): Use Google Page Speed Module at apache.

Link to Module: https://developers.google.com/speed/pagespeed/module
Link to All available Filters for Module: https://developers.google.com/speed/pagespeed/module/config_filters
Instruction of how to install and build: https://developers.google.com/speed/pagespeed/module/download

Steps to integrate it in Dispatcher:

1) Create Apache module using step above. This will give you mod_pagespeed.so and mod_pagespeed_ap24.so

2) Move this file to <Apache location>/modules

3) Change permission (to daemon:daemon (Or Your Apache User)) and permission level (766) using chown and chmod command

4) Open conf/httpd.conf and add following line (If some include is already there ignore that)
Include <Apache Location>/conf/pagespeed.conf

5) Create a folder called <Doc Root>/mod_pagespeed/

6) Add pagespeed.conf under <Apache Location>/conf
* Make sure that All paths mentioned in conf file exist.

7) Restart Apache


Problem:
1) Only Apache module is available. If you are using IIS or any other web server then there is no module yet.
2) You might have to do build distribution for your own OS if above module build does not work (One attached here is build for Red Hat Linux).
3) When you upgrade your Apache make sure to upgrade your module as well. If there is no distribution for newer version of apache, then also you are out of luck.


Note: Test above methods before using them in production. Feel free to ask any question you have.

4 comments:

  1. Packers And Movers Bangalore Local Household Shifting Service, Get Free Best Price Quotes Local Packers and Movers in Bangalore List, Compare Charges, Save Money And Time at
    Local Packers And Movers Bangalore

    ReplyDelete
  2. Nice information. Thanks for sharing this informative blog with us. I really need this type of blog and I’m so lucky to found this.
    packers and movers in pathankot

    ReplyDelete




  3. The agency follows a strict selection procedure and each and every female interested in joining us need to meet the essential conditions and fulfill the eligibility criteria. Russian Escorts in Jaipur we have and There is a long list of elite customers who has blind faith in us. It is because of the uncompromising terms and conditions that we strictly follow to ensure highest level of privacy. Check our other Services...
    Russian Escorts in Jaipur
    Russian Escorts in Jaipur
    Russian Escorts in Jaipur
    Russian Escorts in Jaipur
    Russian Escorts in Kolkata

    ReplyDelete
  4. I would like to thanks for share high-valuable article to cover the very interesting topic. And I hope everyone loves this topic. mechatronics assignment Australia

    ReplyDelete