Private scoped blocks won't show until a POST is performed
Preconditions
- Create a block with $this->_isScopePrivate = true; in its constructor.
- Redefine its _toHtml function to return an empty string if the request is not AJAX and its content if the request is AJAX.
- Include this block in your page layout.
- You must have full page cache enabled
Steps to reproduce
- Go on the page where your block is supposed to show
Expected result
- The block is retrieved from an AJAX call to page_cache/block/render.
Actual result
- An empty block is shown and no AJAX call is performed
Note that once a POST request is performed on the site (for instance adding a product to the cart), the block will be rendered from the AJAX call without any issue. This bug is caused because the cookie 'private_content_version' is not yet set. Because of this, no ajax call is performed in vendor/magento/module-page-cache/view/frontend/web/js/page-cache.js (lines 97-100).
This gets even more obvious when looking at the function process() of Magento\Framework\App\PageCache\Version. Its comment states 'Set cookie if it is not set.' whereas it is not the case, it is only set when a POST request is performed.
Here is a suggested fix (line 78) :
if ($this->request->isPost() || !$this->cookieManager->getCookie(self::COOKIE_NAME)) {
@fxdegroot thank you for feedback. Please identify which version of Magento this issue occurs on.
Hi @veloraven the issue occured on Magento EE 2.1.0
I've been waiting on this one for a while as well...there's a PR which implements the same fix you've suggested here https://github.com/magento/magento2/pull/6406
I couldn't reproduce it for Magento CE 2.1.0 Yet, I made a mistake setting the "$this->_isScopePrivate = true;" after the call to parent::__construct
Setting it before that fixed it for me and the non-cached block showed up (a preference for \Magento\Checkout\Block\Cart\Sidebar)
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Catalog\Helper\Image $imageHelper,
\Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider,
array $data
) {
// Do not cache this block
$this->_isScopePrivate = true;
parent::__construct($context, $customerSession, $checkoutSession, $imageHelper, $jsLayoutDataProvider, $data);
}
@PascalBrouwers
Your solution is not working. Blocks defined as "private scoped" are correctly listed by Magento in the page-cache.js file, whether you set the property before or after calling parent's constructor.
The problem is that the cookie is never set if you don't do any POST request, and then no dynamic blocks will be shown.
See @moloughlin PR for detailed explanation : https://github.com/magento/magento2/pull/6406 Similar reports : https://github.com/magento/magento2/issues/3890
According to https://github.com/magento/magento2/issues/3890#issuecomment-312909473 it seems to be fixed on 2.2 branch.