0

Is it possible to set the cache for JS and CSS files only based on some logic?

When the url of the assets always changes (e.g SignUp.[versiontoken].css when the content is modified) then the cache settings can be (1 year) Cache-Control: max-age=31536000

When the url is static (e.g /ui/some.css) then the cache should be short (<1day).

For the first scenario, I think is enough to add Cache-control in the web.config, but for the second one, what should I do?

<httpProtocol>
    <customHeaders>
        <remove name="Cache-Control" />
        <add name="Cache-Control" value="public, max-age=31536000" />
    </customHeaders>
</httpProtocol>

Project: Sitecore 9.3 based on Helix architecture, no SXA

1
  • Can you give us more information? Like what Sitecore version are you using? How have you setup your JS/CSS? Is this SXA/JSS/Classic MVC? Based on your tags, if this is just static files on your server, then this is not the right place, you'd be better asking on Stack Overflow.
    – Richard Seal
    Commented Feb 23, 2022 at 13:58

1 Answer 1

2

Based on your question it seems like these are static assets on your server which are perhaps generated by a frontend build process. I'm having a similar requirement and use an outbound rewrite rule to accomplish this, see sample below. Another approach would be to set this in Application_BeginRequest.

<outboundRules>
    <rule name="Set long cache time" preCondition="SetLongCache">
        <match serverVariable="RESPONSE_cache-control" pattern="(.*)" />
        <action type="Rewrite" value="max-age=31536000" />
    </rule> 
    <preConditions>     
        <!-- Change condition to meet your requirement -->
        <preCondition name="SetLongCache" >
            <add input="{HTTP_URL}" pattern="SignUp\.(.*)\.css" />
        </preCondition>
    </preConditions>
</outboundRules>
1
  • This is actually good. Is not quite what I need, but it helped a lot. Thanks
    – iusmar
    Commented Feb 25, 2022 at 16:35

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.