Eleventy: How to Set Up Effective HTML Minification (in 24 Seconds)
Published on Nov聽28, 2025 (updated Jan聽18, 2026), filed under development, html, optimization. (Share this post, e.g., on Mastodon.)
You鈥檙e running an Eleventy site and want to minify your HTML (and any inline CSS and JS)?
Here鈥檚 what you鈥檇 need to do, to minify your output using HTML Minifier Next (HMN) (which does well compared with other minifiers):
1. Install
Install HTML Minifier Next in your project (as a dev dependency):
npm i -D html-minifier-next2. Configure
Add the following to your Eleventy configuration (.eleventy.js or eleventy.config.js):
// Close to other imports:
import { minify } from 'html-minifier-next';
// Near other `eleventyConfig` functions:
eleventyConfig.addTransform('html-minifier-next', async function(content) {
if (this.page.outputPath && this.page.outputPath.endsWith('.html')) {
let minified = await minify(content, {
preset: 'comprehensive',
// Options: https://github.com/j9t/html-minifier-next?tab=readme-ov-file#options-quick-reference
});
return minified;
}
return content;
});And that鈥檚 it for a first setup.
The config uses HMN鈥檚 brand-new presets, whose config is very similar to the config I use in my Eleventy projects. HMN comes with many more options, however, for you to tailor and optimize your HTML output.
If you use Eleventy鈥檚 bundle plugin with bundleHtmlContentFromSelector: "style", it will extract inline CSS before the minifier runs, leaving empty <style> elements. Either disable that option or use external style sheets instead of inline styles.
As for those 24 seconds鈥攜es, that鈥檚 exactly how long it took me to add HMN to the Grease starter, one of three Eleventy projects I tested with. (This isn鈥檛 a contest, just a playful way to indicate setting up HTML minification may be done swiftly.)
About Me
I鈥檓 Jens (long: Jens Oliver Meiert), and I鈥檓 an engineering lead, guerrilla philosopher, and indie publisher. I鈥檝e worked as a technical lead and engineering manager at various companies (e.g., Google); I鈥檓 an active web and tool developer (code optimization, digital defense), a contributor to web standards (like HTML, CSS, WCAG), and a book author (O鈥橰eilly, Frontend Dogma).
I love trying things鈥攊n web development and engineering management, but also in politics and philosophy, where I hold to one idea in particular: that we can only be well if we take good care of everyone. Here on meiert.com I talk about some of my perspectives and experiences. (Please share feedback: Interpret charitably, keep it friendly, but do be critical.)
