天天看點

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.

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:

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

繼續閱讀