: Joining and compressing all javascript files together - good idea? Curently, I avoid loading any unnecesary scripts on individual pages of my site. I have a class that remembers all javascript
Curently, I avoid loading any unnecesary scripts on individual pages of my site. I have a class that remembers all javascript files that were requested during PHP processing and adds them to HTML.
I was just thinking that I could merge the current set of files, save the result in special directory and let the browser download just one, big file. Since the number of possible combinations is not very high, I would end up with about 10 combined files for different pages.
I've never seen that on any site. What are the reasons not to do it? I need very fast page load.
More posts by @Samaraweera270
2 Comments
Sorted by latest first Latest Oldest Best
Reducing http requests is one of the best ways to decrease your load time. Consequently it's best to serve one big JS file for your entire site. Give it a high expiry date and it will be cached for a long time, so a visitor will load this file only once.
Even though it might be a bit bigger you'll save a lot of load time by saving all the http requests. Just remember to change the URL of your big JS file whenever your javascript changes, e.g. by adding a ?unique_hash parameter.
The same reasoning goes for bundling all CSS into one big file.
I am using this technique myself for groups of pages and I am very happy with it. I have also learned to wrap my Javascript in an IFEE and to have uglify.js minimize it. It is even better.
In some cases, because I can have many combinations of javascripts for many files, I create many IFEEs and minimize them separately. I can easily achieve this because I use node.js, but I am sure there must be solutions for PHP.
The benefit of grouping Javascript is that it reduces the amount of interaction with the server, especially when setting a proper expiration value. After shrinking it, you get an extra speed kick when displaying the page.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2025 All Rights reserved.