Sunday 22 July 2018

SXA : Disabling asset optimizer not working

1 comment
I’ve previously blogged about disabling Sitecore SXA asset optimizer. Recently, I’ve stumbled upon a situation where we have disabled SXA asset optimizer for SXA site in non-production environment but CSS styles and javascripts are still getting rendered in minified version - optimized.min.js and optimized.min.css.

I’ve made sure that asset optimizer was disabled for local SXA site. For troubleshooting, I’ve also disabled asset optimizer at global level but I have no luck. I’ve verified value of XA.Foundation.Theming.StylesConfigurationId and XA.Foundation.Theming.ScriptsConfigurationId settings and these settings were having correct value assigned.

XA.Foundation.Theming.StylesConfigurationId : The ID of the item that stores the configuration for style asset optimization. 
(/sitecore/system/Settings/Foundation/Experience Accelerator/Theming/Optimiser/Styles)
<setting name="XA.Foundation.Theming.StylesConfigurationId" value="{B3B10434-DACE-483E-B375-49C6D215723D}" />

XA.Foundation.Theming.ScriptsConfigurationId : The ID of the item that stores the configuration for script asset optimization.
(/sitecore/system/Settings/Foundation/Experience Accelerator/Theming/Optimiser/Scripts)
<setting name="XA.Foundation.Theming.ScriptsConfigurationId" value="{4110D103-861E-464F-A258-130A8FCF4C61}" />
Then I’ve peeked into source code of Sitecore.XA.Foundation.Theming.dll and checked implementation of AssetConfigurationReader.
public virtual AssetConfiguration ReadConfiguration()
    {
      AssetConfiguration assetConfiguration = new AssetConfiguration();
      Item designItem = ServiceLocator.ServiceProvider.GetService<IPresentationContext>().DesignItem;
      if (this.RequestAssetsOptimizationDisabled)
        return new AssetConfiguration()
        {
          ScriptsMode = AssetServiceMode.Disabled,
          StylesMode = AssetServiceMode.Disabled,
          RequestAssetsOptimizationDisabled = true
        };
      if (designItem == null)
        return assetConfiguration;
      bool? tristate1 = this.ParseTristate(designItem.Parent[Sitecore.XA.Foundation.Theming.Templates._Optimizable.Fields.StylesOptimisingEnabled]);
      bool? tristate2 = this.ParseTristate(designItem.Parent[Sitecore.XA.Foundation.Theming.Templates._Optimizable.Fields.ScriptsOptimisingEnabled]);
      assetConfiguration.ScriptsMode = this.GetConfiguration(designItem.Database.GetItem(ID.Parse(Settings.GetSetting("XA.Foundation.Theming.ScriptsConfigurationId"))));
      assetConfiguration.StylesMode = this.GetConfiguration(designItem.Database.GetItem(ID.Parse(Settings.GetSetting("XA.Foundation.Theming.StylesConfigurationId"))));
      if (tristate1.HasValue)
        assetConfiguration.StylesMode = !tristate1.Value ? AssetServiceMode.Disabled : AssetServiceMode.ConcatenateAndMinify;
      if (tristate2.HasValue)
        assetConfiguration.ScriptsMode = !tristate2.Value ? AssetServiceMode.Disabled : AssetServiceMode.ConcatenateAndMinify;
      return assetConfiguration;
    }
This code has pointed me to right direction. It seems that designItem was set to null. I’ve opened content editor and navigated to context page item in content tree. In the Designing section, Page Design field value was empty.


I’ve selected appropriate Page Design, saved my changes and published the sitecore item. Voila! Asset optimizer gets disabled and uncompressed CSS styles and javascripts start getting render. Comments and suggestions are most welcome. Happy coding!

1 comment :