5

Good day!

I'm looking for solution to combine, minimize and gzip CSS and JavaScript files. It seems they come in two forms:

  • In form of ASP.NET handler\module with processing files on the fly (with caching results)
  • In form of VS build tasks (to perform processing while building)

Generally I'm ok with either.

I've looked on a number of solutions (and I use ASP.NET handler from this article http://www.codeproject.com/KB/aspnet/httpcompression.aspx a lot), but maybe something "must have" came out and I've missed it.

Thanks in advance!

artvolk
  • 9,448
  • 11
  • 56
  • 85

3 Answers3

6

Here's my advice to you: use build tasks and HTTP cache the output.

In terms of build tasks, you'll want to check out your favorite JavaScript minifier (my favorite is Google Closure Minifier) that has a command line utility that you can just plug into your project file, MSBUILD file or NANT file. Same deal with CSS (I personally use Yahoo! YUI Compressor). If you're into using LESS, you can certainly combine this with the YUI compressor. To optimize images, I'd use optipng. There's directions on how these guys work on their individual sites.

Now, after you have these files all nice and optimized, you'll want to output them using a handler or controller action for MVC. To set the expiration so that subsequent requests will default to the file downloaded on the first request, you'll want this to run in your code:

Response.ExpiresAbsolute = DateTime.Now.AddYears(1);

More than likely you'll want a cache-buster strategy so that you can change the content files. You'd do this by passing a random parameter to your handler. There are a few different ways to go about this... just Google it.

Hope this helps.

zowens
  • 1,566
  • 9
  • 10
  • Thanks for the response, I've played a lot with assembling such pipelines by hand for processing CSS and JS (for both ASP.NET and PHP), have you seen any bundles/components? – artvolk Jan 11 '11 at 08:59
  • @artvolk What do you mean by 'bundles/components'? – zowens Jan 12 '11 at 03:21
  • I mean something (library? dll? msbuild task?) which wraps all this in a nice way into VS. – artvolk Jan 12 '11 at 07:13
  • Use the Exec task in MSBUILD to call out to the various executable to do the optimizing. You'd do this in the BeforeBuild target in your project file (which uses MSBUILD). For example, YUI Compressor looks like this: – zowens Jan 15 '11 at 20:13
2

I'm using the telerik mvc components for small-medium sites. It was simple to add and configure with NuGet.

Brian Cauthon
  • 5,524
  • 2
  • 24
  • 26
  • Thanks for the response, do you mean this part of this bundle? http://www.telerik.com/products/aspnet-mvc/web-asset-managers.aspx#combination – artvolk Jan 11 '11 at 08:58
  • Yes. The Web asset manager is what handles all of that. I'm using the open source version of it. – Brian Cauthon Jan 11 '11 at 18:18
2

Moth can (among other things) handle all your javascript / css requests on the fly. See Wiki: Javascript.

Best of all, it can also put all javascript at the bottom of the page, including parts you write in your partial views! Wiki: Inline script.

Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120