Cogworks.CogFlare 1.3.2

Prefix Reserved
dotnet add package Cogworks.CogFlare --version 1.3.2                
NuGet\Install-Package Cogworks.CogFlare -Version 1.3.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cogworks.CogFlare" Version="1.3.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cogworks.CogFlare --version 1.3.2                
#r "nuget: Cogworks.CogFlare, 1.3.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cogworks.CogFlare as a Cake Addin
#addin nuget:?package=Cogworks.CogFlare&version=1.3.2

// Install Cogworks.CogFlare as a Cake Tool
#tool nuget:?package=Cogworks.CogFlare&version=1.3.2                

CogFlare

A package that helps automatically purge CloudFlare cache with Umbraco 12-13

Built With Built With Cloudflare Open Source License GitHub Issues Status Contributions

Why is CogFlare Needed?

Most CMS platforms, including Umbraco, are designed to be dynamic and, by default, don’t cache full HTML pages. While they handle caching for assets like JavaScript, CSS, and images, HTML is often left uncached because dynamic pages frequently change. This ensures fresh content but comes with a cost: every request hits the server, even for unchanged pages, leading to unnecessary server load and slower response times.

CogFlare solves this problem by integrating Umbraco with Cloudflare, allowing you to leverage full-page HTML caching through their CDN. While Cloudflare can efficiently cache and serve HTML, the challenge lies in knowing when to purge cached content. Without proper purging, outdated or incorrect content may be served to users.

This is where CogFlare steps in. The package automatically monitors changes in your CMS:

  • When nodes or content are updated, CogFlare identifies the affected pages and sends targeted purge requests to Cloudflare.
  • Instead of purging the entire site, only the relevant pages are cleared, ensuring efficient caching and fresh content delivery.

By automating the caching and purging process, CogFlare provides the performance benefits of full-page caching without the complexities of managing it manually.

Usage

Basic Functionality

  • Automatically purges Cloudflare cache when:
    • Content nodes are published, unpublished, or deleted.
    • Media items are saved.
  • Ability to toggle the package functionality on/off in the settings.
  • Ability to toggle logging on/off in the settings.

Advanced Functionality

  • Configure Key Nodes in the settings:
    • A Key Node is any content node that triggers a FULL site cache purge when it or its referenced nodes are changed (e.g., Site Settings, Navigation, Footers).
  • Blocklist blocks that you don’t want to cache by specifying their aliases, with the ability to automatically make form pages uncachable.

Backoffice Dashboard

  • A dashboard has been added to the Settings section of the backoffice.
    • Currently, only Admins can access it.
  • Features include:
    • A button to manually trigger a FULL site cache purge.
    • Viewing the current configuration for the package.

Logs

  • Logs are created whenever:

    • A node eligible for caching is changed.
    • A purge request to Cloudflare is made.
  • Additional logs display the result of the purge request to Cloudflare:

Installation

Install through dotnet CLI:

dotnet add package Cogworks.CogFlare

Or the NuGet Package Manager:

Install-Package Cogworks.CogFlare

Add these settings to the appsettings.json

  "CogFlareSettings": {
    "IsEnabled": true,
    "ApiKey": "xxx",
    "Email": "xxx@xxx.com",
    "Endpoint": "https://api.cloudflare.com/client/v4/zones/[zoneId]/purge_cache",
    "Domain": "https://www.example.com",
    "EnableLogging": true, //optional
    "KeyNodes": "1234, 031089", // optional
    "BlockAliases": "formBlock, otherFormBlock", // optional
    "CacheTime": "2592000" // optional => will default to 1 month
  }

To add cache headers to your pages please add the view component in your Master or required page View

@await Component.InvokeAsync(ApplicationConstants.CogFlareCacheHeaders)

Ensure you include the correct using directive at the top of your file:

@using Cogworks.CogFlare.Core.Constants

By default the cache time will be set to 1 month. This can be overriden in the CogFlare Settings

Umbraco Forms and Anti-Forgery Tokens with Full Page HTML Caching

Umbraco Forms include anti-forgery tokens by default. These tokens must be unique for each page to ensure forms function correctly.

However, when implementing full-page HTML caching, this can cause issues: cached pages will reuse the same anti-forgery token, breaking the forms on those pages.

This package provides a solution to ensure both caching and forms can coexist seamlessly.

Problem with Caching and Forms

If you use full-page HTML caching for a page containing an Umbraco form:

  • The anti-forgery token will be cached along with the page's HTML.
  • When users access the page, the token will no longer be unique, causing form submissions to fail.

Workarounds

To resolve this issue, the package offers two options:

Option 1: Disable Anti-Forgery Tokens

You can disable anti-forgery tokens for the affected pages:

  1. This allows the page to remain cached while keeping the form functional.
  2. Caution: Disabling anti-forgery tokens may reduce the security of form submissions.

Option 2: Use Blocklist Aliases to Disable Caching for Specific Pages

This package includes a feature to conditionally disable caching for pages containing specific blocks, such as forms, to avoid the anti-forgery token issue.

  1. How It Works:

    • In the CogFlareSettings in the appsettings, you can provide a blocklist alias (e.g., a block alias for the Umbraco form or any other block you don’t want to cache).
    • When rendering the page, the package checks for the presence of any of the specified block aliases.
    • If the page includes a block with one of these aliases, private, no-cache, must-revalidate will be set for the page, effectively disabling caching for that page.
  2. Configuration: Add your BlockAliases to the CogFlareSettings:

  "CogFlareSettings": {
    ...
    ...
    "BlockAliases": "formBlock, otherFormBlock"
  }

Backoffice User:

Email: admin@admin.com
Password: 0123456789

License

Licensed under the MIT License

© 2024 Cogworks

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.2 95 12/9/2024
1.3.1 89 12/5/2024
1.3.0 165 10/23/2024
1.2.12 443 6/13/2024
1.2.11 361 2/3/2024
1.2.10 124 1/25/2024
1.2.9 395 1/24/2024
1.2.8 116 1/24/2024
1.2.5 114 1/23/2024
1.2.3 119 1/19/2024
1.2.2 113 1/19/2024
1.1.0 123 1/16/2024
1.0.1 123 1/15/2024
1.0.0 128 1/15/2024