magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Private scoped blocks won't show until a POST is performed

Open fxdegroot opened this issue 10 years ago • 6 comments

Preconditions

  1. Create a block with $this->_isScopePrivate = true; in its constructor.
  2. Redefine its _toHtml function to return an empty string if the request is not AJAX and its content if the request is AJAX.
  3. Include this block in your page layout.
  4. You must have full page cache enabled

Steps to reproduce

  1. Go on the page where your block is supposed to show

Expected result

  1. The block is retrieved from an AJAX call to page_cache/block/render.

Actual result

  1. 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 avatar Sep 25 '16 17:09 fxdegroot

@fxdegroot thank you for feedback. Please identify which version of Magento this issue occurs on.

veloraven avatar Sep 26 '16 12:09 veloraven

Hi @veloraven the issue occured on Magento EE 2.1.0

fxdegroot avatar Sep 26 '16 14:09 fxdegroot

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

moloughlin avatar Sep 27 '16 02:09 moloughlin

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 avatar Sep 30 '16 11:09 PascalBrouwers

@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

fxdegroot avatar Oct 10 '16 22:10 fxdegroot

According to https://github.com/magento/magento2/issues/3890#issuecomment-312909473 it seems to be fixed on 2.2 branch.

orlangur avatar Oct 21 '17 17:10 orlangur