An HTML Optimizerās Config for html-minifier
Published on DecĀ 20, 2019 (updated SepĀ 18, 2024), filed under development, html, performance, optimization (feed). (Share this on Mastodon orĀ Bluesky?)
Jad Joubran asked me about my configuration for html-minifier the other week, and in a hurry I pointed him to the config I had worked out for sum.cumo. In my own projects, however, I work with a different, more ambitious setup. Itās this, which Iām just going to throw over the fence:
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
conservativeCollapse: true,
decodeEntities: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
preventAttributesEscaping: true,
processConditionalComments: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true
It translates to the following parameters in the command line:
--collapse-boolean-attributes
--collapse-inline-tag-whitespace --collapse-whitespace
--conservative-collapse --decode-entities --minify-css
--minify-js --minify-urls --no-include-auto-generated-tags
--prevent-attributes-escaping --process-conditional-comments
--remove-attribute-quotes --remove-comments --remove-empty-attributes
--remove-optional-tags --remove-redundant-attributes
--remove-script-type-attributes --remove-style-link-type-attributes
--sort-attributes --sort-class-name --trim-custom-fragments
--use-short-doctype
(As you can tell Iām assuming some knowledge about using html-minifier.)
Defensive Optimization
Occasionally Iād use a less aggressive, more āstableā configuration to optimize HTML code. The following setup, for example, Iāve recently used to āpolishā the in good parts static files of meiert.com:
collapseBooleanAttributes: true,
decodeEntities: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
For the command line:
--collapse-boolean-attributes --decode-entities
--remove-optional-tags --remove-redundant-attributes
--remove-script-type-attributes --remove-style-link-type-attributes
Hereās the full command I use in my structure, if thatās useful:
html-minifier --collapse-boolean-attributes --decode-entities
--remove-optional-tags --remove-redundant-attributes
--remove-script-type-attributes --remove-style-link-type-attributes
--file-ext html --input-dir meiert.com/ --output-dir meiert.com/
ā§ I work with these setups in a mostly manual fashion because for some projects, like UITest.com or The Worldās Highest Website, I intentionally manage everything manually, whereas in others, like HH Kaffee, itās completely automated. For me this makes for a good mix for honing my skills while not wasting too much time when the code base is rather large.
When it comes to html-minifier, I think Juriy (aka kangax) has done extraordinary work with it. As I said in my optional HTML primer I consider it the best and most important tool for HTML optimization. Use it (while keeping your manual HTML skills sharp)!
Update (September 18, 2024)
html-minifier can take significant time to process, as Iāve been learning in my Eleventy setup for Frontend Dogma. To optimize performance, Iāve fine-tuned my configuration only to use what I really needed or didnāt want to risk missing.
Hereās an updated configuration, with brief individual comments. If performance is not a concern, the config above should be preferable. If it is, you could adopt these settings, but the best course of action would be to go through html-minifierās settings and cherry-pick according to your project needs.
collapseBooleanAttributes: true, // just in case, but important
collapseWhitespace: true,
decodeEntities: true,
minifyJS: true, // needed in some places
preventAttributesEscaping: true,
removeComments: true,
removeOptionalTags: true, // just in case I missed something
removeRedundantAttributes: true // just in case I missed something
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a web developer, manager, and author. Iāve been working as a technical lead and engineering manager for companies youāve never heard of and companies you use every day, Iām an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OāReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)