I was tag-team wrestled by Keyvan Nayyari and Janko today. They wanted me to take up the challenge of writing about my programming history. Since they are two seriously cool dudes I decided to play along.

How old were you when you started programming?

Eighteen years young.

How did you get started in programming?

Red and white wine. That was my business ten years ago. I ran a small wine import business during college and my wines where so good I drank most of it myself. That’s when I knew I had talent. So I started programming.

What was your first language?

VB 5 or 6 - I don’t remember exactly.

What was the first real program you wrote?

The first version of my Prison Bitch Name Generator, written in VB 6, revolutionized modern English forever. There's an online version of it here made by someone else.

What languages have you used since?

VB.NET, C#, Java, PHP, Action Script, Lingo (this is a weird language) and all web oriented scripting- and mark-up languages.

What was your first professional programming gig?

The Prison Bitch Name Generator never took off commercially so I had to look for other venues. I started a web design business like 3 billion other people did during the IT bubble. My success was limited but I did manage to build about 50 websites and win a design award with one of them (I wasn't the designer but took full credit like the gentleman I am). The first website must have been for a small Norwegian pharmaceutical company located in Oslo if I remember correctly.

If you knew then what you know now, would you have started programming?

Definitely yes. It’s the most gratifying, creative and challenging thing and it makes me very happy every day.

What is the one thing you would tell new developers?

Rule #1. Buy the most expensive pair of Ray-Ban’s you can find. You probably look dorky like the rest of us programmers, but with a pair of Ray-Ban’s you look like a rock start. Don’t fall into the trap that any pair of shades will do no matter the price, and take pride in wearing them 24/7/365. 

Rule #2. When a girl ask what you do for a living, lie to her. Here are some good job titles I've had great success with over the years.

  • Pet detective (girls like animals for some reason)
  • Organic chef (girls like organic food for some reason)
  • Hybrid car designer (girls like the environment for some reason)
  • Bestselling novelist (girls like to read for some reason)

What’s the most fun you’ve ever had … programming?

That’s without a doubt when I learned about the semantic web and the process of teaching myself how to implement the various technologies in ASP.NET. It only became more interesting when I learned how to consume, aggregate and do cool things with semantic technologies on the web.

And with those words I’d like to pass the torch to James Avery, Al Nyveldt and John Dyer.

Some weeks ago I wrote how to minify and compress the WebResource.axd handler. In the comments of that post someone asked how to do the same with ScriptResource.axd. I thought about it and realized it could be done much smarter.

If you take a look at the HTML source at the CodePlex Issue Tracker page, you’ll see it references 13 WebResource.axd and ScriptResource.axd. That’s 13 different web requests on each page load. The ScriptResource.axd is using HTTP compression but WebResource.axd does not. None of them minifies the scripts.

Therefore it would be cool to create a plug ‘n play HttpModule that combines all resource.axd scripts into one single web request and then minify and compress them. It would result in smaller HTML, fewer web requests and optimized JavaScript code. If you’re a YSlow nazi like me, this is a must-have.

How it works

To make it work, we need both an HttpModule and an HttpHandler.

The module

The module looks for resource scripts in the HTML code and collects all the references from the src tag. It then constructs a new script tag containing all the references and points it to the HttpHandler. The new script tag is injected into the HTML where the first resource script was found. It keeps the order of the scripts intact.

All the original scripts are now removed and a single new script pointing to the handler is injected.

The handler

When the handler is requested by the browser it looks for a URL parameter containing the references to the original resource script. They are separated by a comma in the URL parameter. It then retrieves the content of each script using a HTTP request. Then content is then aggregated into a single string which then get’s minified. Minifying means that all comments and whitespace is removed from the script.

The string is then being cached so the handler only has to do it once per application life cycle. The output is then compressed and the appropriate header set so the browser will cache the file for 30 days. You can easily set the number of days in cache to whatever you see fit.

Implementation

Download the ScriptCompressor.cs file below and put it in your App_Code folder. Then add the following lines to your web.config:

<httpModules>
  <add type="ScriptCompressorModule" name="ScriptCompressorModule" />
</httpModules>
<httpHandlers>
  <add verb="*" path="*js.axd" type="ScriptCompressorHandler" />
</httpHandlers>

Download

ScriptCompressor.zip (3.43 kb)