Fix FXM beacon

How to fix issues with FXM beacon from using azure web app URL instead of FXM Hostname

FXM beacon uses Azure web app URL instead of using the site domain name when using App Gateway sitting in front of CD web app. This prevents external sites from accessing the web app directly.

In this post, I have further customized the implementation of the Sitecore Beacon Loader Processor to use the FXM hostname instead of the web app name.

Code Changes

Implement an extension class by inheriting IBundleProcessor, the code in bold is the changes from the original processing code.

using Sitecore.Abstractions;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.FXM.Abstractions;
using Sitecore.FXM.Configuration;
using Sitecore.FXM.Pipelines.Bundle;
using Sitecore.StringExtensions;

namespace Bga.Sc.Foundation.SitecoreExtensions.Pipelines
{
    public class BeaconLoaderProcessor : IBundleProcessor
   {
        private readonly BaseSettings _settings;
        private readonly IFileUtil _fileUtil;
 
        public BeaconLoaderProcessor(BaseSettings settings, IFileUtil fileUtil)
        {
            Assert.ArgumentNotNull(settings, "settings");
            Assert.ArgumentNotNull(fileUtil, "fileUtil");
            _settings = settings;
            _fileUtil = fileUtil;
        }

        public void Process(BundleGeneratorArgs args)
        {
            string hostName = Settings.GetSetting("FXM.Hostname");
            if (string.IsNullOrEmpty(hostName))
                hostName = args.HostName;
            string endPoint = ResolveEndpoint(hostName);
            string beaconString = string.Concat("SCBeacon = new SCBeacon(\"", endPoint, "\");");
            string fileOutput = GenerateFileOutput(endPoint);
            _fileUtil.WriteToFile(fileOutput, beaconString, false);
            args.Bundle.Include(string.Concat("~", fileOutput));
        }

        protected virtual string GenerateFileOutput(string endPoint)
        {
            return _fileUtil.UnmapPath($ {_settings.Fxm().BundledJsFilesPath}/beacon_{endPoint.GenerateGuid():N}.js").Replace("//", "/");
        }
 
        protected virtual string ResolveEndpoint(string hostName)
        {
            string endPointUrl = _settings.GetSetting("Sitecore.Services.RouteBase", "sitecore/api/ssc/").TrimEnd(new char[] { '/' });

            return _settings.Fxm().Protocol + hostName.Trim('/') + "/" + endPointUrl + "/Beacon/Service";
        }
    }
}

Add a Patch config

Add a patch config file to update Sitecore to use the custom class for processing

<configuration xmlns:x="http://www.sitecore.net/xmlconfig/" xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
    <sitecore role:require="ContentDelivery">
        <pipelines>
            <bundle.beacon>
                <processor type="Bga.Sc.Foundation.SitecoreExtensions.Pipelines.BeaconLoaderProcessor, Bga.Sc.Foundation.SitecoreExtensions" resolve="true" patch:instead="*[@type='Sitecore.FXM.Pipelines.Bundle.BeaconLoaderProcessor, Sitecore.FXM']" />
            </bundle.beacon>
        </pipelines>
    </sitecore>
</configuration>

This change will update the FXM beacon to use the domain name set in the settings set on FXM.Hostname field in Sitecore configs.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *