Custom JavaScript KungFu and management for Microsoft MVC.NET

Rails community got a lot things right for web application. One of them is the way they manage JavaScript files. I’m working on a web project where I use a lot of JavaScript and MVC.NET as Web Framework. Team is quite big and JavaScript is getting out of control. Files are everywhere with a lot of code. And then when it goes into production it is the best to have it as one compressed and minified file. So, here what we did.

Where am I, ups, production, I better behave

The easiest possible way of telling the app where it is running will be in Web.config. There is already a bunch of custom setting in there, so why not just have one more. When the time comes based on the Environment information we can decide if we will still spit out single un-minified files for a development environment or serve one minified file for lightning fast production performance.

Continue reading

WordPress page load time tweaks ended in … failure

Why was I trying to speed it up on a first place. Well, some of you might be aware of the fact that me and my Family moved to Sydney. We will be here until May. My blog and my hosting is in UK. Broadband in Oz is far below random average back in UK. I was never bothered to check my own blog for response time as it was always good. Trying to load my blog main page I was shocked waiting for few minutes for it to appear on a screen.

The beginning and general enthusiasm

What I did is a little analysis on a page with Firefox and YSlow. It turned out that a lot of time takes for JavaScript files to load. About 8 files that are not too big but for some bizarre reason takes ages. YSlow suggested to combine them into one file and compress.

That’s fine. Sounds like a plan. Grab all the JS files combine them into one and then compress. Turned out more complicated than one would think.

There was no JavaScript that comes with bare WordPress to compress and combine with other files. The JavaScript that I got on my blog was the one from additional plugins I have installed.

After a quick googling I found out that there is a WordPress plugin for JavaScript compression. It is called WP_JS. It works quite sweet, whenever you got a JS file and you need to add it to your post all you do you put this line:

<script src="<?php wp_js('path/to/js/file.js'); ?>"type="text/javascript"></script>

The problem with this one is that it will only work for a files from within your current theme folder.

JS files I would like to compress into one file are those inside plugins. Seems like WP JS doesn’t have access to that. Oh well, first hack then. Copy all the files to one folder that would be accessible to JavaScript compression plugin.

imageWhen I finished I had to go trough all the plugins and comment out the JavaScript imports. Finished with this task I moved to getting all the JS files in a header as one compressed file. So in other hands all the plugin JavaScripts would be compressed and loaded in one file.

When I done that it turned out that there are JavaScript errors on a page. Some plugins are spitting more to a HTML then just a JavaScript imports and they depend on a stuff inside those imports. In other words, one big mess. Impossible to decouple and extract as one file.

Good idea

It would be absolutely awesome if I could apply some sort of a filter for a HTML content before it is being spit out to a browser, grab all the JS imports, create one big JS on a flight, compress it and then send to a browser.

With a knowledge I got about WordPress it seems impossible at the moment.

Failure and what I ended up with

So I ended with disabling plugins. There is only few enabled with very little JavaScript.

Last final solution is to redo the plugins so the JavaScript they are using will be suitable for a single file compression.

I don’t have time for that, so … no plugins instead 🙂

Take care, Gregster

Ps. I’m not going to try to achieve anything similar with CSS :-