HTML 缩小?

发布于 2024-07-17 04:02:29 字数 169 浏览 5 评论 0原文

有没有一个在线工具,我们可以输入页面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

生死何惧 2024-07-24 04:02:29

也许可以尝试 HTML 压缩器,这里有一个前后表,显示了它的功能(包括 Stack溢出本身):

抱歉,markdown 没有表格的概念

它具有许多用于优化页面的选项,包括脚本最小化(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):

Sorry, markdown has no concept of tables

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.

一江春梦 2024-07-24 04:02:29

不要这样做。 或者更确切地说,如果您坚持这样做,请在完成任何更重要的网站优化后再进行。 这项工作的成本/收益很可能可以忽略不计,尤其如果您计划手动使用在线工具来处理每个页面。

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

你好,陌生人 2024-07-24 04:02:29

以下是对您的问题的简短回答:您应该缩小 HTML、CSS、JS。 有一个易于使用的工具,称为 grunt。 它允许您自动执行许多任务。 其中JSCSSHTML 缩小、文件串联许多其他

这里写的答案非常过时,甚至有时没有意义。 与 2009 年相比,很多事情都发生了变化,所以我会尽力正确回答这个问题。

简短的回答 - 你绝对应该缩小 HTML。 今天这很简单,并且可以提供大约 5% 的加速。 对于更长的答案,请阅读整个答案

回到过去,人们手动缩小 css/js (通过通过某些特定工具运行它来缩小它)。 使该过程自动化有点困难,并且肯定需要一些技能。 知道现在很多高级网站都没有使用 gzip(这很简单),人们不愿意缩小 html 是可以理解的。

那么为什么人们缩小 js,而不是 html? 做以下事情:

  • 删除注释
  • 删除空白(制表符、空格、换行符)
  • 将长名称更改为短名称(var isUserLoggedInvar 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 和不同的模板。

结束我的想法:

  • 谷歌正在缩小 html。
  • pageSpeed 要求您缩小 html,
  • 这很简单,
  • 它可以提供约 5% 的改进,
  • 但它与压缩包

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:

  • remove comments
  • remove blanks (tabs, spaces, newlines)
  • change long names to short (var isUserLoggedIn to var 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:

  • google is minifying html.
  • pageSpeed is asking your to minify html
  • it is trivial to do
  • it gives ~5% of improvement
  • it is not the same as gzip
離殇 2024-07-24 04:02:29

我编写了一个网络工具来缩小 HTML。 http://prettydiff.com/?m=minify&html

此工具使用这些规则进行操作:

  • 删除所有 HTML 注释
  • 连续的空白字符将转换为单个空格字符
  • 删除标签内不必要的空白字符
  • 两个标签之间的空白字符(其中这两个标签之一不是单例)将被删除
  • 样式内的所有内容 标记被假定为 CSS,并被缩小为此类
  • script 标记内的所有内容均被假定为 JavaScript,除非提供了不同的媒体类型,然后被缩小为此类
    • CSS 和 JavaScript缩小使用 JSMin 的重分叉形式。 该分支经过扩展,可以原生支持 CSS,并且还支持 SCSS 语法。 JavaScript 缩小支持自动分号插入,但尚不支持自动大括号插入。

    I wrote a web tool to minify HTML. http://prettydiff.com/?m=minify&html

    This tool operates using these rules:

  • All HTML comments are removed
  • Runs of white space characters are converted to single space characters
  • Unnecessary white space characters inside tags are removed
  • White space characters between two tags where one of these two tags is not a singleton is removed
  • All content inside a style tag is presumed to be CSS and is minified as such
  • All content inside a script tag is presumed to be JavaScript, unless provided a different media type, and then minified as such
    • The CSS and JavaScript minification uses a heavily forked form of JSMin. This fork is extended to support CSS natively and also support SCSS syntax. Automatic semicolon insertion is supported for JavaScript minification, however automatic curly brace insertion is not yet supported.
    梦在夏天 2024-07-24 04:02:29

    这对我有用:

    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

    枫以 2024-07-24 04:02:29

    CodeProject 有一个已发布的示例项目 (http://www.codeproject.com/KB/aspnet/AspNetOptimizer.aspx?fid=1528916&df=90&mpp=25&noise=3&sort= Position&view=Quick&select=2794900) 来处理以下一些情况...

    • 将 ScriptResource.axd 调用合并为一个调用
    • 基于浏览器功能压缩所有客户端脚本,包括 gzip/deflate
    • 一个 ScriptMinifier删除注释、缩进和换行符。
    • 一个 HTML 压缩器,用于基于浏览器功能(包括 gzip/deflate)压缩所有 html 标记。
    • 而且 - 最重要的是 - HTML 缩小器将完整的 html 写入单行并在可能的级别(正在建设中)缩小它。

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

    • Combining ScriptResource.axd calls into a single call
    • Compress all client side scripts based on the browser capability including gzip/deflate
    • A ScriptMinifier to remove comments, indentations, and line breaks.
    • An HTML compressor to compress all html markup based on the browser capability including gzip/deflate.
    • And - most importantly - an HTML Minifier to write complete html into single line and minify it at possible level (under construction).
    千纸鹤 2024-07-24 04:02:29

    对于 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.

    说谎友 2024-07-24 04:02:29

    尝试 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.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文