WHY YOU SHOULDN'T MERGE JAVASCRIPT IN MAGENTO

Most people - myself included - thought that merging all of your separate Javascript files was a healthy way to speed up the front end of your site. The logic was that by merging the files, you reduce the number of requests the browser makes when loading your
site. This allows the request to complete quicker and results in your page loading faster. Let's walk through an example of this process in Magento and see what happens.

Imagine you have just enabled Javascript Merging in the Magento Admin and then a user visits your site. Magento collates all of the XML layout files and determines which Javascript files should be included for the homepage request. These files are then merged
into a single file and saved using an md5 hash of their filenames. This new file, named f0eb853c09ba3e46ad21fb89b8d57626.js, is then served to the user, who downloads it and stores it in their browser cache.

Next, the user clicks a link to one of your categories. Once again, Magento collates the XML layout files and determines what Javascript files are needed for the category page. At this point, Magento realises that the exact same Javascript files used on the
homepage are needed on this page. Instead of remaking the single Javascript file, Magento serves the Javascript file created when the user visited the homepage. This time though, the browser knows that it already has this file and therefore doesn't download
it again. Instead, the file is loaded from the user's cache, saving time, bandwidth and computer resources!

So far everything has gone well; we have made the page load faster while decreasing the load on the server, but...

Next, the user is enticed by one of your products and clicks through to a product page. Once again, Magento determines which Javascript files are needed based on the layout XML files. At this point, Magento realises that the same Javascript files on the homepage
are needed as well two new files. As a merged file containing this combination of Javascript doesn't exist, Magento merges the files and creates a new Javascript file named 139f9771ba2ba0cae754978fab4a3c35.js. Roughly 80% of this file is the Javascript that
was used on the homepage and has been downloaded and cached by the user already. The final 20% accounts for the new Javascript files used solely on the product page. This file is then served to the user who is forced to download it all! Although this file
is made of 80% code that has already been cached, the user's browser is completely unaware of this and will download 100% of the file and cache it again!

The intended result of merging Javascript is a decreased page load time, but in the above scenario, the user has been forced to re-download a massive chunk of Javascript! This will no doubt have increased page load time (downloading ~40kb of Javascript that
doesn't need to be downloaded), which goes against the idea of Javascript merging.

Let's consider what would happen if we hadn't of merged our Javascript...

When the user first requests the homepage, the merged Javascript files would be downloaded individually. Although the combined size of these Javascript files matched the size of the merged Javascript file, this request would take longer as each file would need
to be requested, downloaded and cached separately. 1-0 merging!

Second, the user requested the category page, which uses the same files that were just downloaded and cached on the homepage. As a result this takes exactly the same amount of time it would if the Javascript files had been merged. In both instances, all of
the required Javascript has already been downloaded and cached, meaning the browser can skip the Javascript altogether.

Lastly, the user visits the product page, which uses the Javascript files found on the home and category pages as well as two new files. The files used on both the homepage and category page have already been cached, so the browser skips these files and downloads
and caches the two new files that haven't been experienced before! In the merging scenario, we mentioned that files used on the homepage in Magento make up about 80% of the total Javascript on the product page. With merging disabled, we have managed to skip
downloading the 80% and just download the 20%. In the merging example, the full 100% had to be re-downloaded!

This problem is compounded for each page your user visits that has a combination of Javascript not found on another page. Each time this happens, the user will be forced to re-download Javascript that it has already downloaded and cached under a different filename.

WHAT CAN BE DONE?

While the current Magento merging system is flawed, it would only take a minor adjustment to fix this critical issue.

In Magento, Javascript is added to the Head block through XML and stored in an array, which is then iterated over and displayed in the head section of your HTML code. Our suggestion is that instead of simply adding Javascript files to the head, an extra parameter
be included that allows you to specify a group for the Javascript. Consider the following code:

<default>
 <reference name="head">
   <action method="addJs"><script>prototype/prototype.js</script><group>global</group></action>
   <action method="addJs"><script>scriptaculous/builder.js</script><group>global</group></action>
   <action method="addJs"><script>scriptaculous/effects.js</script><group>global</group></action>
   <action method="addJs"><script>varien/form.js</script><group>global</group></action>  </reference>
</default>

<catalog_product_view>
 <reference name="head">
   <action method="addJs"><script>varien/product.js</script><group>product</group></action>
   <action method="addJs"><script>varien/configurable.js</script><group>product</group></action>
 </reference>
</catalog_product_view>

Notice the addition of the paramter group?
Instead of all of the Javascript being merged into a single file, the group parameter would be be used to decide which Javascript files get merged together. The above code would result in two separate merged files being created for the product page; one containing
the global Javascript that is used on the homepage (and the whole of the site) and a second containing the Javascript used solely on the product page. Although this would create an extra page request, time would still be saved by not having to re-download
the Javascript used on the homepage! If this technique were applied to the whole Magento site, each piece of Javascript would be downloaded once and once only!

源文:WHY
YOU SHOULDN'T MERGE JAVASCRIPT IN MAGENTO

时间: 2024-08-05 01:34:37

WHY YOU SHOULDN&#39;T MERGE JAVASCRIPT IN MAGENTO的相关文章

magento -- 推荐插件 -- 智能版Js和Css合并 --Lesti::Merge(Smart Merging of Js and Css)

名称:   Lesti::Merge(Smart Merging of Js and Css) Extension Key:http://connect20.magentocommerce.com/community/Lesti_Merge 网址:    http://www.magentocommerce.com/magento-connect/lesti-merge-2265.html 介绍:   基于Magento原生的合并JS和CSS功能,提供更聪明更智能的JS和CSS合并方案,可以根据

100个不能错过的实用JS自定义函数

 本文收集了100个原生态JavaScript编写的常用.实用自定义函数,需要的朋友可以参考下 1.原生JavaScript实现字符串长度截取   代码如下: function cutstr(str, len) {     var temp;     var icount = 0;     var patrn = /[^x00-xff]/;     var strre = "";     for (var i = 0; i < str.length; i++) {        

100个不能错过的实用JS自定义函数_javascript技巧

1.原生JavaScript实现字符串长度截取 复制代码 代码如下: function cutstr(str, len) {    var temp;    var icount = 0;    var patrn = /[^\x00-\xff]/;    var strre = "";    for (var i = 0; i < str.length; i++) {        if (icount < len - 1) {            temp = str

html-&amp;amp;amp;nbsp;在谷歌浏览器中显示成一个方框,怎么解决呢

问题描述 &nbsp:在谷歌浏览器中显示成一个方框,怎么解决呢 如题,求有经验的指点下,ie和火狐都没问题,其他地方也没问题,就这里 <div class="STYLE1" id="Div1"><div align="center" class="STYLE11"><a href="www.chi-biotech.net"><img src="sr

jQuery入门知识简介_jquery

到目前为之jQuery已经发布到1.4.2版本,而在这之前的一个星期他们刚发布1.4版本,看看他的各个版本的 发布时间 ,不难发现他的飞速发展,每个月都在更新版本:而且不断有人开发出新的 jQuery插件 ,最近又推出了 jQuery UI 库 jQuery于2006年一月十四号在BarCamp NYC (New York City)面世.主将 John Resig ,写有<Pro JavaScript Techniques>一书,因为效力于mozolla,据说firefox 3将包含Jque

Velocity官方指南-Velocity是如何工作的

基本模式 当你在一个应用程序或者一个servlet里,或者在其他任何一个地方使用Velocity时,通常按照如下方式处理: 初始化Velocity.Velocity可以使用两种模式,作为"单独的运行时实例"的单例模式(在下面的内容会介绍),你仅仅只需要初始化一次. 创建一个Context对象(后面会介绍这是什么). 把你的数据对象添加到Context(上下文). 选择一个模板. '合并' 模板和你的数据输出. 在代码里通过org.apache.velocity.app.Velocity

WebBrowser控件的简单应用2

原文:WebBrowser控件的简单应用2 第一个简单应用里面讲述的是如何模拟调用当前网页的元素的事件或者赋值/取值.这次的应用讲述的是1:如何处理弹出新页面的事件(总是在我的浏览器里面现实新页面)2:如何处理window.close事件,让我的浏览器页关闭3:让html页面的js调用我的browse的函数4:如何让我的browse调用html的js函数. 使用场景:一个web程序,让用户使用自定义浏览器来浏览,该web程序会调用浏览者机器上一些接口.我的这个浏览器叫做AppBrowser.关于

设置首页和收藏本站

1 设为首页 和 收藏本站js代码 兼容IE,chrome,ff 2 3 //设为首页 4 function SetHome(obj,url){ 5 try{ 6 obj.style.behavior='url(#default#homepage)'; 7 obj.setHomePage(url); 8 }catch(e){ 9 if(window.netscape){ 10 try{ 11 netscape.security.PrivilegeManager.enablePrivilege("

Javascript中 a.href 和 a.getAttribute(&amp;#39;href&amp;#39;) 结果不完全一致

今天无意中发现这个么问题,页面上对所有A标签的href属性为空的自动添加一个链接地址,结果发现if判断条件始终都没生效,莫名其妙. 原来Javascript中 a.href 和 a.getAttribute('href') 结果在某些情况下是不完全一致的,以前从来都没注意过这个问题. 下面举个栗子: <a href="">测试1</a> <a href="#">测试2</a> <a href="java