开发者

Why do many sites minify CSS and JavaScript but not HTML? [duplicate]

This question already has answers here: 开发者_开发百科 Closed 12 years ago.

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?


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).


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜