allBlogsList

MiniMe JavaScript Minifier and Sitecore

MiniMe is a tool for dynamic minification, bundling and versioning of JavaScript and CSS files. It works well on the website but we encountered a lot of issues with Sitecore Desktop. For instance, the User Manager was grayed out, Security editor would not load. Upon investigation we found that Sitecore uses ComponentArt Grid to render the UI and it does not work well with minified scripts and css. I am not really sure why but we did fix it by modifying the MiniMe handler to only allow minification if the context site is not the "shell" site. Here is what the code looks like.

public class MiniHttpModuleEx : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new System.EventHandler(ApplicationBeginRequest);
        }

        void ApplicationBeginRequest(object sender, System.EventArgs e)
        {
            try
            {
                if (Sitecore.Context.Site != null && Sitecore.Context.GetSiteName().ToLowerInvariant() != "shell")
                {
                    string[] source = new string[] { ".js", ".css", ".png", ".gif", ".jpg" };
                    string url = HttpContext.Current.Request.RawUrl;
                    if (HttpContext.Current.Response.ContentType.Equals("text/html", StringComparison.OrdinalIgnoreCase) && !source.Any<string>(delegate(string ext)
                    {
                        return url.EndsWith(ext, StringComparison.OrdinalIgnoreCase);
                    }))
                    {
                        HttpContext current = HttpContext.Current;
                        Assembly assembly = ReflectionUtil.LoadAssembly("MiniMe");

                        if (assembly != null)
                        {
                            BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public;
                            var miniMeFilter = assembly.CreateInstance("MiniMe.MiniHtmlFilter", true, flags, null, new object[] { current.Response.Filter }, null, null);
                            Stream filterStream = null;
                            if (miniMeFilter != null && (filterStream = (miniMeFilter as Stream)) != null)
                                current.Response.Filter = filterStream;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("minime not initialized", ex, sender);
            }

        }

    }

You'll notice that the code creates an instance of the MiniMeFilter only if the context site is not "shell". Note: remember to update

<modules></modules>

section of the web.config to use this module instead of the default MiniMe module i.e. replace

<add name="MiniHttpModule" type="MiniMe.MiniHttpModule, MiniMe" />

with

<add name="MiniHttpModuleEx" type="<New Namespace>, <Assemblyname>" />