:::: MENU ::::

How to change cache TTL for ESI blocks in Varnish VCL.

0.00 avg. rating (0% score) - 0 votes

Sometimes you need a compromise between the time cache holds an object (to improve the page load times) and the time the changes are applied.

Magento 2 provides some mechanisms for the content to be updated when is modified:

For example when a product is updated, the corresponding URL is flushed from Varnish so the object is regenerated the next time a user request it.

But this not happen with blocks nor widgets. If you update a CMS block that is inserted in a static page or via layout, you will notice the block is not updated.

The TTL that applies to the blocks and widgets is the general TTL which is by default 1 day, so your product or marketing team need to wait for an entire 24 hours to the the block updated.

To avoid this, and knowing that block and widgets are cached via ESI mechanism, you can just simply override the TTL for those specific cases (we can assume that we can do it whenever something is loaded by ESI).

To achieve that, simply add this to your VCL in the function vcl_backend_response:

sub vcl_backend_response {

  . . .

  # Check if the response is an ESI block
  if (beresp.do_esi) {
    # Set a new TTL for the response
    set beresp.ttl = 1h;

  . . .


Hey! Qué opinas sobre el artículo?