简体   繁体   English

CSS压缩和组合/ js缩小 - 在运行时或构建时更好吗?

[英]CSS compression & combining / js minification - Better to do at runtime or at build time?

I have to come up with a solution to compress, version (for caching purposes) and potentially combine our JS and CSS files and I've seen two dominant approaches so far: 我必须提出压缩,版本(用于缓存目的)的解决方案,并可能组合我们的JS和CSS文件,到目前为止我已经看到了两种主要方法:

1) At build time: As an MSBuild task or similar. 1)在构建时:作为MSBuild任务或类似。 2) Dynamically at run time: As a custom HTTPHandler (we are ASP.NET btw) for .js and .css files, with something like this ending up in your pages: 2)在运行时动态:作为.js和.css文件的自定义HTTPHandler(我们是ASP.NET btw),这样的结果会在你的页面中结束:

<link rel="stylesheet" type="text/css" href="~/StyleSheetHandler.ashx?stylesheets=~stylesheets/master.css" /> 

Can anyone provide information of pro's and con's of each? 任何人都可以提供每个人的专业和信息吗? Personally, I can't see the point of doing it dynamically and wasting CPU cycles at the time of each request (I guess you'd only do with the work first time and then cache, but still) but I get the feeling I might be missing something? 就个人而言,我无法看到动态地做这一点并在每次请求时浪费CPU周期(我猜你只是第一次做工作然后缓存,但仍然)但我感觉我可能遗失了什么?

Thanks! 谢谢! Mark. 标记。

I think a good approach is to use different strategies for different environments: 我认为一个好的方法是针对不同的环境使用不同的策略:

  • no compression for development (for developing & debugging) 没有压缩开发 (用于开发和调试)
  • runtime compression for test environment (flexible, not performance-critical) 测试环境的 运行时压缩(灵活,不是性能关键)
  • buildtime compression for staging and production (tested, performance-critical) 用于分段和生产的 构建时压缩(经过测试,性能至关重要)

I have some experience using the YUI compressor for both Javascript and CSS and have learned (the hard way) that testing minified JS and CSS is indeed very important. 我有一些使用YUI压缩器进行Javascript和CSS的经验,并且已经学习(很难)测试缩小的JS和CSS确实非常重要。

Creating static minified JS and CSS files as part of your build for production has the following benefits: 创建静态缩小的JS和CSS文件作为生产构建的一部分具有以下优点:

  • they are tested 他们经过测试
  • static files, can be served without ASP.NET overhead 静态文件,可以在没有ASP.NET开销的情况下提供
  • can be browser cached 可以浏览器缓存
  • should be webserver-gzipped 应该是webserver-gzipped

The best way is to not do it at all , since all modern browsers and servers handle Gzip encoding. 最好的方法是不要这样做 ,因为所有现代浏览器和服务器都处理Gzip编码。 Look at the numbers: 看看数字:

  • cfform.js - 21k cfform.js - 21k
  • cfform-minified.js - 12k cfform-minified.js - 12k
  • cfform.js.gz - 4.2k cfform.js.gz - 4.2k
  • cfform-minified.js.gz - 2.2k cfform-minified.js.gz - 2.2k

This is a fairly large JS file with plenty of unnecessary whitespace but in the final equation you've saved a whopping 2k !! 这是一个相当大的JS文件,有大量不必要的空白,但在最后的等式中,你已经节省了惊人的2k! Not only that but thanks to caching this saving is per-visitor, not per-page. 不仅如此,而且由于缓存这个节省是每个访问者,而不是每页。 Whoo-hoo, now that was worth all the trouble wasn't it? Whoo-hoo,现在值得一试的不是吗?

You'd save 10 times that by cropping a pixel width off the top of your banner and with 99% of your users on broadband you've saved them about 1 millisecond of download time. 您可以通过裁剪横幅顶部的像素宽度来节省10倍,并且99%的用户使用宽带,您可以节省大约1毫秒的下载时间。 Break out the streamers and champagne! 打破飘带和香槟吧!

JS compression is even worse, since you've just hit your clients with the burden of decompressing on EVERY PAGE LOAD. JS压缩甚至更糟,因为你只需要在每个页面加载时解压缩你的客户。 And the savings after gzip are just as miserable. gzip之后的节省也同样悲惨。

Seriously. 认真。 The added complexity and debugging is not worth it unless you are targetting a mobile market (and even that assumes the users are still on CDMA, not 3G) or have a billion hits per day. 增加的复杂性和调试是不值得的,除非你的目标是移动市场(甚至假设用户仍然使用CDMA,而不是3G)或每天有10亿次点击。 Otherwise just don't bother. 否则只是不要打扰。

I'd say its better to do on first request and cache. 我会说在第一次请求和缓存时做得更好。 the reason for this is so that you can update the css as needed in a readable format without having to go back to rebuilding. 这样做的原因是您可以按可读格式更新css,而无需返回重建。 you can base your cache on the css file so as soon as it is changed the cache refreshes. 您可以将缓存基于css文件,以便在更改缓存时刷新。

Josh 玩笑

You do it at runtime but only when you need to. 您可以在运行时执行此操作,但仅在需要时执行。 This gives you the most flexibility. 这为您提供了最大的灵活性。 It's particularly an issue with PHP which otherwise has no build step (ie you don't want to add one when you don't have one otherwise) but still an issue for other platforms that do. 这特别是PHP的一个问题,否则没有构建步骤(即你不想在没有构建步骤时添加一个),但对于其他平台来说仍然存在问题。

At the risk of self-promotion, you might want to read Supercharging Javascript in PHP and Supercharging CSS in PHP , which outline the issues, approaches and best practices. 冒着自我推销的风险,您可能希望阅读PHP中的Supercharge Javascript和PHP中的 Supercharge CSS ,其中概述了问题,方法和最佳实践。 The examples are in PHP but the code itself is trivial. 示例是在PHP中,但代码本身是微不足道的。 The issues and principles apply to any Web platform. 问题和原则适用于任何Web平台。

I think you should make it at run time, except if your CSS and JS files are really huge (more than 1MB). 我认为你应该在运行时使用它,除非你的CSS和JS文件真的很大(超过1MB)。 The browser cache can be force by setting a few http headers , and when you want your application to force a reload at the client side, just change an HTTP param : 通过设置几个http标头可以强制使用浏览器缓存,当您希望应用程序强制在客户端重新加载时,只需更改HTTP参数:

<link rel="stylesheet" type="text/css" href="~/StyleSheetHandler.ashx?stylesheets=~stylesheets/master.css&token=1" />

You can change the token to force the reload of the CSS at client side. 您可以更改令牌以强制在客户端重新加载CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM