ASP.Net MVC: Don't forget to enable bundle optimizations

Monday, February 29, 2016
by Sean McAlinden

This is a simple post about enabling optimizations for your JavaScript and CSS bundles.

The reason I have given this post the title "Don't forget to enable bundle optimizations" is probably pretty clear, I have forgotton to enable it a few times ;).

Anyway, here it is:

BundleTable.EnableOptimizations = true;

This just needs to be called on application startup for example:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        BundleTable.EnableOptimizations = true;
    }
}

NOTE: It is worth adding the boolean value to your web config and setting to true on your release transform.

What does it do?

It does a few very useful tasks:

Minimization

By enabling it to true your JavaScript and CSS files will be minimized.

Cache Busting

By enabling it to true the urls for your JavaScript and CSS bundles will include a cache busting version string.

This means the client browsers will be forced to get the new versions of your CSS and JavaScript rather than using any cached version they currently hold.

Summary

So along with the other million things you need to remember to do for your deployments, make sure you add this to the list.