简体   繁体   English

如何将多个javascript文件的请求合并到一个http请求中?

[英]How to combine requests for multiple javascript files into one http request?

This concept is a new one for me -- I first came across it at the YUI dependency configurator . 这个概念对我来说是一个新概念 - 我首先在YUI依赖配置器中遇到它。 Basically, instead of having multiple requests for many files, the files are chained into one http request to cut down on page load time. 基本上,这些文件不是对多个文件有多个请求,而是链接到一个http请求中,以减少页面加载时间。

Anyone know how to implement this on a LAMP stack? 任何人都知道如何在LAMP堆栈上实现它? (I saw a similar question was asked already, but it seems to be ASP specific . (我已经看到了类似的问题,但它似乎是ASP特定的

Thanks! 谢谢!

Update: Both answers are helpful...(my rep isn't high enough to comment yet so I'm adding some parting thoughts here). 更新:两个答案都很有帮助......(我的代表还不够评论,所以我在这里添加了一些离别的想法)。 I also came across another blog post with PHP-specific examples that might be useful. 我还发现了另一篇博客文章 ,其中包含可能有用的PHP特定示例。 David's build answer, though, is making me consider a different approach. 不过,大卫的构建答案让我考虑采用不同的方法。 Thanks, David! 谢谢,大卫!

There are various ways, the two most obvious would be: 有多种方式,最明显的两种方式是:

  1. Build a tool like YUI which builds a bespoke, unique version based on the components you ticked as required so that you can still serve the file as static. 构建一个像YUI这样的工具,它根据你需要勾选的组件构建一个定制的,唯一的版本,这样你仍然可以将文件作为静态服务。 MooTools and jQuery UI all provide package-builders like this when you download their package to give you the most streamlined and effecient library possible. 当您下载软件包时,MooTools和jQuery UI都提供这样的软件包构建器,以便为您提供最简化和最有效的库。 I'm sure a generic all purpose tool exists out there. 我确信那里存在一个通用的通用工具。
  2. Create a simple Perl/PHP/Python/Ruby script that serves a bunch of JavaScript files based on the request. 创建一个简单的Perl / PHP / Python / Ruby脚本,根据请求提供大量JavaScript文件。 So "onerequest.js?load=ui&load=effects" would go to a PHP script that loads in the files and serves them with the correct content-type. 因此,“onerequest.js?load = ui&load = effects”将转到加载文件的PHP脚本,并使用正确的内容类型为它们提供服务。 There are many examples of this but personally I'm not a fan. 有很多例子,但我个人并不是粉丝。

I prefer not to serve static files through any sort of script, but I also like to develop my code with 10 or so seperate small class files without the cost of 10 HTTP requests. 我不喜欢通过任何类型的脚本提供静态文件,但我也喜欢用10个左右的单独的小类文件开发我的代码而不需要10个HTTP请求。 So I came up with a custom build process that combines all the most common classes and functions and then minifies them into a single file like project.min.js and have a condition in all my views/templates that includes this file on production. 所以我想出了一个自定义构建过程,它结合了所有最常见的类和函数,然后将它们缩小为单个文件,如project.min.js,并且在我的所有视图/模板中都有一个条件,包括生成的这个文件。

Edit - The "custom build process" is actually an extremely simple perl script. 编辑 - “自定义构建过程”实际上是一个非常简单的perl脚本。 It reads in each of the files that I've passed as arguments and writes them to a new file, optionally passing the entire thing through JSMIN (available in all your favourite languages) automatically. 它读入我作为参数传递的每个文件,并将它们写入一个新文件,可选地通过JSMIN (以所有您喜欢的语言提供)自动传递整个文件。

At the command like it looks like: 在命令看起来像:

perl build-project-master.pl core.js class1.js etc.js /path/to/live/js/file.js

有一篇很好的博客文章@ http://www.hunlock.com/blogs/Supercharged_Javascript

What you want is Minify . 你想要的是Minify I just wrote a walkthrough for setting it up. 我刚刚写了一个演练来设置它。

Capistrano is a fairly popular Ruby-based web deployment tool. Capistrano是一个相当流行的基于Ruby的Web部署工具。 If you're considering it or already using it, there's a great gem that will figure out CSS and Javascript dependencies, merge, and minify the files. 如果您正在考虑它或已经在使用它,那么有一个很棒的宝石可以找出CSS和Javascript依赖关系,合并和缩小文件。

gem install juicer

From the Juicer GitHub page , it can figure out which files depend on each other and merge them together, reducing the number of http requests per page view, thus improving performance. Juicer GitHub页面中 ,它可以确定哪些文件相互依赖并将它们合并在一起,从而减少每页视图的http请求数,从而提高性能。

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

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