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!

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 What do you mean by 'bundles/components'?
I mean something (library? dll? msbuild task?) which wraps all this in a nice way into VS.
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: <Exec Command="java -jar yuicompressor.jar --type css -o &quot;myfile.css&quot; &quot;%myfile.css&quot;" />
2

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

2 Comments

Thanks for the response, do you mean this part of this bundle? telerik.com/products/aspnet-mvc/…
Yes. The Web asset manager is what handles all of that. I'm using the open source version of it.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.