Expand my Community achievements bar.

Minify HTML ins AEM 6.5

Avatar

Level 2

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.

3 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);