Implement html compression

This commit is contained in:
Björn Fromme
2021-12-26 17:34:59 +01:00
parent 4f23350b8f
commit 3d8e22c2fc
5 changed files with 2844 additions and 2 deletions
+1
View File
@@ -55,6 +55,7 @@
"typo3/cms-viewpage": "^10.4",
"wazum/pagetree-resizable": "^1.2",
"web-vision/wv_deepltranslate": "^2.0",
"wyrihaximus/html-compress": "^4.1",
"yoast-seo-for-typo3/yoast_seo": "^7.2",
"yohang/calendr": "^2.1"
},
Generated
+2777 -2
View File
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,54 @@
<?php
namespace EP\EpTheme\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Http\StreamFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WyriHaximus\HtmlCompress\Factory;
class HtmlCompress implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
$compressionEnabled = GeneralUtility::makeInstance(ExtensionConfiguration::class)
->get('ep_theme', 'compressHtml');
if (false === (bool)$compressionEnabled || $this->isTypeNumSet($request)) {
return $response;
}
$stream = $response->getBody();
$stream->rewind();
$content = $stream->getContents();
$newBody = (new StreamFactory())->createStream($this->compressHtml($content));
$response = $response->withBody($newBody);
return $response;
}
protected function compressHtml(string $html): string
{
$parser = Factory::construct();
$html = $parser->compress($html);
$html = $this->removeComments($html);
return $html;
}
protected function removeComments(string $html): string
{
return preg_replace('/<!--((?!TYPO3SEARCH)[\s\S])*?-->/', '', $html);
}
protected function isTypeNumSet(ServerRequestInterface $request): bool
{
return $request->getAttribute('routing')->getPageType() > 0;
}
}
@@ -8,5 +8,14 @@ return [
'typo3/cms-frontend/site',
],
],
'ep/html-compress' => [
'target' => \EP\EpTheme\Middleware\HtmlCompress::class,
'before' => [
'typo3/cms-frontend/output-compression'
],
'after' => [
'typo3/cms-adminpanel/renderer'
],
],
],
];
@@ -0,0 +1,3 @@
# customsubcategory=100=General
# cat=eptheme/100/100; type=boolean; label=Compress HTML
compressHtml = 0