te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>asp.net - CSS compression & combiningjs minification - Better to do at runtime or at build time? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - CSS compression & combiningjs minification - Better to do at runtime or at build time? - Stack Overflow

programmeradmin1浏览0评论

I have to e up with a solution to press, version (for caching purposes) and potentially bine our JS and CSS files and I've seen two dominant approaches so far:

1) At build time: As an MSBuild task or similar. 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:

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

Thanks! Mark.

I have to e up with a solution to press, version (for caching purposes) and potentially bine our JS and CSS files and I've seen two dominant approaches so far:

1) At build time: As an MSBuild task or similar. 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:

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

Thanks! Mark.

Share Improve this question asked Jun 22, 2009 at 11:35 Mark GibaudMark Gibaud 2,06123 silver badges35 bronze badges 2
  • Will be turning on GZIP anyway, but bining/versioning/minifying are separate. – Mark Gibaud Commented Jun 24, 2009 at 15:58
  • 1 Here's what I cranked out: markgibaud./blog/?p=22 – Mark Gibaud Commented Nov 11, 2009 at 22:28
Add a ment  | 

5 Answers 5

Reset to default 8

I think a good approach is to use different strategies for different environments:

  • no pression for development (for developing & debugging)
  • runtime pression for test environment (flexible, not performance-critical)
  • buildtime pression for staging and production (tested, performance-critical)

I have some experience using the YUI pressor for both Javascript and CSS and have learned (the hard way) that testing minified JS and CSS is indeed very important.

Creating static minified JS and CSS files as part of your build for production has the following benefits:

  • they are tested
  • static files, can be served without ASP.NET overhead
  • can be browser cached
  • should be webserver-gzipped

The best way is to not do it at all, since all modern browsers and servers handle Gzip encoding. Look at the numbers:

  • cfform.js - 21k
  • cfform-minified.js - 12k
  • cfform.js.gz - 4.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 !! 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?

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. Break out the streamers and champagne!

JS pression is even worse, since you've just hit your clients with the burden of depressing on EVERY PAGE LOAD. And the savings after gzip are just as miserable.

Seriously. The added plexity 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. 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. you can base your cache on the css file so as soon as it is changed the cache refreshes.

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.

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. The examples are in PHP but the code itself is trivial. The issues and principles apply to any Web platform.

I think you should make it at run time, except if your CSS and JS files are really huge (more than 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 :

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

发布评论

评论列表(0)

  1. 暂无评论