somardigital/silverstripe-cloudfront

SilverStripe extension to automatically invalidate CloudFront cache when pages and files are published, unpublished, or deleted

Maintainers

Package info

github.com/somardigital/silverstripe-cloudfront

Type:silverstripe-vendormodule

pkg:composer/somardigital/silverstripe-cloudfront

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

v2.0.2 2026-05-03 22:30 UTC

This package is auto-updated.

Last update: 2026-05-03 22:30:36 UTC


README

Invalidate CloudFront cache when pages and files are published, unpublished, or deleted in Silverstripe CMS.

Problem Statement

When editors publish, unpublish, or delete pages and files, the public-facing site does not update because CloudFront is still serving the old, cached version. This requires manual cache clearing, which is inefficient and error-prone.

Solution

This module provides:

  • InvalidateCacheExtension - Hooks into Silverstripe ORM events to invalidate matching dataobject
  • ElementalInvalidateCacheExtension - On elemental block publish clear page cache
  • MemberSessionCookieExtension - Sets a cookie to bypass standard cached pages
  • CloudfrontService - Handles AWS CloudFront invalidation requests
  • CloudFrontClientFactory - Creates and configures the AWS CloudFront client

When a page or file is published, unpublished, or deleted, the extension automatically sends invalidation requests to CloudFront. All operations are logged using PSR-3 LoggerInterface for proper observability.

Requirements

  • Silverstripe Framework ^6
  • Silverstripe CMS ^6
  • AWS SDK for PHP ^3.379 (installed automatically via Composer)
  • AWS CloudFront distribution with valid credentials

Installation

Via Composer (recommended)

composer require "somardigital/silverstripe-cloudfront"

Or for local development add this to composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "../silverstripe-cloudfront"
        }
    ]
}

Then run:

composer require "somardigital/silverstripe-cloudfront:@dev"

Manual Installation

  1. Clone this repository into your project
  2. Run composer install in your project root
  3. Run vendor/bin/sake dev/build flush=1

Usage

The extension is automatically applied to SiteTree and File classes via YAML configuration. No additional setup is required.

When an editor:

  • Publishes a page or file → CloudFront cache is invalidated
  • Unpublishes a page or file → CloudFront cache is invalidated
  • Deletes a page or file → CloudFront cache is invalidated

Configuration

AWS Credentials

Add the following to your .env file:

# AWS CloudFront Configuration
AWS_ACCESS_KEY_ID=your_access_key_here
AWS_SECRET_ACCESS_KEY=your_secret_key_here
CLOUDFRONT_DISTRIBUTION_ID=your_distribution_id_here

How It Works

  1. The extension hooks into Silverstripe's ORM events:

    • onAfterPublish
    • onAfterUnpublish
    • onAfterDelete
  2. When triggered, it retrieves the object's public URL:

    • For SiteTree: uses Link()
    • For File: uses Link()
  3. Sends two invalidation paths to CloudFront:

    • The exact path: /about-us
    • Wildcard path with query params: /about-us?*

    This ensures both the base page and all variations with query parameters are invalidated.

  4. If invalidation fails, it logs a warning but doesn't break the publish/delete process

CMS User Cache Bypass (Optional)

This module includes an optional feature to help CMS users bypass CloudFront cache by setting a cookie when they log in. This allows logged-in editors and admins to see the latest content immediately without waiting for cache invalidation.

Note: Setting the cookie alone does not bypass the cache. You must also configure a CloudFront Behavior to bypass caching when this cookie is present. See AWS CloudFront documentation for details on cookie-based cache behaviors.

Enabling the Feature

Add to your .env file:

CLOUDFRONT_CACHE_BYPASS_ENABLED=true

Customizing the Configuration

You can customize the cookie name and required permission in your project's YAML configuration:

Somar\CloudFront\Extensions\MemberSessionCookieExtension:
  cookie_name: 'my-custom-cookie'
  required_permission: 'ADMIN'

Default values:

  • cookie_name: ss-cms-user
  • required_permission: CMS_ACCESS_CMSMain

How It Works

When CLOUDFRONT_CACHE_BYPASS_ENABLED=true:

  1. On Login: If the member has the required permission (default: CMS_ACCESS_CMSMain), the configured cookie is set in their browser
  2. On Logout: The cookie is cleared

This cookie can then be used in CloudFront to create a cache behavior that bypasses the cache for users with this cookie present.