March 22, 2020
Estimated Post Reading Time ~

Html Minification In AEM

In Order To Improve Performance, Sometimes You May Want To Minify HTML. However, There Is No Out Of The Box Functionality Within AEM For The Same.
There Is An Option To Have Gzip Enabled On Dispatcher Which Does A Great Job Of Compression.

If We Need Few More Bytes Of Compression We Can Go For HTML Compression
We Can Write A Custom Filter To Minify The HTML And This Is How The Filter Config Looks Like:

The Below Is The Rough Code For Html Compression.
Import Java.Io.IOException;
Import Java.Util.Map;
Import Javax.Servlet.Filter;
Import Javax.Servlet.FilterChain;
Import Javax.Servlet.FilterConfig;
Import Javax.Servlet.ServletException;
Import Javax.Servlet.ServletRequest;
Import Javax.Servlet.ServletResponse;
Import Javax.Servlet.Http.HttpServletResponse;
Import Org.Apache.Felix.Scr.Annotations.Activate;
Import Org.Apache.Felix.Scr.Annotations.Deactivate;
Import Org.Apache.Felix.Scr.Annotations.Property;
Import Org.Apache.Felix.Scr.Annotations.PropertyUnbounded;
Import Org.Apache.Felix.Scr.Annotations.Sling.SlingFilter;
Import Org.Apache.Felix.Scr.Annotations.Sling.SlingFilterScope;
Import Org.Apache.Jackrabbit.Oak.Commons.PropertiesUtil;
Import Org.Apache.Sling.Api.SlingHttpServletRequest;
Import Org.Apache.Sling.Api.SlingHttpServletResponse;
Import Org.Apache.Sling.Api.Resource.Resource;
Import Org.Osgi.Service.Component.ComponentContext;
Import Com.Capella.Website.Common.CommonLogger;
Import Com.Googlecode.Htmlcompressor.Compressor.HtmlCompressor;

/**
* This Class HtmlCompressionFilter Will Have Two Options In Config To Enable
* And Disable Html Compression. We Can Also Provide The Paths Where We Need To
* Add Compression.
*/
@SlingFilter(Label = "HTML Compression Filter", Description = "HTML Compression Filter", Metatype = True, Order = 0, Scope = SlingFilterScope.REQUEST)
Public Class HtmlCompressionFilter Implements Filter {
Private HtmlCompressor Compressor;

@Property(Label = "Enable Html Compression", BoolValue = False, Description = "Please Check The Property To Enable Html Compression")
Public Static Final String HTML_COMPRESSION_ENABLED = "Html.Compression.Enabled";

@Property(Value = {
"/Content/Project/En"
}, Unbounded = PropertyUnbounded.ARRAY, Label = "Compress Html Paths", Cardinality = 50, Description = "Please Enter The Paths Where You Need To Compress The Html")
Private Static Final String HTML_COMPRESSION_PATHS = "Html.Compression.Paths";

Private Boolean IsHtmlCompressionEnabled;

Private String[] HtmlCompressionPaths;

@Override
Public Void Init(FilterConfig FilterConfig) Throws ServletException {
Compressor = New HtmlCompressor();
Compressor.SetCompressJavaScript(True);
Compressor.SetCompressCss(True);
}

@Override
Public Void DoFilter(ServletRequest Request, ServletResponse Response, FilterChain Chain)
Throws IOException, ServletException {
If (!(Request Instanceof SlingHttpServletRequest) || !(Response Instanceof SlingHttpServletResponse)) {
Chain.DoFilter(Request, Response);
Return;
}
Final SlingHttpServletRequest SlingRequest = (SlingHttpServletRequest) Request;
Final Resource Resource = SlingRequest.GetResource();
String RequestPath = Resource.GetPath() + ".Html";
If (IsHtmlCompressionEnabled && StartsWithInArr(HtmlCompressionPaths, RequestPath)) {
CharResponseWrapper ResponseWrapper = New CharResponseWrapper((HttpServletResponse) Response);
Chain.DoFilter(Request, ResponseWrapper);
String ServletResponse = ResponseWrapper.ToString();
Response.GetWriter().Write(Compressor.Compress(ServletResponse));
} Else {
Chain.DoFilter(Request, Response);
}
}

Public Boolean StartsWithInArr(String[] HtmlCompressionPaths, String RequestPath) {
Boolean Flag = False;
For (Int I = 0; I < HtmlCompressionPaths.Length; I++) {
If (RequestPath.StartsWith(HtmlCompressionPaths[I])) {
Flag = True;
Break;
}
}
Return Flag;
}

@Override
Public Void Destroy() {}

@Activate
Protected Void Activate(Final Map < String, String > Properties) {
This.IsHtmlCompressionEnabled = PropertiesUtil.ToBoolean(Properties.Get(HTML_COMPRESSION_ENABLED), False);
This.HtmlCompressionPaths = PropertiesUtil.ToStringArray(Properties.Get(HTML_COMPRESSION_PATHS));
}

@Deactivate
Protected Void Deactivate(ComponentContext ComponentContext) {}
}
CharResponseWrapper.Java
Import Java.Io.CharArrayWriter;
Import Java.Io.PrintWriter;
Import Javax.Servlet.Http.HttpServletResponse;
Import Javax.Servlet.Http.HttpServletResponseWrapper;

Public Class CharResponseWrapper Extends HttpServletResponseWrapper {
Private Final CharArrayWriter Output;
@Override
Public String ToString() {
Return Output.ToString();
}

Public CharResponseWrapper(HttpServletResponse Response) {
Super(Response);
Output = New CharArrayWriter();
}

@Override
Public PrintWriter GetWriter() {
Return New PrintWriter(Output);
}

Dependencies Required:
<Dependency>
<GroupId>Com.Googlecode.Htmlcompressor</GroupId>
<ArtifactId>Htmlcompressor</ArtifactId>
<Version>1.4</Version>
</Dependency>
<Dependency>
<GroupId>Com.Yahoo.Platform.Yui</GroupId>
<ArtifactId>Yuicompressor</ArtifactId>
<Version>2.4.6</Version>
</Dependency>

In The Above Dependencies, We Need To Embed Htmlcompressor Dependency As We Don’t Have In It AEM.
We Can Either Manually Install The Bundle Or Embed It From Pom File.



By aem4beginner

1 comment:

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