somardigital/silverstripe-cloudfront
Composer 安装命令:
composer require somardigital/silverstripe-cloudfront
包简介
SilverStripe extension to automatically invalidate CloudFront cache when pages and files are published, unpublished, or deleted
README 文档
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 dataobjectElementalInvalidateCacheExtension- On elemental block publish clear page cacheMemberSessionCookieExtension- Sets a cookie to bypass standard cached pagesCloudfrontService- Handles AWS CloudFront invalidation requestsCloudFrontClientFactory- 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
- Clone this repository into your project
- Run
composer installin your project root - 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
-
The extension hooks into Silverstripe's ORM events:
onAfterPublishonAfterUnpublishonAfterDelete
-
When triggered, it retrieves the object's public URL:
- For
SiteTree: usesLink() - For
File: usesLink()
- For
-
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.
- The exact path:
-
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-userrequired_permission:CMS_ACCESS_CMSMain
How It Works
When CLOUDFRONT_CACHE_BYPASS_ENABLED=true:
- On Login: If the member has the required permission (default:
CMS_ACCESS_CMSMain), the configured cookie is set in their browser - 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.
统计信息
- 总下载量: 144
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-05-03