3

In our outerlayout.cshtml file we have the following code:

@Html.RenderFormScripts()

which seems to loop round the JS files, that have been attached to the Sitecore form:

enter image description here

and create script tags for each of them in the finally outputted HTML.

Does anyone know of a way where we can add a cache buster to this functionality so that we don't have to remember to manually update the attached JS file references every time one of the source JS files changes ?

We have other areas, of the site, where JS files have cache busting on them by having a buildNumber parameter, which is incremented every time the source code is deployed to PROD, and then injecting this parameter into the script tag but it seems like we need to customise what the RenderFormScripts function does

4
  • Please include the version of your Sitecore instance to provide further context
    – Julius A
    Commented Aug 30, 2022 at 16:35
  • You could add a redirect on a specific file from your-file.js to your-file.js?v=formatteddatetime. You would probably need to add it in code to grab the datetime, you could then control the frequency of the cache busting depending on the amount of detail you put in string (i.e. minute, hour, day, month etc) Commented Aug 30, 2022 at 18:36
  • 1
    @JuliusA, sorry forgot that bit. It's Sitecore 9.2
    – Adam Brunt
    Commented Aug 31, 2022 at 6:36
  • @DeanOBrien we already have a way of adding the cache buster (based on build number) and we use it in other areas of the code. I was hoping there was a way we could combine it with the RenderFormScripts function (which seems to be clever enough to only include the JS files IF there is a form on the page) but it looks like we will just have to try and replace that call with custom code
    – Adam Brunt
    Commented Aug 31, 2022 at 6:41

1 Answer 1

2

You can override IncludeFiles processor of forms.renderForm pipeline and add custom parameter for all the scripts (or styles). Class below adds DateTime.Now.Ticks to scripts only. You can use your own logic there to include desired cache buster:

public class IncludeFiles : Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.IncludeFiles
{
    protected override void IncludePath(HttpContextBase httpContext, string filePath, bool script)
    {
        var filePathWithCacheInfo = script ? $"{filePath}?{DateTime.Now.Ticks}" : filePath;
        base.IncludePath(httpContext, filePathWithCacheInfo, script);
    }
}

Remember to register your class with patch file:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <forms.renderForm>
                <processor type="Sitecore.ExperienceForms.Mvc.Pipelines.RenderForm.IncludeFiles, Sitecore.ExperienceForms.Mvc">
                    <patch:attribute name="type">MyAssembly.MyNamespace.IncludeFiles, MyAssembly</patch:attribute>
                </processor>
            </forms.renderForm>
        </pipelines>
    </sitecore>
</configuration>
1
  • Thanks, that is awesome. Whilst I knew custom processors existed I had no idea where to even look for the processors that handle form scripts
    – Adam Brunt
    Commented Aug 31, 2022 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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