cancel
Showing results for 
Search instead for 
Did you mean: 

Page Cache Identifier plugin not working after upgrade from 2.1 to 2.3

Page Cache Identifier plugin not working after upgrade from 2.1 to 2.3

I'm testing an upgrade to bring our Magento 2.1.x system to Magento 2.3.x. One issue I'm having is that a plugin we had written to modify the Magento PageCache identifier is no longer producing the expected caching behavior.

 

The purpose of this plugin was to add an additional value to the page cache identifier so that we could have two different versions of the page cached depending on a special value that was set in the request header. The difference in the two versions of the page is that the variant page has an additional link in the footer that should only be displayed if the special value was set in the header. Now, only one version of the page gets cached, and the footer ends up being cached either with the link or without it, depending on whether the initial request to the page had the header.

 

Here's the relevant line from the plugin's di.xml:

 

 

<type name="Magento\Framework\App\PageCache\Identifier">
<plugin name="our_plugin_name" type="OurCompanyName\PageCache\Plugin\App\PageCache\Identifier"/>
</type>

And in Identifier.php:

 

 

<?php

namespace OurCompanyName\PageCache\Plugin\App\PageCache;

use Magento\Framework\App\Http\Context;
use Magento\Framework\App\PageCache\Identifier as Subject;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Exception\NoSuchEntityException;

class Identifier
{

    /**
     * @var Http
     */
    private $request;

    /**
     * @var Context
     */
    private $context;

    /**
     * Identifier constructor.
     * @param Http $request
     * @param Context $context
     */
    public function __construct(
        Http $request,
        Context $context
    )
    {
        $this->request = $request;
        $this->context = $context;
    }

    /**
     * @param Subject $subject
     * @param string $identifier
     * @return string
     */
    public function aroundGetValue(Subject $subject, $identifier) {
        $data = [
            $this->request->isSecure(),
            $this->request->getUriString(),
            $this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
            ?: $this->context->getVaryString(),
            $this->getOurCustomData()
        ];
        return sha1(json_encode($data));

    }

    protected function getOurCustomData() {
	$ourCustomValue = $this->request->getHeader('Our-Custom-Header');
        return $ourCustomValue;
    }

}

 

The footer link in question is being added via a modification to the default theme:

 

        <referenceBlock name="footer_links">
                <block class="Magento\Theme\Block\Html\Footer" name="our-custom-footer-link" template="html/custom-footer-link.phtml" after="other-link"/>
        </referenceBlock>

The custom-footer-link.phtml template is the place that contains the logic that determines whether the link should appear:

 

 

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var $block \Magento\Framework\View\Element\Html\Link\Current
 */
$_request = $block->getRequest();
$custom_header_value = $_request->getHeader('Our-Custom-Header');
$customHeaderIsSet = $custom_header_value === '1' ? '1' : '0';
?>
<?php if ($customHeaderIsSet): ?>
<li class="nav item"><a href="/custom-link/">Custom Link</a></li>
<?php endif; ?>

 

 

I have added some error_log statements to the above code in various places to confirm:

- The PageCache Identifier data includes the expected values, including the value based on the custom header.

- The logic in the custom footer link template is still working properly, and the custom link shows up conditionally based on the header value when caching is disabled.

 

Any suggestions on what might have changed or why I may not be getting two different cached page versions with this setup?