From e6bab6bf817726f0f90f3efd6ba436c105c7cdfd Mon Sep 17 00:00:00 2001 From: Frederik Rommel Date: Wed, 28 Aug 2024 14:09:21 +0200 Subject: [PATCH] initial commit --- README.md | 15 ++++++++++++++ composer.json | 26 ++++++++++++++++++++++++ src/Plugin/LayoutPlugin.php | 40 +++++++++++++++++++++++++++++++++++++ src/etc/frontend/di.xml | 7 +++++++ src/etc/module.xml | 5 +++++ src/registration.php | 7 +++++++ 6 files changed, 100 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Plugin/LayoutPlugin.php create mode 100644 src/etc/frontend/di.xml create mode 100644 src/etc/module.xml create mode 100644 src/registration.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..753a2c0 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7683d9d --- /dev/null +++ b/composer.json @@ -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/" + } + } +} diff --git a/src/Plugin/LayoutPlugin.php b/src/Plugin/LayoutPlugin.php new file mode 100644 index 0000000..491445b --- /dev/null +++ b/src/Plugin/LayoutPlugin.php @@ -0,0 +1,40 @@ +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; + } +} diff --git a/src/etc/frontend/di.xml b/src/etc/frontend/di.xml new file mode 100644 index 0000000..482dbd7 --- /dev/null +++ b/src/etc/frontend/di.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/src/etc/module.xml b/src/etc/module.xml new file mode 100644 index 0000000..2a276b9 --- /dev/null +++ b/src/etc/module.xml @@ -0,0 +1,5 @@ + + + + diff --git a/src/registration.php b/src/registration.php new file mode 100644 index 0000000..ee2ec3b --- /dev/null +++ b/src/registration.php @@ -0,0 +1,7 @@ +