Context-sensitive EnableOptimizations extensibility point?

Jun 7, 2015 at 3:19 PM
I was wondering if there is any reason why BundleTable.EnableOptimizations couldn't return a different value per web-request.
Background:
I have a site which has a quite lot of javascript.
I use a TFS server build to publish it to, say, http://www.mysite.com.
I have got a sub=domain/website pointer set up so that http://test.mysite.com points to the same application in IIS as the main site, so it will use exactly the same code and assets.

What I want to achieve is the following:
  • http://www.mysite.com serves up compressed, minified, bundled javascript
  • If a user reports a bug, I can go to http://test.mysite.com and it will serve me up the exact same javascript but unbundled and unminified, so I can debug the javascript
  • I only have to publish the application once. I'm currently achieving this by having two separate applications in IIS, but I have to either do two server builds, one to test and one to live, or remember to ftp the files across, which is a bit of a bind.
So, I thought a better way of solving this would be to download the source code for System.Web.Optimization and change BundleTable.EnableOptimizations to:
        public static bool EnableOptimizations {
            get {
                if (!_enableOptimizationsSet && HttpContext.Current != null)
                {
                    bool? overriddenValue = null;
                    var enableOptimizationsProvider = EnableOptimizationsProvider;
                    if (enableOptimizationsProvider != null && HttpContext.Current != null)
                    {
                        var contextWrapper = new HttpContextWrapper(HttpContext.Current);
                        overriddenValue = enableOptimizationsProvider.GetEnableOptimizations(contextWrapper);
                    }
                    return overriddenValue ?? (!HttpContext.Current.IsDebuggingEnabled);
                }
                return _enableOptimizations;
            }
            set {
                _enableOptimizations = value;
                _enableOptimizationsSet = true;
            }
        }

EnableOptimizationsProvider is an extensibility point that I have added in so in my client project I can then do:
    public class MyEnableOptimizationsProvider : IEnableOptimizationsProvider
    {
        private static Regex wantsTestRegex = new Regex(@"https?://test\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        public bool? GetEnableOptimizations(HttpContextBase context)
        {
            bool? returnValue = null;
            if (context != null)
            {
                var testOverride = (IsAllowedTest(context) &&
                                    WantsTest(context));
                returnValue = !testOverride;
            }

            return returnValue;
        }

        private bool IsAllowedTest(HttpContextBase context)
        {
            return context.User != null && context.User.IsInRole("Administrator");
        }

        private bool WantsTest(HttpContextBase context)
        {
            return wantsTestRegex.IsMatch(context.Request.Url.ToString());
        }
    }
It is backward compatible, so if EnableOptimizationsProvider doesn't get set (remains null), or returns null, then BundleTable.EnableOptimizations would return what it would have done without this.
This seems to work fine, however, I was just wondering if there is any reason to recommend against doing this? Or if you know of any situations where it could cause problems?