Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS bundles do not render without EnableOptimization set to true

Tags:

I don't know if I'm doing something wrong but it's probably a bug inside MVC4. I wonder how can I fix this?

Working scenario

public class BundleConfig {     public static void RegisterBundles(BundleCollection bundles)     {         ScriptBundle scriptBundle = new ScriptBundle("~/js");         string[] scriptArray =         {             "~/content/plugins/jquery/jquery-1.8.2.min.js",             "~/content/plugins/jquery/jquery-ui-1.9.0.min.js",             "~/content/plugins/jquery/jquery.validate.min.js",             "~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",             "~/content/plugins/bootstrap/js/bootstrap.min.js",         };         scriptBundle.Include(scriptArray);         scriptBundle.IncludeDirectory("~/content/js", "*.js");         bundles.Add(scriptBundle);          BundleTable.EnableOptimizations = true;     } } 

@Scripts.Render("~/js")
Converts to (e.g. IT WORKS!)
<script src="/js?v=VeCPNK561DZp34yjmWbLrNM35Kf6gaNDl0xsMMC25BQ1"></script>

Not so much working scenario

public class BundleConfig {     public static void RegisterBundles(BundleCollection bundles)     {         ScriptBundle scriptBundle = new ScriptBundle("~/js");         string[] scriptArray =         {             "~/content/plugins/jquery/jquery-1.8.2.min.js",             "~/content/plugins/jquery/jquery-ui-1.9.0.min.js",             "~/content/plugins/jquery/jquery.validate.min.js",             "~/content/plugins/jquery/jquery.validate.unobtrusive.min.js",             "~/content/plugins/bootstrap/js/bootstrap.min.js",         };         scriptBundle.Include(scriptArray);         scriptBundle.IncludeDirectory("~/content/js", "*.js");         bundles.Add(scriptBundle);          // BundleTable.EnableOptimizations = true; // I could set it to 'false' for same result, it's false by default     } } 

@Scripts.Render("~/js")
Converts to (e.g. IT DOES NOT WORK!)
(nothing, couple of empty break lines)

like image 285
Stan Avatar asked Nov 19 '12 17:11

Stan


People also ask

How do you override the bundling setting of web config?

To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.

How to use bundle config in ASP net MVC?

BundleConfig.cs Shown below is the BundleConfig. cs file present in a default MVC5 application. This file contains RegisterBundles() method which is being called at Application_Start() event by global. asax.

What is bundling and minification in MVC with example?

Bundling and Minification are two performance improvement techniques that improves the request load time of the application. Most of the current major browsers limit the number of simultaneous connections per hostname to six. It means that at a time, all the additional requests will be queued by the browser.


2 Answers

If my understanding is correct, you should define your bundles using the "non-min" JavaScript files. When you enable optimizations it will swap the non-min files for the min files for you:

public class BundleConfig {     public static void RegisterBundles(BundleCollection bundles)     {         ScriptBundle scriptBundle = new ScriptBundle("~/js");         string[] scriptArray =         {             "~/content/plugins/jquery/jquery-1.8.2.js",             "~/content/plugins/jquery/jquery-ui-1.9.0.js",             "~/content/plugins/jquery/jquery.validate.js",             "~/content/plugins/jquery/jquery.validate.unobtrusive.js",             "~/content/plugins/bootstrap/js/bootstrap.js",         };         scriptBundle.Include(scriptArray);         scriptBundle.IncludeDirectory("~/content/js", "*.js");         bundles.Add(scriptBundle);     } } 

Optimizations are set to false when debugging, but are true by default in release mode.

like image 98
Fenton Avatar answered Oct 19 '22 18:10

Fenton


yes, It doesn't work properly with files like *.min.js.

"if you add a file to your bundle that has a name that ends in .min.js (like I did) and optimizations aren’t enabled (e.g. debug is set to true in web.config and BundleTable.EnableOptimizations has not been set to true), this file will be ignored (i.e no script include will be generated for it in your html)."

Here you can read full original response: http://blog.degree.no/2013/06/javascript-file-missing-resource-bundle-asp-net-web-optimization-framework/

like image 20
zenichi Avatar answered Oct 19 '22 19:10

zenichi