Skip to content

Allow usage of cacheTags for FPC #31310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand All @@ -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));
Expand Down