Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Aug 28, 2024
1 parent 28de6c2 commit e6bab6b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
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
```
26 changes: 26 additions & 0 deletions composer.json
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/"
}
}
}
40 changes: 40 additions & 0 deletions src/Plugin/LayoutPlugin.php
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;
}
}
7 changes: 7 additions & 0 deletions src/etc/frontend/di.xml
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>
5 changes: 5 additions & 0 deletions src/etc/module.xml
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>
7 changes: 7 additions & 0 deletions src/registration.php
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__);

0 comments on commit e6bab6b

Please sign in to comment.