ScriptBundle subdirectories no longer work in Microsoft.Web.Optimization 1.1.0 when optimizations are off

Just a heads up for now. I’ll track down where to report this or to whom in the morning. Excuse any spelling, grammar, sloppiness of screenshots, etc., I’m doing this pretty quickly before heading out for the day.

tl;dr If you create ScriptBundles that recurse through subdirectories, and if you want to keep optimizations off for debug, stay on Microsoft.AspNet.Web.Optimization 1.0.0. If you go to 1.1.0, force EnableOptimizations=true, or your scripts from subdirectories won’t come through.

I had the good fortune to do a File > New Project a couple of days ago. When I did, I grabbed the bundling package

Install-Package Microsoft.AspNet.Web.Optimization

To keep this simple (i.e., to provide a reduction), I am including just two scripts here. Scripts/foo.js logs “foo”, and Scripts/subdir/bar.js logs “bar”.

image

I create a simple, standard ScriptBundle that should pick these up.

BundleTable.Bundles.Add(new ScriptBundle("~/scripts/bundle")
.IncludeDirectory("~/Scripts", "*.js", true));

When I include these on the Home/Index view using

@System.Web.Optimization.Scripts.Render("~/scripts/bundle")

Only foo gets logged.

image

But if I downgrade to 1.0.0

Uninstall-Package Microsoft.AspNet.Web.Optimization
Install-Package Microsoft.AspNet.Web.Optimization –version 1.0.0

Then they both get logged.

image

If we update back to the 1.1.0 package, we can look in Fiddler and see the problem.

image

Optimization is recognizing that there’s a bar.js somewhere, but the bundle loses track of the fact that it’s in a subdirectory. Of course, this is in debug mode where the optimizations are not enabled and the scripts are still delivered separately. Let’s see if enabling the optimization to deliver the bundle itself will help us.

BundleTable.EnableOptimizations = true;

Now both statements are logged.

image

Of course, looking at the delivered bundle, we can see that the contents of both scripts are there. In other words, you wouldn’t see this problem in Release or if you’re explicitly forcing the optimizations into effect.

image

I lost a fair bit of time the other day thinking that I had mistyped something setting up my ScriptBundle or a similar mistake. Even after I saw the 404s in Fiddler, I blamed myself and kept looking for my own error. Then I realized that the package version was different and took this wild guess =) Hope this helps save someone from similar trouble until the problem is resolved.

About these ads

About David Ruttka

I've been "making computers do things" since I first saw King's Quest on a 286 PC in the mid-80's, but I turned it into a career just over a decade ago. While the majority of my experience has been on the Microsoft stack (C#, .NET, ASP.NET), I've recently been diving deeper into JavaScript and exploring the Ruby universe. Occasionally, I'll do a public speaking gig or write a blog post. When I'm not coding, I enjoy spending time with my family, watching hockey, and playing the occasional video game. You can also find me on Stack Overflow, Google Plus, and Twitter. Microsoft Certified Programming in HTML5 with JavaScript and CSS3 Specialist; MCPD Windows Developer 3.5
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to ScriptBundle subdirectories no longer work in Microsoft.Web.Optimization 1.1.0 when optimizations are off

  1. This article have many useful informations that is helpful to me. I have subscribe will check back every day for new post.

  2. Anonymous says:

    I found this article while researching this very problem. I was on v1.1.1. Before downgrading, I checked to see if a new version was out first. There was so I installed v1.1.3 and it is working as it should!

    Thank you for this information. It greatly minimized my effort to find a solution.

  3. Thank you both (OP and “Anonymous”) — I too ran into this issue and you saved me a bunch of time.

  4. Linskin says:

    Yes, this is indeed an annoying “feature”. Any chance of a fix Microsoft?

Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s