[PHP] Compress HTML output from TYPO3

[PHP] Compress HTML output from TYPO3

If you want to remove all superfluous spaces and breaks in the HTML of your TYPO3 page, we might have just the thing for you. You do not need an extra extension for this, you can simply add 2 additional files to your site package.

Google Pagespeed also complains from time to time: In addition to the recommended GZIP compression, you can also remove unnecessary characters (e.g. spaces, breaks and comments) from the rendered HTML of a TYPO3 page. Although there is already a ViewHelper (f:spaceless) that helps you to compress the code a bit in the Fluid, it usually only takes care of the body area of your website. If you really want to compress everything, the use of PSR-15 middleware is recommended.

PHP package wyrihaximus/html-compress helps with compressing. Just add it to your composer.json:

{ "name": "vendor/sitepackage", "description": "Sitepackage extension", "type": "typo3-cms-extension", "homepage": "https://www.in2code.de", "require": { "typo3/cms-core": "^10.4", "wyrihaximus/html-compress": "^1" }, "autoload": { "psr-4": { "Vendor\\Sitepackage\\": "Classes/" } } }

EXT:sitepackage/Configuration/RequestMiddlewares.php:

<?php return [ 'frontend' => [ 'sitepackage-compress-html' => [ 'target' => \Vendor\Sitepackage\Middleware\HtmlCompress::class, 'before' => [ 'typo3/cms-frontend/output-compression' ], 'after' => [ 'typo3/cms-adminpanel/renderer' ] ] ] ];

EXT:sitepackage/Classes/Middleware/HtmlCompress.php:

<?php declare(strict_types=1); namespace Vendor\Sitepackage\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\Http\StreamFactory; use WyriHaximus\HtmlCompress\Factory; /** * Class HtmlCompress */ class HtmlCompress implements MiddlewareInterface { /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); if ($this->isTypeNumSet($request) === false) { $stream = $response->getBody(); $stream->rewind(); $content = $stream->getContents(); $newBody = (new StreamFactory())->createStream($this->compressHtml($content)); $response = $response->withBody($newBody); } return $response; } /** * @param string $html * @return string */ protected function compressHtml(string $html): string { $parser = Factory::construct(); $html = $parser->compress($html); $html = $this->removeComments($html); return $html; } /** * Remove all html comments but not "<!--TYPO3SEARCH_begin-->" and "<!--TYPO3SEARCH-end-->" * @param string $html * @return string */ protected function removeComments(string $html): string { return preg_replace('/<!--((?!TYPO3SEARCH)[\s\S])*?-->/', '', $html); } /** * @param ServerRequestInterface $request * @return bool */ protected function isTypeNumSet(ServerRequestInterface $request): bool { return $request->getAttribute('routing')->getPageType() > 0; } }

Note: If you remove HTML comments, please note that you need the comments "TYPO3SEARCH _..." for Solr or the index search! The code above already takes this into account.

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news