David Ruttka

I make computers do things

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

1
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.

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

When I include these on the Home/Index view using

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

Only foo gets logged.

image

But if I downgrade to 1.0.0

1
2
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.

1
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.

This post originally appeared on The DevStop.