How to bundle css and js files in Sitecore MVC

Bundling is a feature in .NET that allows us to combine or bundle multiple files into a single file. We do bundle files to improve performance of a site. Fewer files also means less HTTP requests which means faster performance. To use bundling in Sitecore MVC you are going to have to implement a custom pipeline. So, without further due here we go.

RegisterBundles pipeline

In this custom pipeline we setup our bundles.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using Sitecore;
using Sitecore.Pipelines;

namespace Local.Sitecore.Website.Pipelines.Loader
    public class RegisterBundles
        public virtual void Process(PipelineArgs args)

        private void Register(BundleCollection bundles)
            bundles.Add(new ScriptBundle("~/bundles/js").Include(

            bundles.Add(new StyleBundle("~/bundles/css").IncludeDirectory(
                      "~/includes/css", "*.css"

Configure pipeline

Let's register our pipeline and add "bundles" to the IgnoreUrlPrefixes setting.

<?xml version="1.0"?>
<configuration xmlns:patch="http://local.sitecore.net/xmlconfig" xmlns:set="http://local.sitecore.net/xmlconfig/set/">
        <processor patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeGlobalFilters, Sitecore.Mvc']"
           type="Local.Sitecore.Website.Pipelines.Loader.RegisterBundles, Local.Sitecore.Website" />
      <setting name="IgnoreUrlPrefixes" value="/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing|/sitecore/service/xdb/disabled.aspx|/bundles" />


And this is how we use bundling in the layout.

@using Sitecore.Mvc
@using System.Web.Optimization
<!DOCTYPE html>
    <title>Bundle me in Sitecore MVC</title>

That's all for today. Keeping it simple :)

This is a personal blog. The opinions expressed here represent my own and not those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.. In addition, my thoughts and opinions change from time to time I consider this a necessary consequence of having an open mind. This blog disclaimer is subject to change at anytime without notifications.