I need to specify a ttl for a custom product block to force varnish cache invalidation on it without loosing FPC. Is there a way to get product (at least the productId) from the ESI request so that I can dynamically rebuild my block according to product attributes ?

Here is my catalog_view_view.xml (working in my theme not a specific module):

<referenceContainer name="product.info.main">
    <block class="Magento\Catalog\Block\Product\View\Attributes" name="mytemplate" template="Magento_Catalog::mytemplate.phtml" ttl="100" after="product.info" /> 

In my template.phtml the product is always null when the ESI request is made:

<?php $p = $block->getProduct(); ?>

the request URI looks like :


  • Is the handles parameter on this page_cache/block/esi url hashed or something? I thought that was normally a JSON encoded array, like handles/%5B%22default%22,%22catalog_product_view%22%5D/?
    – thaddeusmt
    Nov 6 '17 at 3:56

I don't think you'll be able to do that with Magento's default ESI TTL functionality. You can see in Magento\PageCache\Observer\ProcessLayoutRenderElement in the _wrapEsi() method that only layout blocks and handles are passed to the page_cache/block/esi URL as parameters. The ESI request is not going to get any of the parent page GET context like product ID, etc.

To get this custom product block to cache with a separate TTL than the rest of the Product Detail Page FPC you'll need to either use AJAX, or roll your own ESI include that passes along the product ID.

  • Thank you, I wanted to avoid to add an ajax request. Adding a param to esi request is a little bit complicated, so I will go with a purge+varnish cache warming at my lowest ttl on product pages. Something like magento.stackexchange.com/questions/3806
    – Laor
    Nov 23 '17 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.