Recently working for the Sitecore project, the client requested to generate minified HTML for the page so overall payload size reduces and the pages should get good ranking in Google Page Speed.
I found the following two articles which helped me understand how I can process my pages through minification:
Minify HTML in Sitecore, by Anders Laub
NTT Minifier, by NTT Data Sitecore
My heartful thanks to both the developers for providing the idea of minifying the content.
Both of the above processors are working a the request level to minify the page markup on the fly.
Also, both the developers have used very well thought process to avoid minifying the administration pages.
However, I wanted to process my HTML output of each rendering so that processed and minified markup can be cached in the Sitecore HTML cache and should not be processed each time.
To minify the rendered output of each rendering I need to override a processor ‘Sitecore.MarkupMinifier.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer’ which is available under ‘mvc.renderRendering’ pipeline. With help of the available code from both the above articles, I prepared my module which minifies the rendering output and rest will be handled by Sitecore.
In the extended version of ExecuteRenderer, I override the Render method to implement my custom logic where I do check for following before minifying the output of the rendering:
- If the page is requested from Sitecore admin and the URL contains the “/sitecore” keyword, then minification will not occur.
- This custom rendered can be enabled or disabled via settings variable “Sitecore.MarkupMinifier.Enabled”. By default, it is enabled.
- By changing the value of “Sitecore.MarkupMinifier.RemoveHTMLComments” settings variable, one can enable or disable the removal of HTML comments in minified output. By default, it is enabled to remove HTML comments.
- By changing the value of “Sitecore.MarkupMinifier.RemoveWhiteSpaces” settings variable, one can enable or disable the removal of white spaces in the minified output. By default, it is enabled to remove white spaces.
- By changing the value of “Sitecore.MarkupMinifier.RemoveLineBreaks” settings variable, one can enable or disable the removal of line breaks from the minified output. By default, it is enabled to remove line breaks.
- With the settings variable, “Sitecore.MarkupMinifier.ExcludedURLKeywords”, once can setup up the keywords to exclude those pages from minification. Provide the comma separated keywords and those keywords are matched in the URL of the page. If the keyword match is found in the URL of a page, then that page will be excluded from the minification.
Implementing the minification at the rendering level, we do not need to minify the whole page at the request level. Another benefit is the minified HTML is cached in the Sitecore HTML cache which will not go for another round of minification for next request.
You can download the module from here.
Happy site optimization!!!!