-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28de6c2
commit e6bab6b
Showing
6 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Magento 2 Extension to prevent that Blocks with a TTL got rendered | ||
----- | ||
|
||
Magento does replace the output HTML of blocks with a TTL with an ESI-Tag for the Varnish Cache. | ||
|
||
In this case Magento wasted time to render the block, because the output of the block will be never used. | ||
|
||
So this module will prevent rendering blocks with a TTL to increase the TTFB (Time to the first byte). | ||
|
||
## Install | ||
|
||
```bash | ||
compose require webidea24/magento-module-prevent-rendering-esi-blocks | ||
bin/magento setup:upgrade | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "webidea24/magento2-module-prevent-rendering-esi-blocks", | ||
"description": "Magento 2 Extension: Prevent rendering ESI blocks", | ||
"type": "magento2-module", | ||
"require": { | ||
"magento/module-page-cache": "*", | ||
"php": "^8.2" | ||
}, | ||
"license": [ | ||
"MIT" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "WEBiDEA", | ||
"homepage": "https://www.webidea24.de/" | ||
} | ||
], | ||
"autoload": { | ||
"files": [ | ||
"src/registration.php" | ||
], | ||
"psr-4": { | ||
"Webidea24\\VarnishPreventRenderEsiBlock\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webidea24\VarnishPreventRenderEsiBlock\Plugin; | ||
|
||
use Magento\Framework\View\Element\AbstractBlock; | ||
use Magento\Framework\View\Layout; | ||
use Magento\PageCache\Model\Config; | ||
|
||
class LayoutPlugin | ||
{ | ||
private bool $_isEnabled; | ||
|
||
public function __construct( | ||
private readonly Config $config | ||
) | ||
{ | ||
} | ||
|
||
public function aroundRenderNonCachedElement(Layout $subject, callable $callable, string $name): ?string | ||
{ | ||
$block = $subject->getBlock($name); | ||
|
||
if ($block instanceof AbstractBlock && is_numeric($block->getTtl()) && ((int)$block->getTtl()) > 0 && $this->isVarnishEnabled()) { | ||
return ''; // esi tag will be added by event observer `core_layout_render_element` | ||
} | ||
|
||
return $callable($name); | ||
} | ||
|
||
private function isVarnishEnabled(): bool | ||
{ | ||
if (!isset($this->_isEnabled)) { | ||
$this->_isEnabled = $this->config->isEnabled() && $this->config->getType() === Config::VARNISH; | ||
} | ||
|
||
return $this->_isEnabled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Framework\View\Layout"> | ||
<plugin name="esi-prevent-render-element" type="Webidea24\VarnishPreventRenderEsiBlock\Plugin\LayoutPlugin"/> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Webidea24_VarnishPreventRenderEsiBlock"/> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webidea24_VarnishPreventRenderEsiBlock', __DIR__); |