Why It Is Hard To Minify On The Fly

One of the tips to speed up websites is it minify the JavaScript. It’s simple, effective and the major players already do it (except Drupal). In the discussions for Drupal 8 and how we can speed up Drupal 7 we’ve been talking about how we go about providing minified JavaScript files for browsers and there are two camps. One camp wants to ship minified files and the other wants to minify on the fly. While minifying on the fly would be nice it turns out that this is really hard for Drupal and other CMS systems.

Not A Technical Problem

When it comes to problems the technical ones can mostly be overcome. It’s not hard to tie in with UglifyJS or another tool to minify JavaScript. PHP libraries like Assetic already do this and there are even web services we could use.

The License Problems

Note: I am not a lawyer and this is not legal advice. If you want legal advice go see a lawyer.

Licenses are something virtually every software project uses. Some will even argue that you need to use license X instead of license Y. When we sign up to use a license we need to respect what we signed up for and follow them. Many of us work for companies with lawyers that will make us respect them/follow the law. This ends up creating a complication because many of the JavaScript files we use contain license and copyright information in comments. When a file is minified comments are stripped out. When we manage how they are minified by hand we can make sure licenses are still properly handled where on the fly processing can’t do this.

Removing comments may seem simple and safe because they aren’t needed for the application to technically run. But, when we ship files from our web servers to execute in a browser the way JavaScript does we are distributing a file. This brings the license sections on distribution into play.

We can see examples in the GPLv2 where is says “keep intact all the notices that refer to this License” and even the MIT License where it says “The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.” At the very least removing notices pointing the MIT license violates the spirit.

Does It Matter?

I look at it in two ways. Does it matter legally and does it matter to the developers who put the license and copyright statements in the code? I think we need to look at both to be a legal and kind community.

On the matter of legality I would recommend you go see a lawyer. If you aren’t going to get one look at what companies with lawyers are doing and saying. From a legal standpoint this was all first brought to my attention by a company I wasn’t working for whom I suggested they minify their JavaScript. Figuring out how they could handle the license issues was what was stopping them.

Being kind is entirely different. Several years ago I watched as a big name company used a JavaScript library and strip out the copyright and license information. The author was hurt because he felt violated. He contacted the company and got no response. He felt violated. On the flip side it did hurt him. Anyone else technically sound who looked at how this functionality worked would not see the authors name. He wouldn’t get credit nor would it cause anyone to seek him out to congratulate him, or try to hire him.

Seeing the legal impacts and personal ones I do think it matters.

What Can We Do?

I see three things we can do about this.

  1. Use the Speedy module to minify Drupal core JavaScript. No license notice exists so there is nothing to strip.
  2. Help get other Drupal contributed modules to ship with minified JavaScript files that properly handle licensing.
  3. Help us try to find a way to minify on the fly and preserve licenses.