New Web Optimization Pre-Release Package on NuGet

Today we pushed a new [pre]release of ASP.NET Web optimization to nuget.org – version 1.1.0-alpha1. This release includes some bug fixes and also a few new features that we wanted to share sooner than later. You may have also noticed that WebGrease 1.3.0 was published to NuGet just a few days ago – 1.3.0 addresses several bugs found in the JavaScript and CSS minification process, so regardless of whether you choose to update Web optimization, make sure to update your WebGrease package.

Here are the new features included in 1.1.0-alpha1 –

CDN fallback

In the first version of the library, we enabled you to specify an alternate CDN location for a bundle and then tell the framework whether or not that CDN path should be used instead of the local path when BundleTable.EnableOptimizations=true (or the ASP.NET debug configuration attribute is set to false when not explicitly setting the EnableOptimizations property).

However, this feature didn’t address the case where you wanted to start with a CDN path and then fall back to the origin server in the case that the CDN was unavailable. In this latest release, we’ve added this support by way of a new property on the bundle, CdnFallbackExpression.  For example, consider the case where we want to reference jquery from a CDN location and then fallback to the local bundle in the case that jquery cannot be reached on the CDN. For this, we need the following entries in the BundleConfig class’ RegisterBundles method..

bundles.UseCdn = true;

BundleTable.EnableOptimizations = true;

var jqb = new ScriptBundle(“~/bundles/jquery”, “foohost”)

.Include(“~/Scripts/jquery-{version}.js”);

jqb.CdnFallbackExpression = “window.jquery”;

bundles.Add(jqb);

In this code, we’ve enabled both optimizations mode and CDN support; we’ve also specified the CDN path as “foohost” (a known invalid location) for the jquery bundle. We then simply need to add the fallback expression “windows.jquery”. As you already know, this represents the jquery object and it will be used in the following script generated by the framework.

<script>

(window.jquery)||document.write(‘<script src=”/bundles/jquery”><\/script>’);

</script>

This script will query to see if the jquery object exists (indicating that jquery was successfully fetched from the CDN), and if not, will write a new script tag into the DOM with the src pointed at jquery on my server.

Element template strings

It used to be that choosing to have full control over your HTML markup meant giving up debug/release support. We’ve eliminated that tradeoff in this latest release with the “RenderFormat” method.  This helper method accepts, in addition to the usual list of bundle references, a format string which represents the HTML to render. For example, to render the jquery bundle with HTML5 markup that takes advantage of the new async attribute, I can use the following:

@Scripts.RenderFormat(“<script src=’{0}’ async></script>”,”~/bundles/jquery”)

I’m now able to see that the async attribute is present when optimizations are turned off…

<script src=’/Scripts/jquery-1.7.1.js’ async></script>

…And, of course, when they are turned on…

<script src=’http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js’ async></script>

Virtual Path Provider

Finally, while it may not one of those scenarios that is broadly used, supporting ASP.NET’s virtual path provider is absolutely necessary for certain application types – a good example is CMS applications that may store resources in a database or a path that is different than what would be a direct mapping to the file system.  The new Web optimization framework release now supports VPP, and while I’m not going to show how to develop a custom VPP (that topic would probably take a post or 4 by itself), here’s how you configure the Web optimization framework to use it.

BundleTable.VirtualPathProvider = HostingEnvironment.VirtualPathProvider;

Note that this code example is a no-op, since we default to use the hosting environment’s VPP if you don’t specify your own.

What’s Next?

As we continue towards the stable 1.1.0 release, the one additional feature that we’re working on, which has been pretty highly requested, is support for build-time optimization. There is some support for this today via the executable that is included in the WebGrease package – however, it’s not a first class feature, and that’s what we want to enable. More to come as that feature gets a little more baked…

So take a look at the new alpha release and let us know what you think at http://aspnetoptimization.codeplex.com/

About Howard Dierking

I like technology...a lot...
This entry was posted in Web. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://tarkus.me/ Konstantin Tarkus

    I’d love to have an ability of generating bundles at build time and including hash tag into file names. So that instead of there was or something like that (without query string parameter)

  • Ken Burkhardt

    These features look great! I had a question regarding the CDN fallback:

    The example above shows a bundle with only one script i.e. JQuery.

    However if I create a bundle with multiple scripts how can I provide a fallback for each element?

    Do we have to have a bundle per script (which seems kind of silly)?

    Can we see an example of a bundle with multiple scripts with fallback functionality?

  • http://www.taritsyn.ru/ Andrey Taritsyn

    Hi, Steve!

    I recommend you to try the WebMarkupMin library – http://webmarkupmin.codeplex.com

    This library supports the minification of HTML, XHTML and XML code. In addition, there are modules that integrate it into ASP.NET: WebMarkupMin.Mvc and WebMarkupMin.WebForms.

  • http://www.multiinfotech.com/seo-services.html seo services

    We understand how important it is to maximise your business’s online potential, which is why our SEO services are designed to help customers find your business online. When you enlist the consulting expertise of an SEO company like SEO Agency USA, you will never have to worry again about losing clients because they can’t find you online. The cost effective SEO services offered through our SEO company are guaranteed to have a positive impact on your business.

  • Johnny

    This feature was mentioned on ASP.NET road map page. However it is not included in the recent web update. Any idea when this feature will be officially released

  • Hampus

    In the CDN Fallback example. Shouldn’t the fallback url include a query string also?
    Like this:
    (window.jquery)||document.write(‘’);

  • Hao

    Are you using Include with something like *.js? We did move the default ignore list entries to the DirectoryFilter IgnoreList which is only used when searchPatterns like *.js are used. My guess is you cleared out the main IgnoreList, but moved that functionality to a different filter to prevent getting in the way of explicit file includes.

  • http://twitter.com/danmiser Dan Miser

    Have you been working with libraries that only provide min versions of their assets (e.g. kenoui.com)? I had things working fine with System.Web.Optimization 1.0.0, and then I upgraded to test the 1.1.0-alpha release and the bundling is not including min files anymore. Known issue?

  • Anonymous

    no – at the moment, we process only CSS and javascript

  • Anonymous
  • tugberk

    Is there a specific blog that we can follow for WebGrease?

  • Steve Chan

    Will this release minify ASP.NET/MVC HTML output as well?

  • Noel Abrahams

    Thanks, Howard. Just installed it and got it working.

  • Anonymous

    yes, that work item has been fixed – just need to mark it in CodePlex

  • Noel Abrahams

    Hi, has http://aspnetoptimization.codeplex.com/workitem/12 been fixed? Is that what the “Virtual Path Provider” is about?

  • Noel Abrahams

    Hi, has http://aspnetoptimization.codeplex.com/workitem/12 been fixed? If not wtf? It’s the top voted item on your issue list. If it has been fixed then sincere apologies!

  • http://www.taritsyn.ru/ Andrey Taritsyn

    It is a pity that in the WebGrease 1.3.0 not fixed the Semantic CSS Minifier bug – http://aspnetoptimization.codeplex.com/workitem/2.

  • Anonymous

    Reached out to the WebGrease team and they are currently working on some content about the spriting features. I’ll post that link as soon as there’s some content available.

  • http://dmytroduk.com/ Dmytro Duk

    Great news about VPPs and CDN fallback!

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1215

  • tugberk

    I think that should have been WG.exe. Sorry, just realized that.

  • tugberk

    Thanks for the post.

    Is it possible for you to get someone to have a blog post out there which explains the WebGrease.exe sprite features in details?

    WebGrease.exe is a great tool as an idea but it is pretty useless when it comes to actually using it. It is silent and doesn’t give you any information about the process.