studioraz / magento2-cloudflare
Magento 2 module
Package info
github.com/studioraz/magento2-cloudflare
Type:magento2-module
pkg:composer/studioraz/magento2-cloudflare
Requires
- php: ~7.4|^8.0
- magento/framework: ^103.0
- studioraz/magento2-base: ^1.5
- studioraz/magento2-gateway: ^2.7
This package is auto-updated.
Last update: 2026-04-02 15:35:38 UTC
README
Use Cloudflare as your Magento 2 full-page cache and image optimization CDN.
Table of Contents
- Overview
- Features
- Prerequisites
- Installation
- Configuration
- How It Works
- Deploying the Cloudflare Worker
- Automatic Cache Purging
- Image Optimization
- Troubleshooting
- Contributing
- License
Overview
Magento's built-in full-page cache and Varnish setups require significant server-side infrastructure to manage, scale, and keep in sync with content changes. Image optimization typically adds another layer of complexity with local cache directories and third-party services.
This module replaces all of that with Cloudflare:
-
Full-Page Cache (FPC) -- use Cloudflare's global CDN as a drop-in replacement for Varnish or the built-in FPC. A bundled Cloudflare Worker handles caching logic at the edge, replicating Magento's Varnish behavior without requiring you to manage Varnish servers.
-
Image Optimization -- automatically rewrite image URLs to Cloudflare's
/cdn-cgi/image/endpoint. Images are resized, converted to modern formats (WebP, AVIF), and served from the edge -- no local image cache needed.
Developed and maintained by Studio Raz and released as open-source for the Magento community.
Features
Full-Page Cache
- Use Cloudflare as a selectable caching application alongside Built-in and Varnish
- Smart cache invalidation -- only affected pages are purged when content changes, not the entire cache
- Automatic cleanup of 50+ marketing/tracking query parameters (UTM, Facebook, Google Ads, etc.) to improve cache hit rates
- GraphQL cache support with proper cache key handling
- Configurable TTL, bypass paths, and debug headers
- Hit-for-pass mechanism prevents cache pollution from uncacheable pages
Image Optimization
- Automatic image URL rewriting to Cloudflare's image resizing pipeline
- Serves images in the best format the browser supports (WebP, AVIF, or original)
- Configurable quality (1--100) and fit mode per store view
- Covers product images, CMS content, logos, and widget images
- Eliminates local image cache generation, reducing disk usage and deployment size
- Debug logging available for API troubleshooting
Prerequisites
Cloudflare
- A Cloudflare account with your domain proxied through Cloudflare (orange cloud icon enabled)
- For FPC: A Cloudflare Workers subscription. The free tier allows 100,000 requests/day; the paid plan ($5/month) includes 10 million requests/month.
- For Image Optimization: Cloudflare Images is available on all plans. The free tier includes 5,000 unique transformations/month. For higher volumes, the paid plan costs $0.50 per 1,000 unique transformations.
- A Cloudflare API Token with the Zone.Cache Purge permission
Magento
| Requirement | Version |
|---|---|
| PHP | ^8.1 |
Magento 2 / magento/framework |
~103.0 |
Installation
-
Require the module via Composer:
composer require studioraz/magento2-cloudflare
-
Enable the module and run setup:
bin/magento module:enable SR_Cloudflare bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:clean
Configuration
All settings are available in the Magento Admin under:
Stores > Configuration > Studio Raz > Cloudflare
General Settings (Image Optimization)
| Field | Default | Description |
|---|---|---|
| Enabled | No | Enable/disable Cloudflare image optimization for the store view. |
| Image Quality | 85 |
JPEG/WebP/AVIF quality (1--100). |
| Image Fit | none |
How image dimensions are interpreted. See Cloudflare fit documentation. |
Image Fit options:
| Value | Description |
|---|---|
none |
No fit constraint applied (parameter omitted). |
contain |
Scale down to fit within width x height, preserving aspect ratio. |
cover |
Resize to fill width x height, cropping if necessary. |
crop |
Crop to exact width x height. |
pad |
Resize to fit within bounds and pad remaining space. |
scale-down |
Like contain, but never scales up. |
Cache (FPC) Settings
These settings are scoped to Default / Website (not Store View).
| Field | Default | Description |
|---|---|---|
| Enabled | No | Enable Cloudflare FPC cache management. |
| Zone ID | -- | Cloudflare Zone ID from the dashboard Overview page. |
| Account ID | -- | Cloudflare Account ID from the dashboard Overview page. |
| API Token | -- | Cloudflare API Token with Zone.Cache Purge permission. Stored encrypted. |
| Debug | No | When enabled, logs API requests and responses to var/log/srcloudflarecache.log. |
Important: You must also select Cloudflare as the FPC application. Go to Stores > Configuration > Advanced > System > Full Page Cache > Caching Application and select Cloudflare.
How It Works
Full-Page Cache Flow
When Cloudflare is selected as the caching application, the module adds cache tags to every page response. A Cloudflare Worker (deployed on your Cloudflare account) intercepts incoming requests before they reach your server and handles caching decisions at the edge -- just like Varnish would, but without any server-side infrastructure.
Browser ──> Cloudflare CDN ──> Worker (cache check) ──> Origin (Magento)
<── Response + Cache-Tag headers
<── Cached response (on subsequent requests)
The Worker automatically bypasses the cache for admin pages, checkout, customer account pages, and API requests. It also strips marketing query parameters (UTM, Facebook, Google Ads, etc.) so that ?utm_source=... variants don't create duplicate cache entries.
Automatic Cache Purging
When content changes in Magento -- saving a product, updating a category, flushing cache from the admin -- the module calls the Cloudflare API to purge only the affected pages by their cache tags. Unrelated cached pages remain warm.
Image Optimization Flow
When image optimization is enabled, the module rewrites image URLs to route through Cloudflare's /cdn-cgi/image/ endpoint. Cloudflare automatically resizes, compresses, and converts images to the best format the browser supports -- all at the edge, with no processing on your server.
Deploying the Cloudflare Worker
The module includes a Cloudflare Worker script (CFWorker/FPC-worker.js) that must be deployed to your Cloudflare account for the FPC feature to work.
Deployment Steps
- Log in to the Cloudflare dashboard.
- Navigate to Workers & Pages > Create Application > Create Worker.
- Paste or upload the contents of
CFWorker/FPC-worker.js. - Set up Environment Variables if needed (see below).
- Add a Route that maps your Magento store domain to this worker (e.g.,
example.com/*).
Worker Environment Variables
Set these in the Cloudflare Worker Settings > Variables panel:
| Variable | Type | Description |
|---|---|---|
DEBUG |
Boolean | Enables diagnostic X-FPC-* response headers for debugging. |
DEFAULT_TTL |
Number | Cache TTL in seconds for successful responses. Falls back to Magento's global FPC TTL. |
HFP_TTL |
Number | TTL in seconds for hit-for-pass markers. Default: 120. |
ADMIN_PATH |
String | Admin URL segment to bypass. Default: admin. |
BYPASS_PATHS |
String | Comma-separated additional paths to bypass (e.g. /api,/rest). |
The Worker strips 50+ common marketing and tracking query parameters to improve cache hit rates. See the FILTER_GET constant in CFWorker/FPC-worker.js for the complete list.
Default bypass paths: /customer, /checkout, /catalogsearch
Worker Routes
Worker Routes allow you to define URL patterns that should bypass the Cloudflare Worker entirely. By excluding specific URLs from Worker processing, you reduce the number of requests handled by the Worker, which helps lower costs and avoid unnecessary overhead for routes that don't need caching (e.g., admin panels, API endpoints, or checkout pages).
Note: The worker itself already has all the basic bypassed urls configuration, in case of this setup it will be as a fallback.
Magento home page speed
Automatic Cache Purging
The module automatically purges the Cloudflare cache when content changes in Magento. Only the affected pages are purged -- the rest of your cache stays warm.
What triggers a cache purge
- Saving a product or category
- Mass product attribute or status updates
- Applying catalog price rules
- Changing currency rates or symbols
- Saving system configuration
- Changing theme assignments
- Reindex operations
What triggers a full cache flush
- Clicking "Flush Magento Cache" or "Flush Cache Storage" in Cache Management
- Cleaning the media or catalog image cache
- Refreshing a specific cache type
Note: Even a "full" flush only purges cached pages for your site. It preserves the Cloudflare Image Transformations cache, so your optimized images remain available without re-processing.
Image Optimization
When image optimization is enabled (General > Enabled = Yes), the module rewrites image URLs from their standard Magento paths to the Cloudflare image transformations endpoint:
https://example.com/cdn-cgi/image/format=auto,metadata=none,quality=85,width=300,height=300/media/catalog/product/image.jpg
Supported image types
- Product catalog images (thumbnails, listings, gallery)
- Product media gallery URLs
- CMS page and block images
- Store logo
- Widget images
- Mirasvit catalog label badges (if Mirasvit CatalogLabel is installed)
When enabled, Magento's local image cache generation is skipped entirely. Images are served directly from Cloudflare, eliminating the /pub/media/catalog/product/cache/ directory and reducing disk usage.
Troubleshooting
Images are not being resized / URLs are not rewritten
- Ensure General > Enabled is set to Yes for the applicable scope (Store View).
- Check that your Cloudflare zone has Image Transformations enabled.
- Verify the image URL contains
/cdn-cgi/image/-- if it does not, check whether another plugin or cache is serving a stale URL.
Cache is not being purged after product/category save
- Ensure Cache > Enabled is set to Yes.
- Ensure Zone ID and API Token are correctly set and that the API Token has Zone.Cache Purge permission.
- Confirm that Cloudflare is selected as the caching application in Stores > Configuration > Advanced > System > Full Page Cache > Caching Application.
- Enable Cache > Debug and inspect
var/log/srcloudflarecache.logfor API errors.
Pages are always served from origin (no CDN HIT)
- Confirm the Cloudflare Worker is deployed and routed to your domain.
- Check that
Cache-Tagheaders are present in origin responses (visible in browser DevTools orcurl -I). - Ensure the worker's
ADMIN_PATHvariable matches your actual admin path if it is customized. - Check
X-FPC-*headers in the response (requiresDEBUG=truein worker env).
Worker debug headers missing
- Set the
DEBUGenvironment variable totruedirectly in the Cloudflare Worker settings.
Images are served in original format instead of WebP/AVIF
- Verify that your Cloudflare zone has Image Transformations enabled.
- Check the browser's
Acceptheader includesimage/webporimage/avif. - Confirm the image URL uses the
/cdn-cgi/image/format=auto,...format -- theformat=autoparameter is what enables format negotiation.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
- Fork the repository
- Create your feature branch (
git checkout -b (feature/bugfix)/my-feature) - Commit your changes
- Push to the branch (
git push origin feature/my-feature) - Open a Pull Request
Please follow the Magento 2 coding standards.
License
This project is licensed under the MIT License.