简体   繁体   English

用Javascript处理多个JS文件的请求

[英]Javascript one request for multiple JS files

I was thinking about creating script that would do the following: 我正在考虑创建执行以下操作的脚本:

  1. Get all javascripts from JS directory used on server 从服务器上使用的JS目录获取所有JavaScript
  2. Combine all scripts to one - that would make only one request instead of multiple 将所有脚本合并为一个-只会发出一个请求,而不是多个
  3. Minify combined script 缩小组合脚本
  4. Cache the file 缓存文件

Let's say that the order in which the files need to be loaded is written in config file somewhere. 假设需要加载文件的顺序写在某个地方的配置文件中。

Now when I load myexamplepage.com I actually use jQuery, backbone, mootools, prototype and few other libraries, but instead of asking server for these multiple files, I call myexamplepage.com/js/getjs and what I get is combined and minified JS file. 现在,当我加载myexamplepage.com时,我实际上使用了jQuery,骨干网,mootools,prototype和其他一些库,但是我没有向服务器询问这些多个文件,而是调用了myexamplepage.com/js/getjs,我得到的是合并并缩小的JS文件。 That way I eliminate those additional requests to server. 这样,我就消除了对服务器的那些额外请求。 And as I read on net about speeding up your website I found out that the more requests you make to server, the slower your web become. 当我在网上阅读有关加速您的网站的信息时,我发现您对服务器的请求越多,您的网站就变得越慢。

Since I'm pretty new to programming world I know that many things that I think of already exists, I don't think that this is exception also. 因为我对编程世界还很陌生,所以我知道我已经想到了很多东西,因此我也不认为这也是例外。

So please list what you know that does exactly or similar to what I described.(please note that you don't need to use any kind of minifiers or third party software everytime you want your scripts to be changed, you keep original files structure, you only use class helper) 因此,请列出您所知道的与我描述的内容完全相同或相似的内容。(请注意,您每次需要更改脚本时都不需要使用任何种类的压缩程序或第三方软件,您可以保留原始文件结构,您只使用班级助手)

PS I think same method could be used for CSS files also. PS我认为同样的方法也可以用于CSS文件。

I'm using PHP and Apache. 我正在使用PHP和Apache。

Rather than having the server do this on-the-fly, I'd recommend doing it in advance: Just concatenate the scripts and run them through a non-destructive minifier, like jsmin or Google Closure Compiler in "simple" mode. 建议不要事先进行服务器操作,而是建议提前进行操作:只需串联脚本并通过“简单”模式下的jsminGoogle Closure Compiler等无损压缩程序运行它们即可。

This also gives you the opportunity to put a version number on that file, and to give it a long cache life, so that users don't have to re-download it each time they come to the page. 这也使您有机会在该文件上放置一个版本号,并具有较长的缓存寿命,从而使用户不必在每次访问该页面时都重新下载它。 For example: Suppose the content of your page changes frequently enough that you set the cache headers on the page to say it expires every day. 例如:假设页面的内容更改频率足够高,以至于您在页面上设置了高速缓存标头,说它每天过期。 Naturally, your JavaScript doesn't change every day. 自然,您的JavaScript并非每天都会更改。 So your page.html can include a file called all-my-js-v4.js which has a long cache life (like, a year). 因此,您的page.html可以包含一个名为all-my-js-v4.js的文件,该文件的缓存寿命很长(例如一年)。 If you update your JavaScript, create a new all-in-one file called all-my-js-v5.js and update page.html to include that instead. 如果您更新JavaScript,请创建一个名为all-my-js-v5.js的新的多合一文件,并更新page.html以包括该文件。 The next time the user sees page.html , they'll request the updated file; 下次用户看到page.html ,他们将请求更新的文件。 but until then, they can use their cached copy. 但在那之前,他们可以使用其缓存的副本。

If you really want to do this on-the-fly, if you're using apache, you could use mod_pagespeed . 如果您真的想即时执行此操作,并且您正在使用apache,则可以使用mod_pagespeed

If you're using .NET, I can recommend Combres . 如果您使用的是.NET,我可以推荐Combres It does combination and minification of JavaScript and CSS files. 它可以对JavaScript和CSS文件进行组合和压缩。

I know this is an old question, but you may be interested in this project: https://github.com/OpenNTF/JavascriptAggregator 我知道这是一个老问题,但是您可能对此项目感兴趣: https : //github.com/OpenNTF/JavascriptAggregator

Assuming you use AMD modules for your javascript, this project will create highly cacheable layers on demand. 假设您为JavaScript使用AMD模块,则该项目将按需创建高度可缓存的层。 It has other features you may be interested in as well. 它还具有您可能感兴趣的其他功能。

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

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