Posts from Technology

Many make software… but far fewer make it well.

May 29

Compression (deflate) and HTML, CSS, JS Minification in ASP.NET

As I’ve already demonstrated, I like performance. So I cache and compress a lot. When I was put onto an ASP.NET project at work, I obviously wanted to optimize the site, so here’s what I did.

Taking some hints from Y! Slow, I decided I wanted to:

  • Get rid of all the MS AJAX/toolkit javascript, as we used jQuery instead
  • Combine all the javascript into one request
  • Combine all the CSS into one request
  • Minify the CSS
  • Minify the javascript
  • Minify the HTML
  • Deflate everything (gzip is slightly larger, and all modern browsers support deflate, so I just ignored gzip)

I followed the directions outlined at this site to override the ScriptManager and prevent it from including the Microsoft AJAX javascript. Removing uunsed code is always a good thing.

Combining the javascript was easy. Starting in ASP.NET 3.5 SP1, ASP.NET’s ScriptManager supports the CombineScript tag inside of it. That was easy.

Combining the CSS was not so easy, as there’s no such thing in ASP.NET as a “ScriptManager.” I had two options: make a CSS manager (and use it everywhere), or figure out another way. Never taking the easy route when there’s a more interesting (and more front end developer transparent) way, I decided to make a filter (implementer of IHttpModule) to find all the “<link>” tags in the page header and replace them with one “<link>” to a combined CSS handler (which I called “CssResource.axd” to parallel ScriptManager’s “ScriptResource.axd”). Then, in my IHttpHandler implementation which handles CssResource.axd, I read the querystring, grab the requested CSS files from the file system, combine them into one string, and return them. CSS combining done.

For minifying the CSS and Javascript, I used the C# version of YUI Compressor. I used the original (Java) YUI Compressor before, and had a great experience, so picking this version was a no-brainer. In my aforementioned filter, I intercept requests for “ScriptResource.axd” and “CssResource.axd,” apply YUI Compressor to the response content, cache the result (so I don’t need to minify every single request), then return.

I also minify inline (as in, mixed with HTML) CSS and Javascript. Also in my filter, if the return type is HTML, I scan for “<script src” and “<link rel=’stylesheet’ src=” and minify their contents. This minification does have to happen for every request to that page, unless that whole page is cached.

Finally, the last thing the filter does is check if the browser accepts deflate compression. If it does, the filter compresses the stream. In the case of “ScriptResource.axd” and “CssResource.axd” requests, the deflating is done before the response is cached, so requests for those resources don’t need to be re-deflated for every request (their content is static, unlike regular html requests, so caching the whole response is okay).

The initial (cache empty) page load was 780k before I started. When I had finished, the page load was only 234k - a 70% decrease.

You can download the code from this site. To use it, you need to modify your web.config.

<system.web>
<httpModules>
<add type="CompressionModule" name="CompressionModule" /><!--This must be the last entry in the httpHandlers list-->
</httpModules>
<httpHandlers>
<add verb="GET,HEAD" path="CssResource.axd" validate="false" type="CssResourceHandler"/>
</httpHandlers>
</system.web>

I cannot claim 100% credit for all of this work. I got many ideas from just browsing web search results, trying things out, and combining ideas from various sources. If I have not credited you, and I should have - I apologize, and will be happy to do. But I can say, that I did not just “copy and paste” this from anywhere - I’m confident that this work cannot be classified as a derived work of anything else. With that in mind, I release it into the public domain.

This post was originally published on my blog at http://candrews.integralblue.com/2009/05/compression-deflate-and-html-css-js-minification-in-asp-net/ - please comment and discuss this post at that location. Thanks!

April 22

Prognosticating about Oracle’s Sun Takeover

Monday’s announcement of Oracle’s takeover of Sun Microsystems was nothing short of a mild earthquake in the world of computing. Our hometown of Boston was celebrating Patriots’ Day and the Boston Marathon was keeping us all riveted. So after three days of mulling, thinking and tossing around ideas of what this all means, here’s my take of where this may lead.
(more…)

April 14

Webcam to the Rescue

Would-be burglar captured on webcam carrying what appears to be Wii motes in his hands.
Would-be burglar, Wii motes in hand, is captured on live webcam.

In the age of real-time information broadcasting, who wins when it comes to law breakers and law enforcers? This YouTube video from a Florida woman’s webcam as her home gets burglarized got me wondering: will whoever has the more powerful live surveillance technology — the ”all seeing eye” — always be the winner?

Think of it this way. There are more and more ways to track live information between live webcams, GPS services like Loopt and Google Latitude, and even Twitter (if you can trust the source).  Both lay people and law enforcement agencies are turning to these tools for information about safety. The Boynton Beach PD relied on this live webcam technology to respond to the burglars in the act and, through their own YouTube channel, have also published it as a PSA to educate the public about protecting their homes. Other examples like this abound: an LA firefighter monitors Twitter for reports of fires and even used real-time tweets from citizens during the 2007 Griffith Park fires to relay information to his crew on the ground.

Despite all the greater good these technologies can serve, I can’t help but wonder what would have happened, if say, the would-be burglars had their own system for tracking the whereabouts of the BBPD and knew they were headed to the house? Could they have gotten away with it? Could these technologies actually be used against law enforcement? So far, I haven’t personally heard of any examples, though I suppose one might be able to tune into the same radio channel as the police to get the same effect. But everyday as I look around me and see all the mobile phones with built-in cameras attached to each person’s ear, hand, and hip, I can’t help but wonder what the world would be like if each one of these devices could stream live footage 24/7 to anyone anywhere in the world. What, if anything, would happen then?

March 17

A better way to test your pages on different Browsers

I have had the opportunity to test several websites on multiple browsers over the years. My biggest problem was testing all my pages on the different browsers without having to download and install the different browsers onto my machine. Most clients will request for their web pages be tested on at least 3 of the following browsers:

1.       Internet Explorer (IE)

2.       Firefox (FF)

3.       Safari

4.       Opera

5.       Chrome

 

Although some browsers like FF tend to be consistent even when there is an upgrade to the latest version, others like IE have proven to be a real pain when upgraded to the next version. For a long time IE6 dominated the browser market, but recently, FF have overtaken IE6, IE7, and IE8 combined together. Never the less a lot of clients still want their pages tested properly on IE6 since it is still being used by approximately 17% of customers. Even testing IE6 pages when you have upgraded to IE7 is not very easy because the portable or stand-alone IE6 plug-ins are not very good and often crash your computer. I have used two of the more popular IE6 testing tools and they are: multiple IEs and IE tester. But they have fallen short of expectations and have proven unreliable especially IE tester.

web_statistics

For a while now, I have being looking for a tool that I can use to test my pages error free and came across a new browser testing Webapp know as Xenocode. Xenocode browser sandbox can run any browser from the web and it is especially easy to run and install. It does a really good job of displaying your web pages as if you were running the real version on your machine.  The browsers supported are IE8, IE7, IE6, FF3, FF2, Google Chrome, Apple Safari, and Opera. This webapp makes it so easy to test your pages against the browsers mentioned above quickly and efficiently. I have also provided a link to the website where you can download the apps. Try it on your project and comment on your experience.

xenocode2

 

http://www.xenocode.com/browsers/

March 17

Inversion of Control/Dependency Injection with PHP

Over the last several years the design paradigm of inversion of control/dependency injection has gone from an interesting design exercise to major league time savings and making complex things less complex (and at the same when done wrong, making complex things even more complex.) Developers in the Java world have enjoyed Spring, the de facto library that makes all of the promises of IoC/DI possible and rather straight forward.

However, with the continual maturity of OOP in PHP the concept of inversion of control/dependency injection has been slow to catch on. There have been attempts at providing IoC/DI functionality to PHP but many have just withered away and died. One such framework that may show some significant promise is Flow3, produced by the community of developers who have been slaving away at Typo3, a long running, incredibly feature-rich open source enterprise CMS. However, to date, Flow3 is very much like vaporware. Oh sure you can download it, but documentation is scarce to non existent and its missing one thing that I believe would slow its adoption, its similarity to Spring.

Spring got it right, by my estimation. The notion of wiring an application together via an XML file is spot on and truly enables developers to “write to interfaces” without ever having to concern themselves with the concrete implementations. The better code design, testing and time savings become very apparent once fully in practice. This shouldn’t just be something that Java (or .NET) developers enjoy. PHP is a major piece of technology, so with that said, I’ve taken the time and out of a graduate school project I recently finished devised a rudimentary IoC/DI container written in PHP. Currently it only supports a limited set of features, but uses the same concepts as used by Spring. Enter, phpBoing, which right now is quite simple and is just an academic curiosity, but with some work it could grow into a full fledged mature application development framework. No it is NOT meant to be another MVC framework. There are too many of those already, in fact, this system simply provides an object registry that lives in memory, defined through an “application context” XML where you can “inject” object dependencies via XML syntax, thus enabling you to “wire” your PHP applications together in XML (as opposed to doing so in actual PHP code).

In invite you to take a look, stab around at it, and if so inclined, contribute code.

Technorati Profile

Browse posts by month

Browse by author

We're always looking for rockstars

Come take a look at careers with Molecular