From 329839a3e8e07caaaaf9dbe31ae06e32ff337ebb Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Wed, 16 Dec 2020 18:03:51 +0100 Subject: [PATCH] Allow usage of cacheTags for FPC Regarding the AbstarctBlock implementation of getCacheTags, tags from identities are merges to cache tags anyway, so we could use it safely. Also Magento does not recommend to implement new Blocks, so we could declare the block's tags in the layout xml for the FPC. --- .../PageCache/Model/Layout/LayoutPlugin.php | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php b/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php index 6aff8aef2c2d9..18668208bcb69 100644 --- a/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php +++ b/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php @@ -10,10 +10,15 @@ use Magento\Framework\App\MaintenanceMode; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\ResponseInterface; +use Magento\Framework\DataObject; use Magento\Framework\DataObject\IdentityInterface; +use Magento\Framework\View\Element\BlockInterface; use Magento\Framework\View\Layout; use Magento\PageCache\Model\Config; use Magento\PageCache\Model\Spi\PageCacheTagsPreprocessorInterface; +use function array_merge; +use function array_unique; +use function implode; /** * Append cacheable pages response headers. @@ -67,7 +72,7 @@ public function __construct( * @param Layout $subject * @return void */ - public function afterGenerateElements(Layout $subject) + public function afterGenerateElements(Layout $subject): void { if ($subject->isCacheable() && !$this->maintenanceMode->isOn() && $this->config->isEnabled()) { $this->response->setPublicHeaders($this->config->getTtl()); @@ -78,22 +83,22 @@ public function afterGenerateElements(Layout $subject) * Retrieve all identities from blocks for further cache invalidation. * * @param Layout $subject - * @param mixed $result - * @return mixed + * @param string $result + * @return string */ - public function afterGetOutput(Layout $subject, $result) + public function afterGetOutput(Layout $subject, string $result): string { if ($subject->isCacheable() && $this->config->isEnabled()) { $tags = []; $isVarnish = $this->config->getType() === Config::VARNISH; + /** @var BlockInterface[] $block */ foreach ($subject->getAllBlocks() as $block) { - if ($block instanceof IdentityInterface) { - $isEsiBlock = $block->getTtl() > 0; - if ($isVarnish && $isEsiBlock) { - continue; + if (!$isVarnish || ($block instanceof DataObject && !$block->getData('ttl'))) { + $tags[] = (array) $block->getData('cache_tags'); + if ($block instanceof IdentityInterface) { + $tags[] = $block->getIdentities(); } - $tags[] = $block->getIdentities(); } } $tags = array_unique(array_merge([], ...$tags));