|
| 1 | +# Statamic Cloudflare Cache |
| 2 | + |
| 3 | +Automatically purge your Cloudflare cache when content changes in Statamic. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Automatically purges Cloudflare cache when Statamic content changes. |
| 8 | +- Configurable events that trigger cache purging. |
| 9 | +- Optional queuing of purge jobs for background processing. |
| 10 | +- CLI command for manual cache purging. |
| 11 | +- Simple configuration. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +You can install the package via composer: |
| 16 | + |
| 17 | +```bash |
| 18 | +composer require eminos/statamic-cloudflare-cache |
| 19 | +``` |
| 20 | + |
| 21 | +Publish the config file: |
| 22 | + |
| 23 | +```bash |
| 24 | +php artisan vendor:publish --tag="cloudflare-cache-config" |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuration |
| 28 | + |
| 29 | +Add the following environment variables to your `.env` file: |
| 30 | + |
| 31 | +``` |
| 32 | +CLOUDFLARE_API_TOKEN=your-api-token |
| 33 | +CLOUDFLARE_ZONE_ID=your-zone-id |
| 34 | +CLOUDFLARE_CACHE_ENABLED=true |
| 35 | +CLOUDFLARE_CACHE_QUEUE_PURGE=false # Optional: Set to true to dispatch purges to the queue |
| 36 | +CLOUDFLARE_CACHE_DEBUG=false # Optional: Set to true to log API calls |
| 37 | +``` |
| 38 | + |
| 39 | +You can get your API token and Zone ID from the Cloudflare dashboard: |
| 40 | + |
| 41 | +1. **API Token**: Log in to your Cloudflare dashboard, go to "My Profile" > "API Tokens" and create a token with the "Zone.Cache Purge" permission. |
| 42 | + |
| 43 | +2. **Zone ID**: Go to your domain's overview page in Cloudflare. The Zone ID is displayed in the right sidebar under "API" section. |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Automatic Cache Purging |
| 48 | + |
| 49 | +Once configured, the addon will automatically purge the Cloudflare cache when content changes in Statamic. By default, it listens for the following events: |
| 50 | + |
| 51 | +- Entry saved/deleted |
| 52 | +- Term saved/deleted |
| 53 | +- Asset saved/deleted |
| 54 | + |
| 55 | +You can configure which events trigger cache purging in the config file. |
| 56 | + |
| 57 | +#### Queued Purging |
| 58 | + |
| 59 | +If you prefer to handle cache purging in the background to avoid potential delays during web requests, you can enable queued purging. Set the `CLOUDFLARE_CACHE_QUEUE_PURGE` environment variable to `true` or set `'queue_purge' => true` in the configuration file. |
| 60 | + |
| 61 | +When enabled, purge operations triggered by events will be dispatched as jobs to your application's queue. **Note:** This requires you to have a queue worker running (`php artisan queue:work`). |
| 62 | + |
| 63 | +### Manual Cache Purging |
| 64 | + |
| 65 | +You can manually purge the cache using the following command: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Purge all cache |
| 69 | +php please cloudflare:purge |
| 70 | + |
| 71 | +# Purge specific URL |
| 72 | +php please cloudflare:purge --url=https://example.com/specific-page |
| 73 | +``` |
| 74 | + |
| 75 | +## Advanced Configuration |
| 76 | + |
| 77 | +The published configuration file (`config/cloudflare-cache.php`) allows you to customize the behavior of the addon: |
| 78 | + |
| 79 | +```php |
| 80 | +return [ |
| 81 | + // Enable or disable the addon |
| 82 | + 'enabled' => env('CLOUDFLARE_CACHE_ENABLED', true), |
| 83 | + |
| 84 | + // Cloudflare API credentials |
| 85 | + 'api_token' => env('CLOUDFLARE_API_TOKEN', ''), |
| 86 | + 'zone_id' => env('CLOUDFLARE_ZONE_ID', ''), |
| 87 | + |
| 88 | + // Configure which events trigger cache purging |
| 89 | + 'purge_on' => [ |
| 90 | + 'entry_saved' => true, |
| 91 | + 'entry_deleted' => true, |
| 92 | + 'term_saved' => true, |
| 93 | + 'term_deleted' => true, |
| 94 | + 'asset_saved' => true, |
| 95 | + 'asset_deleted' => true, |
| 96 | + ], |
| 97 | + |
| 98 | + // Dispatch purge jobs to the queue instead of running synchronously |
| 99 | + 'queue_purge' => env('CLOUDFLARE_CACHE_QUEUE_PURGE', false), |
| 100 | + |
| 101 | + // Advanced settings |
| 102 | + 'purge_urls' => true, // Purge specific URLs if possible |
| 103 | + 'purge_everything_fallback' => true, // Fallback to purging everything if specific URLs can't be determined |
| 104 | + 'debug' => env('CLOUDFLARE_CACHE_DEBUG', false), // Log API call attempts when purging |
| 105 | +]; |
| 106 | +``` |
| 107 | + |
| 108 | +## Testing |
| 109 | + |
| 110 | +```bash |
| 111 | +composer test |
| 112 | +``` |
| 113 | + |
| 114 | +## License |
| 115 | + |
| 116 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments