Skip to content

Commit

Permalink
Allow for Varnish ESI blocks to be interpreted separately
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Apr 27, 2022
1 parent 8302864 commit b32a758
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Same WebP image for example.jpg and example.jpeg
- Add event observer to make sure blocks with non-zero TTL are parsed when FPC is enabled

### Added
- Various unit tests
Expand Down
6 changes: 3 additions & 3 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public function addLazyLoading(): bool
}

/**
* @param LayoutInterface $block
* @param LayoutInterface $layout
* @return bool
*/
public function hasFullPageCacheEnabled(LayoutInterface $block): bool
public function hasFullPageCacheEnabled(LayoutInterface $layout): bool
{
if ($this->depersonalizeChecker->checkIfDepersonalize($block)) {
if ($this->depersonalizeChecker->checkIfDepersonalize($layout)) {
return true;
}

Expand Down
76 changes: 76 additions & 0 deletions Observer/ReplaceBlockHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types=1);

namespace Yireo\NextGenImages\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\View\LayoutInterface;
use Yireo\NextGenImages\Config\Config;
use Yireo\NextGenImages\Util\HtmlReplacer;
use Yireo\NextGenImages\Util\ShouldModifyOutput;

/**
* @todo Add integration test for URL /page_cache/block/esi/blocks/[%22catalog.topnav%22]/handles/WyJkZWZhdWx0Il0=
*/
class ReplaceBlockHtml implements ObserverInterface
{
/**
* @var HtmlReplacer
*/
private $htmlReplacer;

/**
* @var ShouldModifyOutput
*/
private $shouldModifyOutput;

/**
* @var LayoutInterface
*/
private $layout;

/**
* @var Config
*/
private $config;

/**
* ReplaceTags constructor.
*
* @param HtmlReplacer $htmlReplacer
* @param ShouldModifyOutput $shouldModifyOutput
* @param LayoutInterface $layout
* @param Config $config
*/
public function __construct(
HtmlReplacer $htmlReplacer,
ShouldModifyOutput $shouldModifyOutput,
LayoutInterface $layout,
Config $config
) {
$this->htmlReplacer = $htmlReplacer;
$this->shouldModifyOutput = $shouldModifyOutput;
$this->layout = $layout;
$this->config = $config;
}

public function execute(Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if (!$block->getData('ttl')) {
return;
}

if (!$this->config->hasFullPageCacheEnabled($this->layout)) {
return;
}

if ($this->shouldModifyOutput->shouldModifyOutput($this->layout) === false) {
return;
}

$transport = $observer->getEvent()->getTransport();
$html = $this->htmlReplacer->replace($transport->getHtml());
$transport->setHtml($html);
}
}
49 changes: 8 additions & 41 deletions Plugin/ReplaceTagsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Yireo\NextGenImages\Plugin;

use Magento\Framework\View\LayoutInterface;
use Yireo\NextGenImages\Config\Config;
use Yireo\NextGenImages\Util\HtmlReplacer;
use Yireo\NextGenImages\Util\ShouldModifyOutput;

class ReplaceTagsPlugin
{
Expand All @@ -14,22 +14,22 @@ class ReplaceTagsPlugin
private $htmlReplacer;

/**
* @var Config
* @var ShouldModifyOutput
*/
private $config;

private $shouldModifyOutput;
/**
* ReplaceTags constructor.
*
* @param HtmlReplacer $htmlReplacer
* @param Config $config
* @param ShouldModifyOutput $shouldModifyOutput
*/
public function __construct(
HtmlReplacer $htmlReplacer,
Config $config
ShouldModifyOutput $shouldModifyOutput
) {
$this->htmlReplacer = $htmlReplacer;
$this->config = $config;
$this->shouldModifyOutput = $shouldModifyOutput;
}

/**
Expand All @@ -41,43 +41,10 @@ public function __construct(
*/
public function afterGetOutput(LayoutInterface $layout, string $output): string
{
if (!$this->config->enabled()) {
return $output;
}

if ($this->shouldModifyOutput($layout) === false) {
if ($this->shouldModifyOutput->shouldModifyOutput($layout) === false) {
return $output;
}

return $this->htmlReplacer->replace($output);
}

/**
* @param LayoutInterface $layout
* @return bool
*/
private function shouldModifyOutput(LayoutInterface $layout): bool
{
$handles = $layout->getUpdate()->getHandles();
if (empty($handles)) {
return false;
}

foreach ($handles as $handle) {
if (strstr($handle, '_email_')) {
return false;
}
}

$skippedHandles = [
'webp_skip',
'nextgenimages_skip',
];

if (array_intersect($skippedHandles, $handles)) {
return false;
}

return true;
}
}
56 changes: 56 additions & 0 deletions Util/ShouldModifyOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Yireo\NextGenImages\Util;

use Magento\Framework\View\LayoutInterface;
use Yireo\NextGenImages\Config\Config;

class ShouldModifyOutput
{
/**
* @var Config
*/
private $config;

/**
* @param Config $config
*/
public function __construct(
Config $config
) {
$this->config = $config;
}

/**
* @param LayoutInterface $layout
* @return bool
*/
public function shouldModifyOutput(LayoutInterface $layout): bool
{
if (!$this->config->enabled()) {
return false;
}

$handles = $layout->getUpdate()->getHandles();
if (empty($handles)) {
return false;
}

foreach ($handles as $handle) {
if (strstr($handle, '_email_')) {
return false;
}
}

$skippedHandles = [
'webp_skip',
'nextgenimages_skip',
];

if (array_intersect($skippedHandles, $handles)) {
return false;
}

return true;
}
}
6 changes: 6 additions & 0 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="view_block_abstract_to_html_after">
<observer name="Yireo_NextGenImages::replaceBlockHtml" instance="Yireo\NextGenImages\Observer\ReplaceBlockHtml" />
</event>
</config>

0 comments on commit b32a758

Please sign in to comment.