2

I have created bundling in my project as follows : Have created a config under App_Start/BundleConfig.cs

public class BundleConfig
{
    [UsedImplicitly]
    public virtual void Process(PipelineArgs args)
    {
        RegisterBundles(BundleTable.Bundles);

    }        
    public static void RegisterBundles(BundleCollection bundles)
    {            
        bundles.Add(new ScriptBundle("~/bundles/js").Include("~/Assets/Project/scripts/main.js"));           
        bundles.Add(new StyleBundle("~/bundles/css").Include("~/Assets/Project/styles/main.css"));
    }        
}

and added the same in Pipeline

<initialize>
    <processor type="MyProject.Foundation.Assets.App_Start.BundleConfig, MyProject.Foundation.Assets" patch:before="processor[@type='Sitecore.Pipelines.Loader.ShowVersion, Sitecore.Kernel']" />
  </initialize>

So the bundling is working fine as expected.

But now I need to optimize them using a query parameter in the URL, example if I browse example.local?debug=true, I need to enable optimizations as false

So now I have added another config

public class BundleOptimizations : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {            
        EnableOptimizations();
    }       

    public void EnableOptimizations()
    {
        var debugMode = HttpContext.Current.Request.QueryString["debug"];
        if (!string.IsNullOrEmpty(debugMode) && string.Equals(debugMode, "true", StringComparison.InvariantCultureIgnoreCase))
        {
            BundleTable.EnableOptimizations = false;
        }
        else
        {
            BundleTable.EnableOptimizations = true;
        }

    }
}

and this config I have added in HttpRequestBegin pipeline

<httpRequestBegin>
    <processor type="MyProject.Foundation.Assets.App_Start.BundleOptimizations, MyProject.Foundation.Assets" patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']" />
  </httpRequestBegin>

But I am not able to get these bundles optimized using the query parameter. Please can someone help me with the same

Thanks

4

Sorry for providing a single line answer.

Here is what i did to get the optimization done :

In my question, I had mentioned about the BundeOptimizations which I had created inheriting HttpRequesProcessor and placing the same in the httpRequestBegin. So while I was investigaing on the same I came across the show config which has the following registered in the preprocess request :

<processor type="Sitecore.FXM.Pipelines.PreProcessRequest.HandleBundleRequest, Sitecore.FXM" resolve="true" patch:source="Sitecore.FXM.Bundle.config">

And when i decompiled the code, I could see that they were registering the bundles in this.

Then I tried to implement my optimization code inside the preprocess request as follows

public class BundleOptimizations : PreprocessRequestProcessor
{    
        public override void Process(PreprocessRequestArgs args)
        {            
            EnableOptimizations();
        }       

        public static void EnableOptimizations()
        {
          var debugMode = HttpContext.Current.Request.QueryString["debug"];
          if (!string.IsNullOrEmpty(debugMode) && string.Equals(debugMode, "true", StringComparison.InvariantCultureIgnoreCase))
          {
              BundleTable.EnableOptimizations = false;
          }
          else
          {
              BundleTable.EnableOptimizations = true;
          }

        }
}

and then updated the same in preprocessrequest config

<preprocessRequest>
    <processor type="Project.Foundation.Assets.App_Start.BundleOptimizations, Project.Foundation.Assets" />
</preprocessRequest>

Then i tried to add the URL with ?debug =true in it and it worked.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.