November Happy Hour will be moved to Thursday December 5th.

Cache busting in CMS 12

Vote:
 

I'm trying to figure out the best way to configure our caching strategy. Media assets, mainly PDFs, are uploaded into CMS, are updated frequently, and keep the same file name. Cloudflare seems to serve cached versions of these media assets for a long period of time even after the PDF has been updated in the CMS. It seems like the max-age value on the cache-control header varies from PDF to PDF. Is there a best practice to follow to ensure that CDN cache is evicted when a media asset is updated in the CMS?

#333143
Nov 19, 2024 3:11
Vote:
 

Few key steps to consider:

  1. Turning off or reducing caching duration:

    • Reducing the cache duration ensures that updates propagate faster to end-users.
    • A cache duration of 30–60 minutes strikes a balance between performance and freshness. Turning caching off entirely is another option for near-instant invalidation but can strain server resources.
  2. ETag or Last-Modified Headers:

    • These headers allow conditional requests. Cloudflare can revalidate with the origin server to check if the asset has changed.
    • If the asset hasn’t changed, the server responds with 304 Not Modified, avoiding unnecessary data transfer while ensuring users see the latest version.

Best Practice:

If invalidating assets immediately is critical:

  • Use Cache Purge: Cloudflare offers features to purge specific files (or the entire cache) programmatically via its API. This ensures updated assets are immediately served.
  • Set Cache-Control headers strategically:
    • Use short max-age values when assets change frequently.
    • For less dynamic content, use longer caching durations.

Optimizley Consideration:

Ensure Episerver is set up to generate accurate ETag and Last-Modified headers. This way, Cloudflare revalidation remains efficient and reliable.

#333146
Edited, Nov 19, 2024 4:25
Mike Malloy - Nov 20, 2024 15:05
I was always told the only thing we can do is a full purge in the PaasPortal. Did you figure a way to call the API with the DXP?
Sujit Senapati - Nov 20, 2024 21:08
No Mike! Optimizely DXP program does not give us the option to do it ourselves. But if we request them with URLs or tags we want them to clear, the Service Desk will do it.
LarryVictorio - Nov 20, 2024 21:50
Hi Sujit,
Can you point me in the right direction to use ETags on a CMS12 solution?
In regards to setting cache-control headers strategically, is it possible to target specific URI patterns? media types? Can I target the CDN cache specifically? Or do these headers apply to everything in front of the origin server, including the client browser cache?
Can you provide examples on how to ensure Episerver is set up to generate accurate ETag and Last modified headers. Is this something you might handle in a controller action, PageContextActionFilter, in appsettings.json? If so, what section?
Sujit Senapati - Nov 20, 2024 23:17
In previous version it was plain and simple to add to web.config under staticFiles and create profiles for each file types differently with different cache expiration. For dotnet core, you can make use of UseStaticFiles with StaticFileOptions and FileProvider to build the same configuration. You can see here
https://andrewlock.net/adding-cache-control-headers-to-static-files-in-asp-net-core/
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files

Although ETag is also explained how to use in the above URL, you can also look following link with more complex approach.
https://stackoverflow.com/questions/35458737/implement-http-cache-etag-in-asp-net-core-web-api
You can create an implementation of ActionFilter to modify the header and ETag for your purpose.

By default, the ETags are generated for all your static files unless until you disable them. You can see them in the browser under Response Headers for static files.
Vote:
 

Add this project to your Solution, it gives every file version a unique URL.

https://github.com/episerver/EPiServer.CdnSupport/

#333150
Nov 19, 2024 16:23
Sujit Senapati - Nov 19, 2024 16:31
Thanks for sharing @Johan. I didn’t know about the above package. Definitely an unique take on solving this problem. Would you mind sharing your package in the link below as some people are still looking for a solution it seems. It might help them. https://feedback.optimizely.com/ideas/DXCS-I-254
Johan Kronberg - Nov 19, 2024 16:35
It's not my package but I added a comment in the feedback item as well. The repo has been around since before the DXP was available and works well with CMS 12.
LarryVictorio - Nov 20, 2024 21:38
If an external site or an email for example had a link to one of these assets, would the link only be valid at the point in time that asset served as the latest copy? In other words, if you uploaded mediaasset.pdf, from what I understand the addon gives a unique URL of mediaassetGUID123.pdf. You then provide this unique URL to be used on some marketing collateral, but later modify the PDF and reupload into CMS. Does the addon change it to mediaassetGUID456.pdf rendering the link in the media collateral broken or stale?
Sujit Senapati - Nov 20, 2024 21:43
I have not tried, but as per the documentation it should automatically redirect to new URL. "Requests where the injected key is outdated will be permanently redirected to an updated key for SEO reasons and to make sure bookmarks continue to work."
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.