Overview
Customers may experience an issue with their DNN websites being cached on Cloudflare, which can cause outdated content to be displayed. They may want to automate the process of clearing the Cloudflare cache whenever they publish updates to a page on their website. This would require the development of a custom DNN module that can subscribe to the page published event and then use the Cloudflare API to clear the cache.
Solution
To automate the process of clearing the Cloudflare cache whenever updates are published to a page on a DNN website, follow these steps:
- Develop a custom DNN module that will manage the page publish event.
- Within your module, subscribe to the `PagePublished` event. If a specific `PagePublished` event is unavailable, consider utilizing the `TabUpdated` event.
- Upon event activation, utilize Cloudflare's API to clear the cache effectively.
Here is a C# implementation example:
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Modules;
using System.Net.Http;
using System.Text;
public class PagePublishHandler : IEventHandler
{
public void HandleEvent(object sender, EventArgs e)
{
// Your cache clearing logic
ClearCloudflareCache();
}
private void ClearCloudflareCache()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_CLOUDFLARE_API_TOKEN");
var response = client.PostAsync("https://api.cloudflare.com/client/v4/zones/YOUR_ZONE_ID/purge_cache", new StringContent("{\"purge_everything\":true}", Encoding.UTF8, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
// Cache successfully cleared
}
else
{
// Error handling
}
}
}
public class CustomModule : PortalModuleBase
{
public override void Init()
{
base.Init();
TabController.Instance.TabUpdated += new EventHandler(OnPagePublished);
}
private void OnPagePublished(object sender, TabEventArgs e)
{
// Instantiate the handler and execute the cache clearing method
var handler = new PagePublishHandler();
handler.HandleEvent(sender, e);
}
}
Replace `YOUR_CLOUDFLARE_API_TOKEN` and `YOUR_ZONE_ID` with your specific Cloudflare API token and zone ID.
-
PagePublishHandler Class:
- This class encompasses the mechanism to clear the Cloudflare cache through the Cloudflare API.
-
CustomModule Class:
- This class links to the
TabUpdated
event, which activates when a page (tab) is updated in DNN. - The
OnPagePublished
method is invoked upon event triggering, initializing thePagePublishHandler
to clear the cache.
- This class links to the
Note: This request involves pure customization, and unfortunately, we are unable to assist with any code that is not part of the core modules, as it falls outside our support scope
Summary
This article provides a solution for automating the process of clearing the Cloudflare cache whenever updates are published to a page on a DNN website. This involves developing a custom DNN module that subscribes to the page published event and uses the Cloudflare API to clear the cache.
FAQ
-
What is the `PagePublished` event in DNN?
The `PagePublished` event is a specific event in DNN that is triggered when a page is published. If this event is unavailable, the `TabUpdated` event can be used instead. -
How does the Cloudflare API clear the cache?
The Cloudflare API clears the cache by sending a POST request to the purge_cache endpoint. This request must include the Cloudflare API token and zone ID. -
What is a DNN module?
A DNN module is a component of a DNN website that provides specific functionality. In this case, the custom DNN module is used to manage the page publish event and clear the Cloudflare cache.
Comments
0 comments
Article is closed for comments.