English 中文(简体)
Scripts/Stylesheets: Consolidate locally or use CDN?
原标题:

I can obviously do some limited testing of my own, but I m hoping to hear from some people with real-world experience on at least medium-scale web sites.

Two of the items on every "top 10" list for optimizing sites/bandwidth are:

  • Consolidate as much JS and CSS as possible into a single file (to reduce round trips); and
  • Use a Content Delivery Network (to save bandwidth/speed up downloads).

Nowadays you can find almost every important script on either Google s or Microsoft s CDN (or both). You can even find some pretty heavyweight items like jQuery ThemeRoller packages. One of the major advantages of using these CDNs over a private CDN account like Amazon S3 is that many visitors will already have these scripts cached from some other web site. Even if they don t, it s hard to beat the performance of a site like Google.

So let s say I want to use these public CDNs rather than paying extra for a personal CDN account for performance that could be better or worse or the same. And let s also say I m using a bunch of JS/CSS files that are all on these public CDNs: jQuery, jquery-ui, one theme, jQuery TOOLS, and maybe a few others. In addition to those I may be using several scripts that are not available on a public CDN, like superfish and jquery.approach and hoverIntent. Pretty common stuff really. It seems I have two mutually exclusive options:

  • Refer to the public CDNs for the scripts listed above. The advantage is that I get to use somebody else s bandwidth and take advantage of the popularity of some of these scripts. The disadvantage is that the client must make no fewer than 7 round-trips for all these files (4 individual JS, 1 CSS, and 1 each CSS and JS for the consolidated/minified local stuff). Or...
  • Cram everything into two monolithic minified files (one for JS and one for CSS) and serve everything off my own web server. The advantage is that there are only two round trips (a 66% savings, at least). The disadvantage, of course, is that now I now have to serve a much bigger amount myself.

This seems like a tough choice, and I think the only way to really understand which will work better ("better" being a function of client load times and overall user experience) is to actually put it out there in the wild and see what happens.

So has anyone had to make this choice before, and if so, what did you choose, and why, and how well did it turn out? Or, if anyone has tried to tackle the task of testing in an isolated environment, how did you go about it, and what were the results?

P.S. This is all describing a "web application" type of site - in other words, not very media-heavy, but may contain a ton of styles and scripts that actually account for a significant chunk of the page size. Therefore I do consider this more than just an intellectual exercise.

最佳回答

Check out Focus.com in Firebug

We took a hybrid approach. We pull jQuery off Google and the rest of our css and js are combined / minified and served off S3. We automated the combo / minification step via our build process and each version is deployed under its own (numbered) directory to allow browsers to aggressively cache our resources.

We choose to pull jQuery off Google to save the 40K hit given the frequency of code updates to our site (roughly every two weeks). That having been said, before we launched we did a bunch of testing of different configurations and didn t detect significant differences in performance.

问题回答

I would think it all comes down to the kind of traffic you expect. If you re getting a ton of uniques per month then there is a financial incentive to at least use Google or Microsoft to serve JS/CSS and let them bear the bandwidth costs.

Microsoft hosts a bunch of jQuery and related files on their CDN here.





相关问题
Export to pdf on the client or server side?

My client side application is using jqgrid that requests the server side, which in its turn queries MySQL and sends back the resultSet in XML format. I m willing to add the export to PDF ...

Image generator

I d like to be able to generate an image with overlaying text (that is inputted by the user) such as how it is done on www.depressiondog.info. Can someone show/link me to some code that will do that. ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

服务器面与用户对用户的MVC在网络信号中

我最近找到了一种与《伙伴关系协定》有关的有趣的替代办法。 NET MVC,我目前做的是:静态超文本,配有先进的 Java本式服务电话(大多数例子使用开放式雷达)。 例如,见此或......。

MySQL Users and All Privileges

Ok, I m trying to add a user to a MySQL database. This user should be able to add other users to the database it has privileges for. So I did this: GRANT ALL privileges ON thedbname.* TO topuser @ % ...

Doesn t get into Debug Mode

When I Press F5 on my VS2005 to debug the application, it launches the web app window but it is coming out of debug mode. When I tried to trace the Error in the EventViewer, this was the error: ...

热门标签