Skip to content

Instantly share code, notes, and snippets.

@klpatil
Created July 19, 2021 03:54
Show Gist options
  • Save klpatil/faec0704ebf75c03c7fda034eb251d3e to your computer and use it in GitHub Desktop.
Save klpatil/faec0704ebf75c03c7fda034eb251d3e to your computer and use it in GitHub Desktop.
SXA Media Handler PDF Caching Override
using Sitecore.Diagnostics;
using Sitecore.XA.Foundation.MediaRequestHandler.Pipelines.MediaRequestHeaders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SCBasics.Foundation.Caching.Pipelines
{
public class MediaRequestHeaderProcessor
{
public void Process(MediaRequestHeadersArgs args)
{
try
{
Assert.ArgumentNotNull(args, nameof(args));
var media = args.Media;
var cache = args.Context.Response.Cache;
int maxAgeInSeconds =
Sitecore.Configuration.Settings.GetIntSetting("MediaRequest.CachingOverride.MaxAgeInSeconds", 0);
string mimeTypeToApplyCache =
Sitecore.Configuration.Settings.GetSetting("MediaRequest.CachingOverride.MimeTypeToApplyCache", "application");
string httpCacheability =
Sitecore.Configuration.Settings.GetSetting("MediaRequest.CachingOverride.HttpCacheability", "NoCache");
string cacheExtension =
Sitecore.Configuration.Settings.GetSetting("MediaRequest.CachingOverride.CacheExtension", "no-cache");
if (media.MimeType.StartsWith(mimeTypeToApplyCache
, StringComparison.OrdinalIgnoreCase))
{
cache.SetMaxAge(TimeSpan.FromSeconds(maxAgeInSeconds));
if (!string.IsNullOrWhiteSpace(httpCacheability))
{
// Please make sure there is no typo and value should be from above list of possibe values
// This will be mainly owned by Developers - So, if Dev makes any typo - This will throw error - Which is intentional
var httpCacheabilityValue = (HttpCacheability)
Enum.Parse(typeof(System.Web.HttpCacheability), httpCacheability, true);
if (httpCacheability != null)
cache.SetCacheability(httpCacheabilityValue);
}
if (!string.IsNullOrWhiteSpace(cacheExtension))
cache.AppendCacheExtension(cacheExtension);
}
}
catch (Exception exception)
{
// Log it and swallow it
Log.Error("Something went wrong while setting cache-control headers for media file", exception, this);
}
}
}
}
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore>
<!-- Thanks! https://ggullentops.blogspot.com/2021/01/sitecore-media-cache-headers-pdf.html-->
<!--NOTE : This pipeline is to add additional cache-control headers to disable caching for specific mimetype -->
<settings>
<!--MimeType To Apply Cache
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
We are doint it for application - Which covers most of the documents-->
<setting name="MediaRequest.CachingOverride.MimeTypeToApplyCache" value="application"/>
<!--//
// Summary:
// Sets the Cache-Control: no-cache header. Without a field name, the directive
// applies to the entire request and a shared (proxy server) cache must force a
// successful revalidation with the origin Web server before satisfying the request.
// With a field name, the directive applies only to the named field; the rest of
// the response may be supplied from a shared cache.
NoCache = 1,
//
// Summary:
// Default value. Sets Cache-Control: private to specify that the response is cacheable
// only on the client and not by shared (proxy server) caches.
Private = 2,
//
// Summary:
// Specifies that the response is cached only at the origin server. Similar to the
// System.Web.HttpCacheability.NoCache option. Clients receive a Cache-Control:
// no-cache directive but the document is cached on the origin server. Equivalent
// to System.Web.HttpCacheability.ServerAndNoCache.
Server = 3,
//
// Summary:
// Applies the settings of both System.Web.HttpCacheability.Server and System.Web.HttpCacheability.NoCache
// to indicate that the content is cached at the server but all others are explicitly
// denied the ability to cache the response.
ServerAndNoCache = 3,
//
// Summary:
// Sets Cache-Control: public to specify that the response is cacheable by clients
// and shared (proxy) caches.
Public = 4,
//
// Summary:
// Indicates that the response is cached at the server and at the client but nowhere
// else. Proxy servers are not allowed to cache the response.
ServerAndPrivate = 5
NOTE : Please make sure there is no typo and value should be from above list of possibe values-->
<setting name="MediaRequest.CachingOverride.HttpCacheability" value="NoCache"/>
<!--Appends the specified text to the Cache-Control HTTP header.-->
<setting name="MediaRequest.CachingOverride.CacheExtension" value=""/>
<!--Max Age In Seconds-->
<setting name="MediaRequest.CachingOverride.MaxAgeInSeconds" value="0"/>
</settings>
<!-- Added to Disable PDF Browser Caching -->
<pipelines>
<mediaRequestHeaders>
<processor type="SCBasics.Foundation.Caching.Pipelines.MediaRequestHeaderProcessor, SCBasics.Foundation.Caching" />
</mediaRequestHeaders>
</pipelines>
</sitecore>
</configuration>
<handlers>
<add xdt:Transform="Replace" xdt:Locator="Match(name)" name="Sitecore.MediaRequestHandler" verb="*" path="sitecore_media.ashx" type="Sitecore.XA.Foundation.MediaRequestHandler.MediaRequestHandler, Sitecore.XA.Foundation.MediaRequestHandler" />
</handlers>
<!--Optional-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment