问题
Wikipedia defines minification as...
[...] the process of removing all unnecessary characters from source code without changing its functionality. These unnecessary characters usually include white space characters, new line characters, comments, and sometimes block delimiters, which are used to add readability to the code but are not required for it to execute.
I'm currently doing this to my HTML, CSS and Javascript in order to save bandwidth, but someone told me that he remembers a browser misbehaving when there's no white space between certain tags (namely ul
and li
items). Is this true?
Are there any notable browsers, proxies, or other user agents that misbehave when dealing with minified code?
Other than losing readability when viewing the source, is there any other downside to minification?
回答1:
someone told me that he remembers a browser misbehaving when there's no white space between certain tags (namely ul and li items). Is this true?
Yes, in certain circumstances, like if the elements are set to display inline-block
or inline
.
The following two lists will render differently, because of the whitespace between the <li>
elements:
html
<ul>
<li>simple</li>
<li>list</li>
</ul>
<ul><li>minified</li><li>list</li></ul>
css
li {
display: inline-block;
background: blue;
color: white;
}
rendered output
http://jsfiddle.net/Uwv3e/
回答2:
Minification removes maintainability for the sake of... usually about 4-8kb of savings on a site size. You can get more savings by compressing a single jpg on the site and removing the image's meta-data.
Unless you're building a website that has a MASSIVE amount of pages and subpages and templates and over 5,000 lines of CSS and JS, you're going to find minification is a waste of effort, especially when maintenance comes to play and you have to keep unminified versions of files floating around just to do fixes, minify, overwrite the minified files with the new version, pray the next guy that maintains the site uses your same workflow and doesn't make changes to the minfiied CSS file, then when you come back in and wipe out his changes...
I bring this up because I've seen this happen. I've done GTmetrics and Pingdom scores on sites pre and post minification and the score and load speed barely changes enough to make it worth it.
I've always called minification "Spending dollars to save pennies". Your efforts can be better spent elsewhere on a dev project.
回答3:
Is minifying your HTML, CSS, and Javascript a bad idea? Yes of course!
A good idea is, however, to minify your CSS, and Javascript
Why?
- Minification works by (a) replacing human-readable variable names for compact names, like "mySuperTollesFunctionCall()" to "m()" and by (b) Removing Whitespaces
- Your JavaScript you can benefit from both (a) and (b)
- Your CSS can only benefit from (b)
- Your HTML code, in theory could benefit only from (b) but that is just not worth the try.
Any simple good HTML editor can nice-format your HTML file for you. Take care of whitespaces inside "pre" blocks, gives you hint on W3C compliance, etc, etc, etc.
If you're worried about the size of your HTML file... You should enable GZIP compression of static files in your web server, most living fossils will handle it, and your customers will appreciate it.
http://www.schroepl.net/projekte/mod_gzip/browser.htm
回答4:
Some browsers IE versions have problem with minified font-face
declarations, see:
- CSS fonts not loading in IE9
- How to minify CSS with font-face
回答5:
someone told me that he remembers a browser misbehaving when there's no white space between certain tags (namely ul and li items). Is this true?
No that statement is false. This is how browsers should work according to the HTML specification regarding whitespace. Any sequence of whitespace (tabs, newlines, spaces) means the same thing.
Take the following markup:
<ul>
<li>A</li>
<li>B</li>
</ul>
The correct minification of this after also taking into account SGML line break rules stating that the whitespace after the opening tag and before the closing tag can be ignored, then the correct minifcation would be:
<ul><li>A</li> <li>B</li></ul>
If a minification tool gave the following it would be appropriate to say that the minifier was broken.
<ul><li>A</li><li>B</li></ul>
The exception to this rule is PRE tags, which specify preformatted content and browsers are supposed to render all whitespace characters, and I image you'd also need to leave CDATA as is since it's technically not part of the HTML content.
来源:https://stackoverflow.com/questions/23293168/is-minifying-your-html-css-and-javascript-a-bad-idea