Skip to content

Commit 329839a

Browse files
committed
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.
1 parent f26ae80 commit 329839a

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
use Magento\Framework\App\MaintenanceMode;
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\App\ResponseInterface;
13+
use Magento\Framework\DataObject;
1314
use Magento\Framework\DataObject\IdentityInterface;
15+
use Magento\Framework\View\Element\BlockInterface;
1416
use Magento\Framework\View\Layout;
1517
use Magento\PageCache\Model\Config;
1618
use Magento\PageCache\Model\Spi\PageCacheTagsPreprocessorInterface;
19+
use function array_merge;
20+
use function array_unique;
21+
use function implode;
1722

1823
/**
1924
* Append cacheable pages response headers.
@@ -67,7 +72,7 @@ public function __construct(
6772
* @param Layout $subject
6873
* @return void
6974
*/
70-
public function afterGenerateElements(Layout $subject)
75+
public function afterGenerateElements(Layout $subject): void
7176
{
7277
if ($subject->isCacheable() && !$this->maintenanceMode->isOn() && $this->config->isEnabled()) {
7378
$this->response->setPublicHeaders($this->config->getTtl());
@@ -78,22 +83,22 @@ public function afterGenerateElements(Layout $subject)
7883
* Retrieve all identities from blocks for further cache invalidation.
7984
*
8085
* @param Layout $subject
81-
* @param mixed $result
82-
* @return mixed
86+
* @param string $result
87+
* @return string
8388
*/
84-
public function afterGetOutput(Layout $subject, $result)
89+
public function afterGetOutput(Layout $subject, string $result): string
8590
{
8691
if ($subject->isCacheable() && $this->config->isEnabled()) {
8792
$tags = [];
8893
$isVarnish = $this->config->getType() === Config::VARNISH;
8994

95+
/** @var BlockInterface[] $block */
9096
foreach ($subject->getAllBlocks() as $block) {
91-
if ($block instanceof IdentityInterface) {
92-
$isEsiBlock = $block->getTtl() > 0;
93-
if ($isVarnish && $isEsiBlock) {
94-
continue;
97+
if (!$isVarnish || ($block instanceof DataObject && !$block->getData('ttl'))) {
98+
$tags[] = (array) $block->getData('cache_tags');
99+
if ($block instanceof IdentityInterface) {
100+
$tags[] = $block->getIdentities();
95101
}
96-
$tags[] = $block->getIdentities();
97102
}
98103
}
99104
$tags = array_unique(array_merge([], ...$tags));

0 commit comments

Comments
 (0)