Skip to content

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.

Storefront /api/revalidatenginxMagentoAdmin / importStorefront /api/revalidatenginxMagentoAdmin / importnext request gets the old pageand refreshes it in the background,the request after that is freshsave product 42PURGE / with X-Magento-Tags-Pattern: cat_p_42POST {"magentoTagsPattern": "cat_p_42"}mark cache tags for that product stale
  1. With the full page cache in Varnish mode, Magento’s Magento_CacheInvalidate sends PURGE / with an X-Magento-Tags-Pattern header to every host in its cache-host list.
  2. 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 a PURGE method.
  3. 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.
  4. A full flush (.*, or bin/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.

  1. In Magento, set Stores > Configuration > Advanced > System > Full Page Cache > Caching Application to Varnish Cache.

  2. Add the storefront nginx to Magento’s cache hosts:

    Terminal window
    bin/magento setup:config:set --http-cache-hosts=nginx:80
  3. Keep the @magento_purge location from the nginx config in magenxcommerce-docker. It only accepts PURGE from the private network.

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.