60

Possible Duplicate:
Why minify assets and not the markup?

I have seen a lot of sites using minified CSS and JavaScript to increase website response time but I have never seen any sites use minified HTML. Why would you not want your HTML to be minified?

Community
  • 1
  • 1
Josh Curren
  • 10,171
  • 17
  • 62
  • 73
  • 2
    http://stackoverflow.com/questions/1306792/why-minify-assets-and-not-the-markup – UmYeah Mar 01 '10 at 22:18
  • 3
    @Ally - what is relevant for Google.com's homepage probably doesn't apply to the average site! – Dominic Rodger Mar 01 '10 at 22:18
  • 1
    How is this closed as exact duplicate when there's no link to another question at the top? – Joachim Sauer Mar 01 '10 at 22:29
  • @Joachim - there is now, looks like the OP edited it out. – Dominic Rodger Mar 01 '10 at 22:30
  • 3
    Precisely, @Dominic. He has rolled it back twice now. So have I. Josh, this question *is* a duplicate. Please do not delete the link again. Don't take it personally. The link is there so *others* can find the original question. If you disagree with the judgment of being a duplicate, please *make your case* instead of trying to "hide" the evidence. – Rob Kennedy Mar 01 '10 at 22:33
  • 1
    @Dominic: I was not aware that the link is part of the article and can be edited out. Thanks. – Joachim Sauer Mar 01 '10 at 22:50
  • 1
    Answers posted here are either outdated or do not make sense today. Check my answer here http://stackoverflow.com/a/22446770/1090562 – Salvador Dali Mar 17 '14 at 04:07

5 Answers5

69

Because if you're doing things properly you're serving HTML gzipped anyway, so the low-hanging fruit of HTML minification - whitespace - isn't all that relevant. There aren't lots of easy targets (e.g. variable names) for minification in HTML, which are present in CSS and JavaScript. Much of the content of HTML is the actual content of the page, which probably can't be minified (and, as others have pointed out, will almost certainly vary more frequently than your CSS or JS).

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • 4
    note that most often minify css or javascript include merging files which could not be done with html file for the reasons you give. – Boris Guéry Mar 01 '10 at 22:22
67

I'd guess that most sites have static CSS and Javascript. This means they can be minified just once whenever they are updated. On the other hand, HTML tends to be dynamically generated, which means it would have to be minified on every page request, which is considerably more expensive than minifying static CSS and Javascript files.

Michael Williamson
  • 11,308
  • 4
  • 37
  • 33
  • 5
    Static websites would only need to be minified once. But additionally, dynamically generated html would also be easily minified. When the html content is created, you would just not insert the white space in the first place. It'd actually be faster to produce html without whitespace. – Jeff Vdovjak Mar 17 '16 at 04:14
9

I don't think there is that much room for minification in HTML: You can remove white spaces and line breaks, but essentially, that's about it without actually getting into the page's structure.

JS minification can shorten variable and function names, probably the biggest net profit in terms of saved space. With its fixed set of tags, HTML does not provide that possibility.

The option of gzipping HTML probably eliminates much of the need to minify anyway, especially as it is usually enabled for HTML, while it (unnecessarily) not always is for the CSS and JS file types.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
5

Primarily because Javascript files and CSS stylesheets are often static files that will not change upon deployment. Markup, on the other hand, is often generated on the fly (with database-driven web apps, at least), and the number of "pages" is usually large and dynamic, which makes the benefits of minification more work than it's worth.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
3

Html content being gzipped takes care of most of the compression, minifying on top of that wouldn't accomplish much or save a great deal of bandwidth.

Javascript you can minify as part of the build, the only way this would happen with the entire HTML content would be to minify every piece (what if it's generated?) or to have it minified the whole time (nightmare to work on?)

It's cost vs benefit, cost: marginal bandwidth, benefit: easier to work on, easier to generate, easier to debug, pretty in my source view window.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155