I am using the ASP.NET Bundling mechanism:
BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/Master-js").Include(
"~/Scripts/respond.min.js",
"~/Scripts/jquery.form.js",
"~/Scripts/jquery.MetaData.js",
"~/Scripts/jquery.validate.js",
"~/Scripts/bootstrap.js",
"~/Scripts/jquery.viewport.js",
"~/Scripts/jquery.cookie.js"
));
I want this to happen if the build is in release. If the build is in debug, I want the un-minified individual files to load so debugging would be easy.
The only way I have been able to do this is to write the following in my view:
<% if(HttpContext.Current.IsDebuggingEnabled)
{
Response.Write("<script type='text/javascript' src='../../Scripts/respond.min.js'></script>");
Response.Write("<script type='text/javascript' src='../../Scripts/jquery.form.js'></script>");
Response.Write("<script type='text/javascript' src='../../Scripts/jquery.MetaData.js'></script>");
Response.Write("<script type='text/javascript' src='../../Scripts/jquery.validate.js'></script>");
Response.Write("<script type='text/javascript' src='../../Scripts/bootstrap.js'></script>");
Response.Write("<script type='text/javascript' src='../../Scripts/jquery.viewport.js'></script>");
Response.Write("<script type='text/javascript' src='../../Scripts/jquery.cookie.js'></script>");
}
else
{
Scripts.Render("~/Scripts/Master-js");
}
%>
As you can see, I am repeating myself here. Is there a better way?
Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)
You can see ScriptBundle and StyleBundle objects we are using for bundling the js and CSS types of files. ScriptBundle: is responsible for Script files i.e javascript (JS). StyleBundle: is responsible for stylesheet files i.e CSS.
To improve the performance of the application, ASP.NET MVC provides inbuilt feature to bundle multiple files into a single, file which in turn improves the page load performance because of fewer HTTP requests.
Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.
By default, when using Scripts.Render()
on your view and if the application has debugging enabled (debug="true"
in the web.config) the files will not be bundled or minified. Also a individual script tag will be rendered for every asset in that bundle.
You can manually enable/disable bundling and minification by using System.Web.Optimization.BundleTable.EnableOptimizations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With