Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Minify HTML ins AEM 6.5

Avatar

Level 3

Hi Guys,
I want to minify HTML in my project. When I do view source code for page ,in the HTML generated, I can see lot of whitespace. I need a way to minify the HTML files (in a way that after minifcation , the size of the HTML should be 30% less than original HTML file.)

note: The scenario mentioned in bracket is secondary, Primary aim is to minify the HTML files
(screenshot below)
adi411_0-1719926855309.png


Thank You,
Aditya Srivastava

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@adi411 - I believe you are doing email development in AEM and sending the HTML to comms platform.

 

The comms platform can request the AEM page by calling a custom servlet. In the servlet you can then do HttpGet of the page path (which is requested) and the compress and send as response.

 

You need to add maven dependency for html compressor:

<dependency>
    <groupId>com.googlecode.htmlcompressor</groupId>
    <artifactId>htmlcompressor</artifactId>
    <version>1.5.3</version>
    <scope>provided</scope>
</dependency>

 

View solution in original post

7 Replies

Avatar

Community Advisor

Hi, 

Please check this blog which explains how to enable the mod_deflate from Apache (dispatcher) https://www.albinsblog.com/2018/09/improve-performance-of-adobe-experience.html which is responsible for compressing other types of documents.

Also check the official documentation about the same: https://experienceleague.adobe.com/en/docs/experience-manager-dispatcher/using/configuring/dispatche... 


Hope this helps



Esteban Bustamante

Avatar

Community Advisor

@adi411 - While compression can be achived from Dispatcher by enabling gzip as mentioned by others using Apache Dispatcher modules. One other way is to use HTML Compression libraries which gives you flexibility to remove whitespaces, clean CSS, etc.

Check Google's HTML Compressor Java library - https://code.google.com/archive/p/htmlcompressor/

 

A custom servlet can be written to produce the output:

HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
String output = httpResponse.getEntity().toString()

HtmlCompressor compressor = new HtmlCompressor();
String compressedHtml = compressor.compress(html);

response.setContentType(ContentType.TEXT_HTML.getMimeType());
response.setStatus(HttpStatus.SC_OK);
response.getWriter().write(compressedHtml);

 

Avatar

Level 3

Hi @Jineet_Vora  - Can you explain a bit more on the custom servlet part?

 https://code.google.com/archive/p/htmlcompressor/  --> I visited this link and there I found Maven Integration section, but how to use it  those links result in  404.

Avatar

Correct answer by
Community Advisor

@adi411 - I believe you are doing email development in AEM and sending the HTML to comms platform.

 

The comms platform can request the AEM page by calling a custom servlet. In the servlet you can then do HttpGet of the page path (which is requested) and the compress and send as response.

 

You need to add maven dependency for html compressor:

<dependency>
    <groupId>com.googlecode.htmlcompressor</groupId>
    <artifactId>htmlcompressor</artifactId>
    <version>1.5.3</version>
    <scope>provided</scope>
</dependency>

 

Avatar

Community Advisor

@adi411 

In addition to responses shared by others, In case if you are using CDN such as Cloudflare / Akamai

You can try to enable HTML minification in CDN configuration

https://developers.cloudflare.com/speed/optimization/content/auto-minify/

 

Avatar

Community Advisor

Hi @adi411 
What are the reasons for minifying HTML? What benefits you are targeting? If the goal is solely to deliver faster content to end users, you can use Gzip compression?

Gzip compression works by compressing the files on the server side and decompressing them on the client side, further reducing load times and improving user experience.
https://blog.hubspot.com/website/gzip-compression 

 



Arun Patidar