When I refresh page and check the header of homepage I got this below information where x-varnish-age=0
enter image description here
Want to confirm if there is an issue with cacheable="false"? If any block on page is cacheable="false" so it causes x-varnish-age=0 for that page ? If yes if I have to make some block noncacheable, what should I do to make page varnish cacheable ?
2 Answers 2
I see that there's a Set-Cookie header in your response. That will cause Varnish to mark this incoming response as uncacheable because it changes the state.
If the next request for this resource doesn't return a response that contains the Set-Cookie header, you'll be fine and Varnish will store the object in the cache.
If every request returns a Set-Cookie header, the page will never end up in the cache.
As yourself why this cookie needs to be set and if this happens only once?
If you're looking for more information on Varnish and Magento, please have a look at the following article: https://www.varnish-software.com/developers/tutorials/configuring-varnish-magento/
The following function checks if a block is cacheable and is called for all blocks on page load. You can modify it (temporarily) to log the name in case of an uncacheable block:
vendor/magento/framework/View/Layout.php
/**
* Check existed non-cacheable layout elements.
*
* @return bool
*/
public function isCacheable()
{
$this->build();
$elements = $this->getXml()->xpath('//' . Element::TYPE_BLOCK . '[@cacheable="false"]');
$cacheable = $this->cacheable;
foreach ($elements as $element) {
$blockName = $element->getBlockName();
if ($blockName !== false && $this->structure->hasElement($blockName)) {
$cacheable = false;
//LOG $blockName
break;
}
}
return $cacheable;
}
If the culprit does turn out to be an uncacheable block, you can goto the associated layout .xml file and make changes accordingly (if you are sure it won't affect the functionality of your app).