Caching and invalidation
Catalog pages are served from Next.js’s cache. When data changes in Magento, Magento’s own cache invalidation tells the storefront which pages to refresh. No custom Magento module is involved.
How it works
Section titled “How it works”- With the full page cache in Varnish mode, Magento’s
Magento_CacheInvalidatesendsPURGE /with anX-Magento-Tags-Patternheader to every host in its cache-host list. - You list the storefront’s nginx as that cache host. nginx turns the PURGE into a
POST /api/revalidate, because Next.js route handlers can’t receive aPURGEmethod. - The storefront maps Magento’s tags to its own cache tags and marks them stale.
cat_p_<id>refreshes a product and every listing it appears on.cat_c_<id>refreshes a category.cat_c_p_<id>refreshes a category’s listings when a product joins or leaves it, is enabled or disabled, or goes in or out of stock. CMS block and blog tags refresh those blocks and posts. - A full flush (
.*, orbin/magento cache:flush) marks every broad tag stale: products, categories, CMS blocks, brands, blog, store config, search and URL lookups.
Invalidation is stale-while-revalidate. The first request for a purged page still gets the old version and triggers the refresh in the background. The request after that gets fresh data. This keeps a purge from sending a burst of requests to Magento at once.
Set it up
Section titled “Set it up”-
In Magento, set Stores > Configuration > Advanced > System > Full Page Cache > Caching Application to Varnish Cache.
-
Add the storefront nginx to Magento’s cache hosts:
Terminal window bin/magento setup:config:set --http-cache-hosts=nginx:80 -
Keep the
@magento_purgelocation from the nginx config in magenxcommerce-docker. It only accepts PURGE from the private network.
On boot
Section titled “On boot”Some pages can’t be filled during next build, because the build talks to the public /api/graphql, which refuses server-only operations. With CACHE_WARM_ON_BOOT on (the default), the container refreshes those cache tags and re-renders one page per locale when it starts.