Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily disable bundling and minification at runtime?

I need to be able to temporarily disable bundling and minification for a single request for the purpose of debugging JavaScript & CSS Issues. I would like to do this at run time by adding a parameter to the QueryString like so..

http://domain.com/page?DisableOptimizations=true

Here's the approach I am considering.

protected void Application_BeginRequest(object sender, EventArgs e)
{
  // Enable for every request
  BundleTable.EnableOptimizations = true;

  // Disable for requests that explicitly request it
  bool disable;
  bool.TryParse(Context.Request.QueryString["DisableOptimizations"], out disable);
  if (disable)
  {
    BundleTable.EnableOptimizations = false;
  }
}
  • Are there any potential issues with the fact that I am setting this static property on every web request? (The web app will run on a web farm)
  • Are there better ways to handle this?
like image 512
jessegavin Avatar asked Jan 10 '13 19:01

jessegavin


People also ask

What must be done to enable bundling and minification?

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled. To enable bundling and minification, set the debug value to "false".

What are the different ways for bundling and minification in asp net core?

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.

How bundling and minification works in .NET core?

What is bundling and minification. Bundling and minification are two distinct performance optimizations you can apply in a web app. Used together, bundling and minification improve performance by reducing the number of server requests and reducing the size of the requested static assets.

Can we use bundling and minification with ASP NET web forms like MVC?

To optimize the performance of an application I found that bundling and minification can significantly improve the performance. It can be applied on MVC as well as in ASP.NET web forms.


1 Answers

Extending what I mentioned in a comment:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class OptimizationsDebuggingAttribute : ActionFilterAttribute
{
    private const String PARAM_NAME = "DisableOptimizations";
    private const String COOKIE_NAME = "MySite.DisableOptimizations";

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Boolean parsedPref;
        Boolean optimizationsDisabled = false;

        if (filterContext.HttpContext.Request.QueryString[PARAM_NAME] != null)
        { // incoming change request
            var pref = filterContext.HttpContext.Request.QueryString[PARAM_NAME].ToString();
            if (Boolean.TryParse(pref, out parsedPref))
            {
                optimizationsDisabled = parsedPref;
            }
        }
        else
        { // use existing settings
            var cookie = filterContext.HttpContext.Request.Cookies[COOKIE_NAME];
            if (cookie != null && Boolean.TryParse(cookie.Value, out parsedPref))
            {
                optimizationsDisabled = parsedPref;
            }
        }

        // make the change
        System.Web.Optimization.BundleTable.EnableOptimizations = !optimizationsDisabled;

        // save for future requests
        HttpCookie savedPref = new HttpCookie(COOKIE_NAME, optimizationsDisabled.ToString())
        {
            Expires = DateTime.Now.AddDays(1)
        };
        filterContext.HttpContext.Response.SetCookie(savedPref);

        base.OnActionExecuting(filterContext);
    }
}

then of course implementing it looks something like:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new OptimizationsDebuggingAttribute());
}

Or, if you prefer the hands-on approach:

[OptimizationsDebugging]
public ActionResult TroublesomeAction()
{
    return View();
}
like image 173
Brad Christie Avatar answered Oct 16 '22 17:10

Brad Christie