3

Is there a easy way to have @Html.RenderFormStyles() and @Html.RenderForScripts() return the styles and scripts with a hostname - specifically with a CDN hostname of my choice ?

I have a website with lots of forms and the visitors will benefit a lot if those scripts/styles are loaded from a CDN.

Did anyone tried to do it? How?

7

You can override the processor in forms.renderForm pipeline that parses and builds the list of files to include, and prepend the CDN url of your provider.

Create a new pipeline processor, inheriting from the existing Sitecore one and override the CreateFullFilePath method:

public class IncludeFiles : Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.IncludeFiles
{
    public string CdnProvider { get; set; }

    protected override string CreateFullFilePath(string filePath, bool script)
    {
        string scriptPath = base.CreateFullFilePath(filePath, script);
        return CdnProvider + scriptPath;
    }
}

Add a new config file to patch the existing processor and to set the URL of your CDN provider:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="ContentDelivery">
    <pipelines>
      <forms.renderForm>
        <processor patch:instead="*[contains(@type, 'IncludeFiles')]" 
                   type="MyProject.Pipelines.IncludeFiles, MyProject" resolve="true">
          <CdnProvider>http://azure.edgeserver.net/</CdnProvider>
        </processor>
      </forms.renderForm>
    </pipelines>
  </sitecore>
</configuration>

Note that the config uses Role Config to only patch in Content Delivery, you probably still want to serve directly from the server in Content Management server but you can change/remove that if you wish.

| improve this answer | |
  • Oh much nicer, I like this – Dave Goosem Dec 14 '19 at 6:09
  • @jammykam thank you for the answer. I have totally missed the configs and the fact you can an be override processors from forms.renderForm – Sebastian Tecsi Dec 16 '19 at 9:17
1

I've not tried it before, but it's an interesting question.. I'd try a rewrite rule to rewrite the generated styles/scripts output domain to your CDN domain as a first test.

Failing that, if you crack open the Sitecore.ExperienceForms.Mvc dll and have a look at what they're dong, you can likely just create your own variation of those extensions and bake in your CDN logic..

@Html.RenderFormScript() and @html.RenderFormStyles() both call this:

{
    //IL_0090: Unknown result type (might be due to invalid IL or missing references)
    //IL_0097: Expected O, but got Unknown
    using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
    {
        string key = scripts ? "ExperienceFormsScriptsKey" : "ExperienceFormsStylesKey";
        if (((ControllerContext)htmlHelper.get_ViewContext()).get_HttpContext().Items.Contains(key))
        {
            Dictionary<string, IHtmlString> dictionary = ((ControllerContext)htmlHelper.get_ViewContext()).get_HttpContext().Items[key] as Dictionary<string, IHtmlString>;
            if (dictionary != null)
            {
                foreach (KeyValuePair<string, IHtmlString> item in dictionary)
                {
                    stringWriter.Write(item.Value);
                }
            }
        }
        return (IHtmlString)new MvcHtmlString(stringWriter.ToString());
    }
}

There are probably other ways too, but I can't see why this wouldn't work :)

| improve this answer | |
  • Thank you, but @jammykam's answer makes more sense and its a more clean approach. – Sebastian Tecsi Dec 16 '19 at 9:29
  • Agreed mate, not a problem :) – Dave Goosem Dec 17 '19 at 3:20

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.