简体   繁体   English

单个JS文件XMLHttpRequest vs组合gzip下载

[英]individual JS file XMLHttpRequest vs combined gzip download

some stats before i can state the situation, 在我可以陈述情况之前的一些统计数据,

total JS code = 122 MB
minified = 36 MB
minified and gzip = 4 MB

I would like to get the entire 4 MB down in one shot (with a loading progress indicator on the page), uncompress them, but not parse them yet. 我想一次将整个4 MB压缩下来(在页面上带有加载进度指示器),解压缩它们,但尚未解析它们。 We don't want the code expanding in browsers memory when a lot of it might not be required at this point. 当此时可能不需要很多代码时,我们不希望代码在浏览器的内存中扩展。 The parsing should happen when a script tag with the corresponding js file name is encountered. 当遇到具有相应js文件名的脚本标签时,应进行解析。

Intention: faster one shot download of js files, but keeping the behaviour unchanged from the browser perspective. 目的:更快地一键下载js文件,但从浏览器角度看,其行为保持不变。

Do any such solutions exist? 是否存在这样的解决方案? Am I even thinking sane? 我什至在想理智吗? If yes, I know how to get the gzip, I would like to know how to keep them in the browser cache so that when a script tag is encountered the browser doesn't fire a XMLHttpRequest for it again. 如果是,我知道如何获取gzip,我想知道如何将其保存在浏览器缓存中,以便在遇到脚本标签时,浏览器不会再次为其触发XMLHttpRequest。

The trick is to leverage HTTP caching directives. 诀窍是利用HTTP缓存指令。 For a starter take a look at this . 首先,看看这个 You should only need to fetch your JS code once because you can safely set the cache directive to instruct the browser to hold on to the JS file indefinitely (subject to space). 您只需要获取一次JS代码,因为您可以安全地设置cache指令来指示浏览器无限期地保留JS文件(取决于空间)。 Indefinitely in this context typically means the year 2035. 在这种情况下,无限期通常表示2035年。

When you're ready to update all your browser-side caches with a new version of the JS file simply use a cache busting Query String. 当您准备使用新版本的JS文件更新所有浏览器端的缓存时,只需使用缓存无效查询字符串即可。 Any serial number or time and date will do, or a simple version number eg; 可以使用任何序列号或时间和日期,也可以使用简单的版本号,例如;

<script src="/js/myfile.js?v2.1"></script>

Some minification frameworks handle the cache-busting for you. 一些缩小框架为您处理缓存清除。 A good technique for example is those that MD5 the contents and use that as the cache buster query string. 例如,一种好的技术是将内容MD5用作MD5内容并将其用作缓存无效化查询字符串。 That way, whenever your source JS changes the browser will request the new version (because the QS is embedded in your HTML script tag) and then cache for as long as possible again. 这样,无论您的源JS何时更改,浏览器都会请求新版本(因为QS嵌入在HTML脚本标记中),然后再次缓存尽可能长的时间。

XMLHttpRequest will honour the caching primities you set. XMLHttpRequest将尊重您设置的缓存素数。

In the other part of your question, I believe what you're asking is whether you can download one combined script file and then only refer to parts of it with individual script tags on the page. 在问题的另一部分,我相信您要问的是是否可以下载一个组合的脚本文件,然后仅在页面上使用单个脚本标签引用它的一部分。 No - I don't believe you can do that. 不-我不相信你可以做到。 If you want to refer to individual files you would need to have a HTTP URL and caching directives for each piece of GZIPped content you want to use separately. 如果要引用单个文件,则需要为要单独使用的每条GZIPped内容提供一个HTTP URL和缓存指令。 However, you might find this is as much or maybe even more performant than one big file at first depending on how much parallelisation you can achieve. 但是,您可能会发现它起初的性能与一个大文件相比甚至更高,甚至更多,这取决于您可以实现多少并行化。

A neat trick here is to pre-load a lot of what you need. 一个巧妙的技巧是预加载很多您需要的东西。 Google have been doing this on the home page for years. Google多年来一直在主页上进行此操作。 Basically, they pre-load stacks of resources (images certainly, but possibly also JS). 基本上,它们预加载资源堆栈(当然是图像,但也可能是JS)。 So whilst you're thinking about what search query to enter, they are already loading the cache up with stuff you'll want on the subsequent page. 因此,当您考虑输入什么搜索查询时,他们已经在缓存中加载了您在下一页上想要的内容。

So you could use XMLHttpRequest to fetch your JS files (without parsing them) well before you need them. 因此,您可以在需要它们之前使用XMLHttpRequest来获取您的JS文件(无需解析它们)。 Then by the time your <script/> tag refers to them they'll already be downloaded and you just need to parse them. 然后,当您的<script/>标记引用它们时,它们便已被下载,您只需解析它们即可。

In addition to cirrus's point about using HTTP caching, you could break that still-pretty-large 4mb file down and only load them when that functionality is required. 除了cirrus关于使用HTTP缓存的观点之外,您还可以分解仍然很大的4mb文件,并仅在需要此功能时才加载它们。

It's more HTTP requests, but 4MB is a big hit in one go. 这是更多的HTTP请求,但是4MB一口气就大受欢迎。

Suggest something like require.js to load in the appropriate files when they are needed: http://requirejs.org/docs/start.html 建议像require.js这样的东西在需要时加载适当的文件: http ://requirejs.org/docs/start.html

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

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