HTML 缩小?
有没有一个在线工具,我们可以输入页面的 HTML 源代码并缩小代码?
我会对 aspx 文件执行此操作,因为让网络服务器对它们进行 gzip 压缩并不是一个好主意......
Is there a online tool that we can input the HTML source of a page into and will minify the code?
I would do that for aspx files as it is not a good idea to make the webserver gzip them...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
也许可以尝试 HTML 压缩器,这里有一个前后表,显示了它的功能(包括 Stack溢出本身):
它具有许多用于优化页面的选项,包括脚本最小化(ompressor、Google Closure Compiler、您自己的压缩器)在那里它会是安全的。 默认选项集相当保守,因此您可以从它开始并尝试启用更激进的选项。
该项目有非常完善的记录和支持。
Perhaps try HTML Compressor, here's a before and after table showing what it can do (including for Stack Overflow itself):
It features many selections for optimizing your pages up to and including script minimizing (ompressor, Google Closure Compiler, your own compressor) where it would be safe. The default option set is quite conservative, so you can start with that and experiment with enabling more aggressive options.
The project is extremely well documented and supported.
不要这样做。 或者更确切地说,如果您坚持这样做,请在完成任何更重要的网站优化后再进行。 这项工作的成本/收益很可能可以忽略不计,尤其如果您计划手动使用在线工具来处理每个页面。
使用 YSlow 或 页面速度来确定您真正需要做什么来优化您的页面。 我的猜测是,减少 HTML 字节数不会是您网站的最大问题。 压缩、缓存管理、图像优化等更有可能对网站的整体性能产生更大的影响。 这些工具将向您展示最大的问题是什么——如果您已经解决了所有这些问题,并且仍然发现 HTML 缩小有显着的不同,那就继续吧。
(如果您确定要这样做,并且使用 Apache httpd,则可以考虑使用 mod_pagespeed 并打开一些选项来减少空格等,但请注意风险。)
Don't do this. Or rather, if you insist on it, do it after any more significant site optimizations are complete. Chances are very high that the cost/benefit for this effort is negligible, especially if you were planning to manually use online tools to deal with each page.
Use YSlow or Page Speed to determine what you really need to do to optimize your pages. My guess is that reducing bytes of HTML will not be your site's biggest problem. It's much more likely that compression, cache management, image optimization, etc will make a bigger difference to the performance of your site overall. Those tools will show you what the biggest problems are -- if you've dealt with them all and still find that HTML minification makes a significant difference, go for it.
(If you're sure you want to go for it, and you use Apache httpd, you might consider using mod_pagespeed and turning on some of the options to reduce whitespace, etc., but be aware of the risks.)
以下是对您的问题的简短回答:您应该缩小 HTML、CSS、JS。 有一个易于使用的工具,称为 grunt。 它允许您自动执行许多任务。 其中JS、CSS、HTML 缩小、文件串联和许多其他。
这里写的答案非常过时,甚至有时没有意义。 与 2009 年相比,很多事情都发生了变化,所以我会尽力正确回答这个问题。
简短的回答 - 你绝对应该缩小 HTML。 今天这很简单,并且可以提供大约 5% 的加速。 对于更长的答案,请阅读整个答案
回到过去,人们手动缩小 css/js (通过通过某些特定工具运行它来缩小它)。 使该过程自动化有点困难,并且肯定需要一些技能。 知道现在很多高级网站都没有使用 gzip(这很简单),人们不愿意缩小 html 是可以理解的。
那么为什么人们缩小 js,而不是 html? 做以下事情:
var isUserLoggedIn
到var a
)当你缩小 JS 时,你会 即使在过去也有很大的进步。 但是在html中你无法将长名称更改为短名称,而且在那段时间几乎没有什么可评论的。 所以剩下的唯一的事情就是删除空格和换行符。 这仅提供了少量的改进。
这里写的一个错误的论点是,因为内容是通过 gzip 提供的,所以缩小没有意义。这是完全错误的。 是的,gzip 减少缩小的改进是有道理的,但是为什么要 gzip 注释、空格(如果你可以正确修剪它们)并且 gzip 只是重要的部分。 这就像您有一个要存档的文件夹,其中有一些您永远不会使用的垃圾,并且您决定将其压缩而不是清理并压缩它。
为什么缩小没有意义的另一个论点是它很乏味。也许这在 2009 年是正确的,但在这之后出现了新的工具。 现在您不需要手动缩小标记。 对于像 Grunt 这样的东西,安装 很简单grunt-contrib-htmlmin (依赖于 @kangax 的 HTMLMinifier)并配置它来缩小你的 html 。 您只需要花 2 个小时来学习 grunt 并配置所有内容,然后一切都会在不到一秒的时间内自动完成。 听起来 1 秒(您甚至可以使用 grunt-contrib-watch 自动执行任何操作)并非如此大约有 5% 的改进(即使使用 gzip)。
还有一个论点是 CSS 和 JS 是静态的,而 HTML 是由服务器生成的,因此您无法对其进行预缩小。 2009 年也是如此,但目前更多和更多 站点看起来像一个单页应用程序,其中服务器很薄,客户端正在执行所有路由、模板和其他逻辑。 所以服务器只给你 JSON 并且客户端渲染它。 这里有很多页面的 html 和不同的模板。
结束我的想法:
Here is a short answer to your question: you should minify your HTML, CSS, JS. There is an easy to use tool which is called grunt. It allows you to automate a lot of tasks. Among them JS, CSS, HTML minification, file concatenation and many others.
The answers written here are extremely outdated or even sometimes does not make sense. A lot of things changed from old 2009, so I will try to answer this properly.
Short answer - you should definitely minify HTML. It is trivial today and gives approximately 5% speedup. For longer answer read the whole answer
Back in old days people were manually minifying css/js (by running it through some specific tool to minify it). It was kind of hard to automate the process and definitely required some skills. Knowing that a lot of high level sites even right now are not using gzip (which is trivial), it is understandable that people were reluctant in minifying html.
So why were people minifying js, but not html? When you minify JS, you do the following things:
var isUserLoggedIn
tovar a
)Which gave a lot of improvement even at old days. But in html you were not able to change long names for short, also there was almost nothing to comment during that time. So the only thing that was left is to remove spaces and newlines. Which gives only small amount of improvement.
One wrong argument written here is that because content is served with gzip, minification does not make sense. This is totally wrong. Yes, it makes sense that gzip decrease the improvement of minification, but why should you gzip comments, whitespaces if you can properly trim them and gzip only important part. It is the same as if you have a folder to archive which has some crap that you will never use and you decide to just zip it instead of cleaning up and zip it.
Another argument why it pointless to do minification is that it is tedious. Maybe this was true in 2009, but new tools appeared after this time. Right now you do not need to manually minify your markup. With things like Grunt it is trivial to install grunt-contrib-htmlmin (relies on HTMLMinifier by @kangax) and to configure it to minify your html. All you need is like 2 hours to learn grunt and to configure everything and then everything is done automatically in less than a second. Sounds that 1 second (which you can even automate to do nothing with grunt-contrib-watch) is not really so bad for approximately 5% of improvement (even with gzip).
One more argument is that CSS and JS are static, and HTML is generated by the server so you can not pre-minify it. This was also true in 2009, but currently more and more sites are looking like a single page app, where the server is thin and the client is doing all the routing, templating and other logic. So the server is only giving you JSON and client renders it. Here you have a lot of html for the page and different templates.
So to finish my thoughts:
我编写了一个网络工具来缩小 HTML。 http://prettydiff.com/?m=minify&html
此工具使用这些规则进行操作:
样式内的所有内容
标记被假定为 CSS,并被缩小为此类script
标记内的所有内容均被假定为 JavaScript,除非提供了不同的媒体类型,然后被缩小为此类I wrote a web tool to minify HTML. http://prettydiff.com/?m=minify&html
This tool operates using these rules:
style
tag is presumed to be CSS and is minified as suchscript
tag is presumed to be JavaScript, unless provided a different media type, and then minified as such这对我有用:
http://minify.googlecode.com/git /min/lib/Minify/HTML.php
它不是一个已经可用的在线工具,但作为一个简单的 PHP 包含,它很容易,您可以自己运行它。
不过,我不会保存压缩文件,如果确实需要的话,可以动态执行此操作,并且启用 Gzip 服务器压缩始终是一个更好的主意。
我不知道这在 IIS/.Net 中有多复杂,但在 PHP 中这就像在全局包含文件中添加一行一样简单
This worked for me:
http://minify.googlecode.com/git/min/lib/Minify/HTML.php
It's not an already available online tool, but being a simple PHP include it's easy enough you can just run it yourself.
I would not save compressed files though, do this dynamically if you really have to, and it's always a better idea to enable Gzip server compression.
I don't know how involved that is in IIS/.Net, but in PHP it's as trivial as adding one line to the global include file
CodeProject 有一个已发布的示例项目 (http://www.codeproject.com/KB/aspnet/AspNetOptimizer.aspx?fid=1528916&df=90&mpp=25&noise=3&sort= Position&view=Quick&select=2794900) 来处理以下一些情况...
CodeProject has a published sample project (http://www.codeproject.com/KB/aspnet/AspNetOptimizer.aspx?fid=1528916&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2794900) to handle some of the following situations...
对于 Microsoft .NET 平台,有一个名为 WebMarkupMin 的库,它可以缩小 HTML 代码。
此外,还有一个用于将此库集成到 ASP.NET MVC 中的模块 - WebMarkupMin.Mvc。
For Microsoft .NET platform there is a library called the WebMarkupMin, which produces the minification of HTML code.
In addition, there is a module for integration this library into ASP.NET MVC - WebMarkupMin.Mvc.
尝试 http://code.mini-tips.com/html-minifier.html,这是 Html Minifier 的 .NET 库
HtmlCompressor 是一个小型、快速且非常易于使用的 .NET 库,它通过删除额外的空格、注释和其他不需要的字符而不破坏内容结构来缩小给定的 HTML 或 XML 源。 结果,页面变得更小并且加载速度更快。 还提供压缩器的命令行版本。
try http://code.mini-tips.com/html-minifier.html, this is .NET Libary for Html Minifier
HtmlCompressor is a small, fast and very easy to use .NET library that minifies given HTML or XML source by removing extra whitespaces, comments and other unneeded characters without breaking the content structure. As a result pages become smaller in size and load faster. A command-line version of the compressor is also available.