Is there a way to minify HTML in Drupal 8, something like the Minify module which is for Drupal 7?
2 Answers
Use spaceless tag. This will not minify the HTML, instead avoids extra whitespace between HTML tags to avoid browser rendering quirks. When cached, can serve better.
In html.html.twig
{% spaceless %}
<!DOCTYPE html>
<html{{ html_attributes }}>
#rest of the code
</html>
{% endspaceless %}
For compression there is a twig extension
Edit: Use Minify Source HTML module which comes for both D7 and D8.
-
After applying this tag Google Page SpeedInsights does not list html minification as a possible improvement anymore. Commented Apr 27, 2017 at 13:53
-
1This tag will only remove extra whitespaces, this will not minify HTML but in some way helps in reducing page size and serves better when cached.– echoCommented Apr 27, 2017 at 16:51
The Advanced CSS/JS Aggregation module has a minify feature:
CSS/JS Compress Submodules. Can minifiy files & inline CSS/JS. Can also turn off core CSS compression for theme development.
-
There is a Advagg feature request for html minification drupal.org/node/2787385 and a sandbox project for a separate module drupal.org/sandbox/almare/2730729 Commented Apr 27, 2017 at 13:57