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)
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
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:
- If a cached version is found and it's not stale, then the cached content is served to the user. No request is made to the Magento Server.
- If a cached version is found and it is stale, then the cached content is served to the user. The CF worker is executed in the background and requests a new version of the page caches from the Magento backend server for future requests. CF FPC is revalidated from the server asynchronously after 5 minutes or so, but you can change the time and logic.
- If a cached version isn't found, the CF FPC worker sends a request to the Magento server to be used for future requests.
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:
- &cfw=false - disable Cloud worker, bypass all worker logic
- &cf-cdn=false - bypass Cloud Flare EDGE CDN cache, but R2 cache still works
- &r2-cdn=false - bypass R2 cache
- &cf-revalidate=true - revalidate URL in the cache
- &cf-delete=true - delete from the edge cache but not from R2
- &cf-purge - purge entire cache by changing cache version. called ONCE(1) - soft purge. TWICE(2) - hard purge.
Speculation is added to the worker response.
To Debug speculative rules, go to Dev Tol Bar -> 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:
- Be a response to a GET request
- Have a 2XX or 3XX status code.
- Have a public Cache-Control header set with a non-zero max-age, or s-maxage, value
- Url doesn't match the worker's blacklist
- URL Query String Filtering and sorting
- Traffic filtering and control
- Cache logic adjustment. You don't need any VCL now you can do everething in pure JavaScript
- Content manipulation
- Speculation rules prerender and prefetch
- Link header resource preload
- Custom CORS
- ESI (Edge Server Include - AJAX requests) blocks with auth and HTTPS support. Easy to use with microservices.
- GOD MOD - cache can't be invalidated. It can be but not easy.
- PWA installable APP
- The full-page cache is designed to work with the default magento cache, which is PHP Built-in FPC, FAST FPC (See repo: https://github.com/Genaker/FastFPC), or Varnish. You can try to use it as a main cache (see Cache Reserve), but it is not what it was designed for. *The main idea of the CF Worker FPC Cache is Magento 2 pages are always served from the CF cache with async revalidation.
- You can't clear the cache by page. You can clear the entire cache only. That is why you need a default magento cache. However, it is designed to work without any cache clears. The worker will update it asynchronously. You mark the entire cache stale by changing its version. To hard clear the cache, you need to change the version twice. CF Worker checks the previous cache version to see if it is a stale cache.
Open Cloud Flare and Go to Workers
It is better to Upgrade the plan to a Bundle of 5$ per month. It is better and has no limitations.
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
Workers & Pages -> KV -> Create a namespace
Workers & Pages -> Overview -> Create -> Create Worker -> Deploy
Edit Code -> Insert Code From the Git
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 (for example, google.com) variable to test workers through the worker domain. This will replace the worker domain with your staging or prod domain, and the server response will be fetched from there. This variable is not required.
NOTE: OTHER_HOST is not required settings
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.
Disable Cloud Flare Chache for Static and Media save and serve from the Cache Reserve <be > Caching -> Cache Rules
Add Rule
(http.request.full_uri wildcard "*.site.com/static/*") or (http.request.full_uri wildcard "*.site.com/media/*"
Replace site with your <br>
Please update this documentation when you will do it yourself. It is just a fast-written manual. For detailed information, check the Worker code.
If you have any issues, create an issue or email me: egorshitikov[A]gmail.com
We also developed a Magento Extension designed to enhance communication and performance between CF Workers and the cache system. <br> While this extension is not yet publicly available, feel free to contact me directly if you’re interested in receiving it.
You can add any cookies you want just by changing the script.
Cache Reserve is a large, persistent data store implemented on top of CF R2. Your website’s cacheable FPC content will be written to Cache Reserve. Cache Reserve serves as the ultimate upper-tier cache, reserving storage space for your FPC for as long as you want. This ensures that your FPC is served from the cache. Cache Reserve is a CF feature that allows the use of Claud Flare as a main cache solution.
Like the standard CDN, Cache Reserve also uses the cf-cache-status header to indicate cache statuses like MISS, HIT, and REVALIDATED. Cache Reserve cache misses and hits are factored into the dashboard’s cache hit ratio.
Individual sampled requests that were filled or were served by Cache Reserve are viewable via the CacheReserveUsed Logpush field.
Cache Reserve monthly operations and storage usage are viewable in the dashboard.
With the CF FPC Workers, you can achieve 91%+ page cache rate from CDN