It is required to show dynamic javascript userId
var in head.additional block if the user is logged in, otherwise null
.
So I am injecting my block into default
layout handle
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="head.additional">
<block class="Test\Test\Block\Head" name="testtest_head" />
</referenceContainer>
</body>
</page>
in the block, I use this code and it works great
class Head extends Template
{
...
protected function _afterToHtml($html)
{
return $html. $this->getMyUserIdCode();
}
}
But when I enable the cache, userId
won't refresh even if customer logged in. How can I properly avoid caching in this sutuation ?