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.
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.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test.wemblog | |
import javax.servlet.jsp.tagext.BodyContent; | |
import javax.servlet.jsp.tagext.BodyTagSupport; | |
import java.io.IOException; | |
import javax.servlet.jsp.JspWriter; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* wemblog:removeWhitespace tag removes whitespace between HTML elements | |
*/ | |
public class RemoveWhitespace extends BodyTagSupport | |
{ | |
private static final Logger LOGGER = LoggerFactory.getLogger(RemoveWhitespace.class); | |
private static final String REGEX_WHITESPACE_BETWEEN_HTML = "[>]{1}\\s+[<]{1}"; | |
private static final String REGEX_REPLACE_BETWEEN_HTML = "><"; | |
public void setBodyContent(BodyContent bc) | |
{ | |
super.setBodyContent(bc); | |
} | |
@Override | |
public int doAfterBody() | |
{ | |
try | |
{ | |
BodyContent bodyContent = super.getBodyContent(); | |
JspWriter out = bodyContent.getEnclosingWriter(); | |
String html = bodyContent.getString(); | |
out.print(html.replaceAll(REGEX_WHITESPACE_BETWEEN_HTML, REGEX_REPLACE_BETWEEN_HTML)); | |
} | |
catch (IOException e) | |
{ | |
LOGGER.debug("wemblog:removeWhitespace error: " + e.getMessage()); | |
} | |
return SKIP_BODY; | |
} | |
} | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1"> | |
<tlib-version>5.4.0-SNAPSHOT</tlib-version> | |
<short-name>wemblog</short-name> | |
<uri>http://www.wemblog.com</uri> | |
<tag> | |
<name>removeWhitespace</name> | |
<tagclass>com.test.wemblog.RemoveWhitespace</tagclass> | |
<body-content>scriptless</body-content> | |
<info>Remove Whitespace</info> | |
</tag> | |
</taglib> |
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.