Bundling and Minifying Inline Css and Js

        Introduction:


                    Application performance is the very important factor for an application success. Yahoo's Best Practices for Speeding Up Your Web Site is a great resource for increasing your application performance. Out of these practices, 'Putting Stylesheets at the Top', 'Putting Scripts at the Bottom' and 'Minifying(external and inline) JavaScript and CSS' are very important practices. Minifying inline css and js is also very important. From Yahoo Best Practices page 'In addition to minifying external scripts and styles, inlined <script> and <style> blocks can and should also be minified. Even if you gzip your scripts and styles, minifying them will still reduce the size by 5% or more. As the use and size of JavaScript and CSS increases, so will the savings gained by minifying your code '. So, in this article, I will show you how to minify and bundle(combine all css/js) your inline css/js.


        Description:


                    Open your ASP.NET application(WebForm or MVC) and install BundleMinifyInlineJsCss nuget package.


                     


                    Then register the response filter. If you are using WebForm, you can register response filter in a master page and if you are MVC, you can use register response filter in an action filter.  


    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Filter = new BundleAndMinifyResponseFilter(Response.Filter);
        }
    }
    public class BundleMinifyInlineCssJsAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Filter = new BundleAndMinifyResponseFilter(filterContext.HttpContext.Response.Filter);
        }
    }
    [BundleMinifyInlineCssJs]
    public class HomeController : Controller

                    Now just run your application. If a page view-source is,




                    After using the above response filter, it will become,


.



                    Note in the above screen the inline css moved to top, inline javascript moved to bottom and inline javascript/css is minified and bundled. 


        Summary:


                    In this article, I showed you how to you quickly and easily put all your inline css at the top, put all your js at bottom and minifying/bundle all your inline  javascript/css using a response filter. Hopefully you will enjoy this article too.



Published Wednesday, July 25, 2012 1:40 PM by imran_ku07

Comments

# re: Bundling and Minifying Inline Css and Js

Thursday, July 26, 2012 5:53 PM by ricka6

Nice addition.

# re: Bundling and Minifying Inline Css and Js

Friday, July 27, 2012 7:17 AM by Ed Spencer

Very cool. Are there any performance considerations we should consider when running this response filter?

# re: Bundling and Minifying Inline Css and Js

Friday, July 27, 2012 8:10 AM by mitsbits

It doesn't seem to work with Razor / MVC 4

I get a Filtering is not allowed exception and I moved the code to OnActionExecuting as well.

Then I Checked if the Result is View or Partial View, and applied the filter in those cases but no luck!!!!

Any ideas?  

# re: Bundling and Minifying Inline Css and Js

Friday, July 27, 2012 10:15 AM by imran_ku07

@mitsbits, I have checked it is working. Please send me a sample application at 'imran_ku07 at yahoo.com' if it still not work for u

# re: Bundling and Minifying Inline Css and Js

Friday, July 27, 2012 10:18 AM by imran_ku07

@Ed Spence, the response filter is moving the script/css. So, I don't thnik it has any performance impact.

# re: Bundling and Minifying Inline Css and Js

Friday, July 27, 2012 2:27 PM by asad.malik

Wow.. great

# re: Bundling and Minifying Inline Css and Js

Friday, July 27, 2012 5:13 PM by Eric

Definitely a step in the right direction. I'd like to see:

var a="data1",b="data2";a="data1",b="data2"

# re: Bundling and Minifying Inline Css and Js

Saturday, July 28, 2012 6:41 AM by Domlia

Not that bad idea, but not so good made up.

# re: Bundling and Minifying Inline Css and Js

Saturday, July 28, 2012 8:00 AM by imran_ku07

@Domlia, how would you think to make it better. Response.Filter was the quickest way for me.

# re: Bundling and Minifying Inline Css and Js

Monday, July 30, 2012 3:24 AM by Mark.

@mitbits - I had the same problem which turned out to be child actions.  Update your attribute code to the following:

   public class BundleMinifyInlineCssJsAttribute : ActionFilterAttribute

   {

       public override void OnResultExecuting(ResultExecutingContext filterContext)

       {

           if (filterContext.HttpContext.Response.IsRequestBeingRedirected)

               return;

           if (filterContext.IsChildAction)

               return;

           filterContext.HttpContext.Response.Filter = new BundleAndMinifyResponseFilter(filterContext.HttpContext.Response.Filter, false, true, false);

       }

   }

Worked for me.

# re: Bundling and Minifying Inline Css and Js

Tuesday, July 31, 2012 5:53 AM by amincold

Great work.

# re: Bundling and Minifying Inline Css and Js

Wednesday, August 01, 2012 10:26 AM by quynhnh

Great work!

I have been waiting for sometime for a solution for inline js and css, which are a big part of my project.

Thank you.