How do I minify the generated Symfony / Twig HTML to satisfy Google PageSpeed ​​Insight?

When testing my webpage based on Symfony 2.8

Google PageSpeed ​​Insights, I get a warning that the HTML is not minified.

This is true, but Assetic

takes care of minimizing all files CSS

and JS

, the HTML itself is pretty messy.

Google recommends using HTML Minifier , but since this JavaScript tool cannot be used as an extension Twig

, filter, etc., can it?

The Twig

tag document spaceless

makes it clear that this tag is not meant to minify HTML and more:

If you want to create a tag that actually removes any extra whitespace in your HTML string, be warned that this is not as easy as it sounds (think for example textarea or pre tags). Using a third party library like Tidy is probably the best idea.

But then again I don't see how Tidy

one could integrate into templates Twig

etc.

So what is the best way to generate minified HTML output with Symfony

and Twig

?

+3


source to share


1 answer


This is a good question and there is no flagship package, but after a quick search, you have two packages that might help you:

SemaMinifierBundle

This package allows you to minify all your responses in a single config value (automatically to the KernelResponse event) or, on demand, with branch expansion.



But this package is quite old (3 years old) and not ready for Symfony3.

HtmlCompressorBundle

This package is slightly more recent and uses the htmlcompressor library.

+1


source







All Articles