CDN Cache Clear Using SPE


These days, performance and response times for web are more important than ever. It's very easy to lose a visitor when your page takes too long to load. One of the ways to improve performance of your site is to leverage a CDN for delivering static assets (CSS, JS, images, etc.). CDNs are great because they keep load off of your servers, and they often come with some built in optimization that can be leveraged to further improve your site's performance. However, many CDNs are a black box when it comes to knowing what is in the cache and what isn't. Sometimes you need to bust something out of the cache to ensure you are getting the latest version of an asset. In my case, we were leveraging Sucuri and I needed to bust some poorly optimized (fuzzy) images. Enter Sitecore Powershell Extensions, and an API call.

SPE has lots of neat tooling that allows you to add handy scripts to specific items in the Ribbon or in the Context Menu. In my case, I wanted to be able to select a single image out of the Media Library, and trigger a call to bust it out of the cache. Let's start with the actual PowerShell script we need to accomplish this:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$imageTemplate = "{F1828A2C-7E5D-4BBD-98CA-320474871548}"
 
$wafKey = "{key-goes-here}"
 
$wafSecrets = @(
    @{ domain = "{not-needed}"; secret = "{secrets-here}" },
    @{ domain = "{but-I-added}"; secret = "{secrets-here}"},
    @{ domain = "{for-verbosity}"; secret = "{secrets-here}" },
)
 
$item = Get-Item .
 
$hasTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($item).InheritsFrom($imageTemplate);
 
if (!$hasTemplate) { exit }
 
$imageUrl = [Sitecore.Resources.Media.MediaManager]::GetMediaUrl($item)
$imageUrl = $imageUrl.Replace("/sitecore/shell", "")
 
foreach ($secret in $wafSecrets) {
    $pathInCache = "$($imageUrl)"
     
    Write-Host "Cache Path: $pathInCache"
     
    $parameters = @{
        k = $wafKey;
        s = $($secret.secret);
        a = "clear_cache";
        file = $pathInCache;
    }
     
    Invoke-WebRequest -UseBasicParsing -Method GET -Uri $wafEndpoint -Body $parameters
}

The above script is pretty straight forward. We're assuming that we are going to be executing from a context menu, so `Get-Item .` is getting our context item. We verify that the item in question is an image item; if it isn't then we just get out. Note, we are going to add some rules to the context menu script that will prevent it from showing up for non-images, so this is really just a safety check. Once we know we have a good item, we leverage the Media Manager to get the URL of the item. Because we are executing in the context of the CM, we're going to get '/sitecore/shell' in the path. We don't need that, so we can just take it off. Lastly, we build and execute a web request call to the Sucuri api.

The api expects 4 parameters:

  • k - this is the key for the WAF. If you have multiple sites going through the same WAF, this will likely be the same for all of them
  • s - this is the secret, that is specific to the domain you are trying to clear
  • a - this is the action you are performing
  • file - this is the file you want to clear - if left blank, the entire cache for the domain will be flushed

Sucuri caches based on the relative path to the item (e.g. /-/media/path/to/image/image-name.extension).

 Now to add this script to the context menu for images. A quick Google search and a three-year-old blog post from @mike_i_reynolds make this a pretty easy task. Create your module, and put your script in the path '/Content Editor/Context Menu/{Script goes here}'. A show rule is leveraged to ensure the script only appears for images.

Cache Clear Script Image

 If you're like me, and don't remember the path to the Image template off of the top of your head: /sitecore/templates/System/Media/Unversioned/Image.

It's as simple as that. Now we have an option to clear the cache when we right click on a media item:

Context Menu

More importantly, the image is appropriately busted when we choose to clear it out:

CacheHitMiss

Just another example of how we can leverage SPE to make our everyday Sitecore lives easier. Happy scripting!

Categories: SPE, Sitecore, CDN
Tags: CDN, Sitecore, Sitecore;

SEARCH ARTICLES

CATEGORIES

Sitecore 144
Commerce 100
Web Development 100
Sitecore Commerce 83
Sitecore Experience Commerce 9 64
Sitecore Experience Commerce 59
Content Management 55
eCommerce 48
B2B eCommerce 42
Sitecore Experience Platform 39
Sitecore Platinum Partner 39
Architecture 34
Insite 29
User Experience 26
Strategy 22
B2C eCommerce 21
B2B Commerce Blogs 21
commerceconnect 21
CloudCraze 20
SaaS (Software as a Service) 20
Cloud 17
Commerce Server 17
Salesforce B2B Commerce Cloud 16
Mobile 13
Search 13
Plugins 12
Analytics 12
Application Development 12
Digital Transformation 11
Sitecore Symposium 11
SPEAK 10
Helix 10
DMS 8
Social 8
Business Process 7
Authentication 7
5 Reasons 7
BI and Big Data 6
Data Visualization 6
Sitecore Presentation 6
Coveo 6
NET Development 5
Microsoft Azure 5
Automation 5
Front-End Development 4
SaaS (Software as a Service) 4
Digital Strategy 4
Brightcove 4
Press Release 4
Avalara Tax 3
Sitecore Experience Accelerator (SXA) 3
Sitecore Layouts 3
Video 3
SPE 3
Multi-site 3
Multi-lingual 3
Accessibility 3
Habitat 3
Vault 3
Identity 2
Managed Services 2
CDN 2
SMB 2
Cryptocurrency 2
Sitecore Forms 2
Sitecore Experience Commerce 9 Promotions 2
Uncategorized 2
EXM 2
Conversational Commerce 2
Sitecore SaaS 2
Security 2
Unit Testing 2
Headless Architecture 2
Sitecore Experience Awards 2
Google 1
Content Delivery Network 1
Configure Price Quote 1
CPQ 1
Blockchain 1
Coupons 1
Sitecore Rss 1
Artificial Intelligence 1
Machine Learning 1
Okta 1
RFP Process 1
NoSQL 1
Flex Accelerator for Sitecore 1
Reviews 1
SEO 1
Page Labels 1