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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/101893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:44:26  来源:igfitidea点击:

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

phpjavascriptapachehttp

提问by Novaktually

This concept is a new one for me -- I first came across it at the YUI dependency configurator. Basically, instead of having multiple requests for many files, the files are chained into one http request to cut down on page load time.

这个概念对我来说是一个新概念——我第一次在YUI 依赖配置器中遇到它。基本上,不是对多个文件有多个请求,而是将文件链接到一个 http 请求中以减少页面加载时间。

Anyone know how to implement this on a LAMP stack? (I saw a similar question was asked already, but it seems to be ASP specific.

任何人都知道如何在 LAMP 堆栈上实现它?(我看到已经有人问过类似的问题,但似乎是 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 postwith PHP-specific examples that might be useful. David's build answer, though, is making me consider a different approach. Thanks, David!

更新:两个答案都有帮助......(我的代表还不够高,无法发表评论,所以我在这里添加了一些离别想法)。我还看到了另一篇博客文章,其中包含可能有用的特定于 PHP 的示例。不过,大卫的构建答案让我考虑采用不同的方法。谢谢,大卫!

回答by David McLaughlin

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. 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. 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. 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. There are many examples of this but personally I'm not a fan.
  1. 构建一个像 YUI 这样的工具,它根据您根据需要勾选的组件构建一个定制的、独特的版本,以便您仍然可以将文件作为静态文件提供。MooTools 和 jQuery UI 都提供了这样的包构建器,当您下载它们的包时,可以为您提供最精简和最高效的库。我确信那里存在一个通用的通用工具。
  2. 创建一个简单的 Perl/PHP/Python/Ruby 脚本,根据请求提供一堆 JavaScript 文件。因此,“onerequest.js?load=ui&load=effects”将转到加载文件并为它们提供正确内容类型的 PHP 脚本。这方面的例子很多,但我个人不是粉丝。

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

我不喜欢通过任何类型的脚本提供静态文件,但我也喜欢用 10 个左右的独立小类文件开发我的代码,而无需 10 个 HTTP 请求。所以我想出了一个自定义构建过程,它结合了所有最常见的类和函数,然后将它们缩小到一个文件中,比如 project.min.js,并且在我的所有视图/模板中都有一个条件,在生产中包含这个文件。

Edit - The "custom build process" is actually an extremely simple perl script. 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.

编辑 - “自定义构建过程”实际上是一个非常简单的 perl 脚本。它读入我作为参数传递的每个文件并将它们写入一个新文件,可选地通过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

回答by Jamie

There is a good blog post on this @ http://www.hunlock.com/blogs/Supercharged_Javascript.

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

回答by Steve Clay

What you want is Minify. I just wrote a walkthroughfor setting it up.

你想要的是Minify。我刚刚写了一个演练来设置它。

回答by Jonathan Berger

Capistrano is a fairly popular Ruby-based web deployment tool. 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.

Capistrano 是一种相当流行的基于 Ruby 的 Web 部署工具。如果您正在考虑或已经在使用它,那么有一个很棒的 gem 可以找出 CSS 和 Javascript 依赖项、合并和缩小文件。

gem install juicer

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 请求数,从而提高性能。