Prezet works seamlessly with Cloudflare to provide powerful caching capabilities, allowing you to achieve static-site-like performance while maintaining the flexibility of a dynamic Laravel application.
#Why Edge-Only Caching
We rely on Cloudflare's Edge TTL instead of cache control headers to avoid client-side caching issues. When browsers cache pages, they might reference CSS or JavaScript files that no longer exist after deployment. Since there's no way to invalidate browser caches on demand, this can break your site until the cache expires.
By using Edge TTL only, you maintain total control over cache invalidation through Cloudflare's API.
#Setup
Configure Cloudflare to cache your content:
- Log into your Cloudflare dashboard and select your domain
- Navigate to "Caching" > "Cache Rules" and click "Create cache rule"
- Configure the rule:
Rule name: Cache Prezet Content
When incoming requests match:
- "All incoming requests" if your entire site is static
- "Custom filter expression" if serving dynamic content alongside Prezet
Cache eligibility: Eligible for cache
Edge TTL: Ignore cache-control header and use this TTL
- Set a longer duration (you can purge anytime)
- Click "Save" to activate the rule
You can purge the cache manually from the Cloudflare dashboard or use the built-in Prezet command.
#Cache Purging
Prezet provides a command to purge your Cloudflare cache when deploying new content:
php artisan prezet:purge
#Configuration
Add to config/services.php:
'cloudflare' => [
'token' => env('CLOUDFLARE_TOKEN'),
'zone_id' => env('CLOUDFLARE_ZONE_ID'),
],
Add to .env:
CLOUDFLARE_TOKEN=your_cloudflare_api_token
CLOUDFLARE_ZONE_ID=your_cloudflare_zone_id
The CLOUDFLARE_ZONE_ID is an MD5 hash found in the Cloudflare dashboard, not your domain name.
#Creating an API Token
- Log into your Cloudflare dashboard
- Go to "My Profile" > "API Tokens"
- Click "Create Token"
- Click "Get Started" next to Custom Token
- Configure:
- Token name: Purge cache for [your domain]
- Permissions: Zone - Cache Purge - Purge
- Zone Resources: Include - Specific zone - [Your domain]
- Complete creation and copy the token
Use this token as CLOUDFLARE_TOKEN in your .env file.
#Automated Purging
Integrate prezet:purge into your deployment process to automatically clear the cache when deploying new content.
#GitHub Actions
If using GitHub Actions, you can use jakejarvis/cloudflare-purge-action. This documentation site uses that action in its deployment pipeline - see our GitHub workflow.
#Credits
This feature was inspired by Aaron Francis's video on serving static content with Laravel: