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


目录
相关文章
|
数据安全/隐私保护 Windows
Windows 命令提示符(CMD)操作(三):用户管理
Windows 命令提示符(CMD)操作(三):用户管理
|
缓存 网络协议 5G
剖析KCP以及KCP在游戏中是如何使用的
剖析KCP以及KCP在游戏中是如何使用的
|
12月前
|
弹性计算 固态存储 大数据
今年最新阿里云服务器年/月/小时价格表详解:含ECS、轻量与GPU云服务器
阿里云服务器多少钱?今年最新阿里云服务器年/月/小时价格表详解:含ECS、轻量与GPU云服务器。今年最新的云服务器租用价格表,包括轻量应用服务器、云服务器ECS和GPU服务器。现在最新阿里云服务器租用费用价格表,轻量2核2G轻量服务器一年68元,折合5.6元1个月,新老用户同享99元一年服务器,2核4G5M服务器ECS优惠价199元一年(企业专享),2核4G4M轻量服务器298元一年,4核8G服务器955元一年,4核16G10M服务器70元1个月、210元3个月,8核32G服务器160元1个月、480元3个月,整理2026阿里云服务器租用费用价格表,包括一年优惠价格、一个月和1小时收费明细表:
|
人工智能 文字识别 算法
搬运5款让你眼前一亮的神器软件
本文推荐了五款小众但实用的软件,涵盖多种需求:GoodSync实现跨端文件同步与备份,采用高效加密技术;Cryptomator提供云盘文件加密保护,确保数据安全;3171.cn集成多种在线AI工具,助力办公学习;OneNote作为全能笔记工具,支持多平台协作与智能管理;EasyRecovery专注数据恢复,解决误删或格式化问题。这些软件功能强大且经过测试,值得尝试。
1153 17
搬运5款让你眼前一亮的神器软件
|
安全 区块链 数据安全/隐私保护
区块链技术在跨境支付中的应用:打破传统,畅行全球支付新时代
区块链技术在跨境支付中的应用:打破传统,畅行全球支付新时代
1791 12
区块链技术在跨境支付中的应用:打破传统,畅行全球支付新时代
|
存储 监控 调度
云迁移中心CMH:助力企业高效上云实践全解析
随着云计算的发展,企业上云已成为创新发展的关键。然而,企业上云面临诸多挑战,如复杂的应用依赖梳理、成本效益分析等。阿里云推出的云迁移中心(CMH)旨在解决这些问题,提供自动化的系统调研、规划、迁移和割接等功能,简化上云过程。CMH通过评估、准备、迁移和割接四个阶段,帮助企业高效完成数字化转型。未来,CMH将继续提升智能化水平,支持更多行业和复杂环境,助力企业轻松上云。
|
监控 数据可视化 项目管理
WBS元素是什么?在项目管理中的作用与类型定义
工作分解结构(WBS)是项目管理中的重要工具,用于将复杂项目分解为更小、更易管理的任务或工作包。本文介绍了WBS的概念、特点、核心目标及其在项目管理中的作用,包括项目范围定义、任务分配、进度管理、预算控制和风险管理等方面。同时,文章还探讨了几种常见的WBS类型,如交付物导向型、阶段导向型、功能导向型和混合型WBS,并提供了创建有效WBS的步骤建议。最后,推荐了几款实用的项目管理工具,帮助团队更高效地管理和执行项目。
2355 7
|
搜索推荐 Python
Python 冒泡排序:原理、使用场景与实现方法
本文主要介绍了Python 冒泡排序:原理、使用场景与实现方法
1241 6
Python 冒泡排序:原理、使用场景与实现方法
|
机器学习/深度学习 编解码 并行计算
【深度学习】多头注意力机制详解
【深度学习】多头注意力机制详解
1119 1
|
NoSQL 关系型数据库 MySQL
主备切换大揭秘:保证系统永不停机的秘密
本文由小米分享,介绍了分布式系统中的主备切换机制,旨在确保高可用性和可靠性。内容涵盖热备和冷备的概念,以及MySQL和Redis的主从复制原理和配置方法。通过主从复制,当主服务器故障时,备服务器能接管工作,维持服务连续性。文章还讨论了主备切换的挑战,如数据一致性与切换延迟,并提出了相应的解决方案。最后,作者鼓励读者就该主题提出疑问和建议。
1547 4