CloudFlare(CF) CDN Worker Full Page Cache(FPC) Layer for Magento 2. It has become true. Now, it is Open Source and free. The original idea was the Shopify FPC cache from the CloudFlare CDN, AWS Cloud Front Lambda Edge S3 Buket, Akamai EdgeWorkers with Object Storage etc. and it is a part of our true Magento SaaS solution(Magneto)
You can deploy the worker in several ways:
https://github.com/Genaker/CloudFlare_FPC_Worker*yoursite.com/*)See the Installation section below for detailed steps.
If you use Wrangler:
git clone https://github.com/Genaker/CloudFlare_FPC_Worker.git
cd CloudFlare_FPC_Worker
npm install
cp wrangler.toml.example wrangler.toml # if available
# Edit wrangler.toml with your KV namespace and account ID
npx wrangler deploy
You can add the worker to your CI pipeline. Example GitHub Actions step:
- name: Deploy Cloudflare Worker
run: |
npm install -g wrangler
wrangler deploy
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
Store your secrets in the repository settings and ensure your wrangler.toml has the correct KV namespace binding.
Use the cloudflare Terraform provider to deploy the worker as infrastructure-as-code:
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
variable "cloudflare_account_id" {}
variable "cloudflare_api_token" {}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
resource "cloudflare_workers_kv_namespace" "fpc_kv" {
account_id = var.cloudflare_account_id
title = "fpc-cache"
}
resource "cloudflare_worker_script" "fpc" {
account_id = var.cloudflare_account_id
name = "magento-fpc"
content = file("${path.module}/FPC.js")
kv_namespace_binding {
name = "KV"
namespace_id = cloudflare_workers_kv_namespace.fpc_kv.id
}
}
resource "cloudflare_worker_route" "fpc_route" {
zone_id = var.cloudflare_zone_id
pattern = "*yoursite.com/*"
script_name = cloudflare_worker_script.fpc.name
}
Run terraform init, terraform plan, then terraform apply. Ensure FPC.js and any required files are in the Terraform module path.
Use the cloudflare Python package to deploy programmatically:
from cloudflare import Cloudflare
api = Cloudflare(api_token="YOUR_API_TOKEN")
account_id = "YOUR_ACCOUNT_ID"
# Read worker script
with open("FPC.js", "r") as f:
script_content = f.read()
# Create or update worker
api.workers.scripts.upload(
account_id=account_id,
script_name="magento-fpc",
body={"script": script_content},
)
# Add KV binding (create namespace first via API or dashboard)
# api.workers.scripts.settings.update(...)
Install: pip install cloudflare. For full deployment including KV bindings and routes, use the Workers API endpoints for Workers Scripts, KV Namespaces, and Worker Routes.
The Edge Worker Magento full-page cache feature helps you optimize eCommerce performance by caching your Magento backend server's generated HTML or API response.
All functionality is covered with the Jest integration tests. You can test your website and you rule as well. Running the test:
export TEST_URL="https://******.com/"
npm install
npm test
TEST_URL=https://yoursite.com/ docker compose run --rm fpc-tests
Unit tests run locally without a live site. They use Miniflare to emulate the Cloudflare Workers runtime.
npm run test:unit
Or with Docker:
docker compose run --rm fpc-unit
Run next command
node generate.js
CF Edge Worker Magent Full-page cache intercepts incoming requests and checks if a cached version of the requested content is available in the CloudFlare locations or in the cache Reserve. This check for the cached version can have the following outcomes, depending on its state:
CF Worker “softpurge” the cache by changing cache Version stored in the KV(Key Value)storage. Cloud flare serve the stale content untill it will not be updayed asynchronously (in the background) fetches the new page. Cloud Front ignores any cache rules from Magento and has own logic which serve web pages from the CDN cache even if Magento 2 website is broken.
Now, you can set HTML_CACHE_VERSION via the Cloud Flare dashboard by adding the variable ENV_HTML_CACHE_VERSION. It will override the default cache version logic and can't be purged except to set a new version from the dashboard.
You can override many variables by setting the from the dashboard by adding ENV_ prefix to the var name:
Speculation is added to the worker response.
To Debug speculative rules, go to DevTools → Application → Speculative Load → Speculation:
For CF FPC Worker to consider a response from a Magento backend as cacheable, the response must meet the following criteria:
When R2 is configured (Ultra version), the worker can store and serve cached content from Cloudflare R2. This provides a persistent cache layer beyond the edge. The worker races R2 and the origin — whichever responds first is served. When the origin wins, the response is marked as server-first or r2-null-server in the R2-cache header. Both outcomes are valid; the cache is updated asynchronously.
Cache Reserve uses R2 under the hood and integrates with the worker for higher hit rates. Enable Cache Reserve in the Cloudflare dashboard to improve cache persistence.
Navigation: Cloudflare Dashboard → Workers & Pages (or go to dash.cloudflare.com and select Workers & Pages from the sidebar).
It is better to Upgrade the plan to a Bundle of 5$ per month. It is better and has no limitations.
Bundle:
Workers features Includes 10 million requests per month 3
Up to 50ms CPU time per request Always lowest latency Key-value storage features 4 10 million read operations per month 1 million write, delete, list operations per month
Path: Workers & Pages → KV → Create a namespace. (Alternatively: create the Worker first, then add a KV binding under Settings → Bindings; you can create a new namespace from there.)
Path: Workers & Pages → Create (or Create application) → Create Worker. If using Git: choose Clone and bootstrap a public repository and enter https://github.com/Genaker/CloudFlare_FPC_Worker, then Deploy.
Path: Edit Code → Insert Code from Git (or Connect to Git). Connect to
https://github.com/Genaker/CloudFlare_FPC_Worker. If you created the worker from the Git repo in the previous step, skip this.
Path: Your Worker → Settings → Variables and Secrets (for ENV vars) and Settings → Bindings (for KV).
Worker Variable name must be KV. KV name doesn't matter (Select from the drop-down)
R2 storage is not available in the open source version, only in the Ultra Version. Please contact for more details.
NOTE: OTHER_HOST is not required settings
Configure the OTHER_HOST variable with your actual domain (e.g.
www.yoursite.com) to test the worker through the worker domain. This replaces the worker domain with your staging or prod domain so responses are fetched from your Magento server. This variable is not required for production.
NOTE: OTHER_HOST is not required settings
Path: Your Worker → Settings → Triggers (or Routes). Add route pattern e.g. *yoursite.com/* or yoursite.com/* (replace with your domain).
Done! Test it using Dev Console.
You can also exclude some page rules, such as static and media, from workers. It will save money on request.
Also, Enable CF Cache Reserve to increase edge cache HIT rate. To reduce CF costs, you can exclude media and static from the cache reserve. However, cache reserve is a nice stuff, and you can benefit from storing images in it.
![image](https://github.com/user-attachments/assets/0c1bc4df-483e-
$ claude mcp add CloudFlare_FPC_Worker \
-- python -m otcore.mcp_server <graph>