1

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.

  • 1
    Well, one reason is that the shared content among the different pages will be downloaded multiple times rather than once. Generally speaking though this is a very common concept (concat JS files). – Benjamin Gruenbaum Aug 23 '14 at 14:05
  • Don't forget to approve an answer if you like any... – Jérôme Verstrynge Aug 25 '14 at 17:11
0

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.