reactjs / React.NET

.NET library for JSX compilation and server-side rendering of React components
https://reactjs.net/
MIT License
2.28k stars 940 forks source link

Using ReactJs.Net in multisite solution #1273

Open RadikFayskhanov opened 2 years ago

RadikFayskhanov commented 2 years ago

I'm using these library versions:

Runtime environment:

Hi guys! We are having issues with configuring the react.NET library for the multisite solution. We are using Sitecore CMS where there are multiple sites configured within one app domain in .NET. Each site has its own front-end build with a separate output path, i.e. '~/dist/'. At the moment we are using the following code to configure react.NET:

public class ReactJsNetRequestHandler : HttpRequestProcessor
{
        private static readonly IDictionary<string, string> ReactPaths = new Dictionary<string, string>
        {
            { "SiteOne", "~/dist/siteOne" },
            { "SiteTwo", "~/dist/siteTwo" }
        };

        public override void Process(HttpRequestArgs args)
        {
            if (Sitecore.Context.Site == null || !ReactPaths.ContainsKey(Sitecore.Context.Site.Name))
                return;

            var reactPath = ReactPaths[Sitecore.Context.Site.Name];
            Configure(reactPath);
        }

        private static void Configure(string path)
        {
            ReactSiteConfiguration.Configuration
                .SetLoadBabel(false)
                .SetLoadReact(false)
                .SetReactAppBuildPath(path)
                ;

            JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName;
            JsEngineSwitcher.Current.EngineFactories.AddV8();
        }
}

The issue with the current code is it only works with the first initialized site, all subsequent sites are unable to find JSX resources.

Is it possible to have multiple configurations or dynamically loaded configurations?

Best regards, Radik.