April 1, 2020
Estimated Post Reading Time ~

How to remove white space from generated HTML in CQ (Or In general)

Use Case: There are a lot of white spaces in the generated Output of CQ increasing the size of the 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 removes 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

package com.test.
aem4beginner
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;
/**
aem4beginner: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("aem4beginner: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>
aem4beginner</short-name>
<uri>http://www.
aem4beginner.com</uri>
<tag>
<name>removeWhitespace</name>
<tagclass>com.test.
aem4beginner.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 an Apache module using the 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 the 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 the above module build does not work (One attached here is built for Red Hat Linux).
3) When you upgrade your Apache make sure to upgrade your module as well. If there is no distribution for a newer version of apache, then also you are out of luck.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.