Skip to content

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

@fxdegroot

Description

@fxdegroot

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)) {

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions