Skip to content

Commit

Permalink
[BUGFIX] cached inline xml
Browse files Browse the repository at this point in the history
Fixes: #24
  • Loading branch information
achimfritz committed Nov 29, 2024
1 parent 972072c commit e14bdb8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Classes/Listener/AfterCacheableContentIsGenerated.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@
* of the License, or any later version.
*/

use B13\Assetcollector\AssetCollector;
use B13\Assetcollector\Hooks\AssetRenderer;
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent;

class AfterCacheableContentIsGenerated
{
protected AssetRenderer $assetRenderer;
protected AssetCollector $assetCollector;

public function __construct(AssetRenderer $assetRenderer)
public function __construct(AssetRenderer $assetRenderer, AssetCollector $assetCollector)
{
$this->assetRenderer = $assetRenderer;
$this->assetCollector = $assetCollector;
}

public function __invoke(AfterCacheableContentIsGeneratedEvent $event)
{
$frontendController = $event->getController();
$this->assetRenderer->collectInlineAssets([], $frontendController);
$event->getController()->content = str_ireplace(
'</body>',
$this->assetCollector->buildInlineXmlTag() . '</body>',
$event->getController()->content
);
$event->enableCaching();
}
}
4 changes: 4 additions & 0 deletions Classes/Middleware/InlineSvgInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand All @@ -30,6 +31,9 @@ class InlineSvgInjector implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() > 11) {
return $response;
}
if ($this->isOutputting($response)) {
$svgAsset = $this->getInlineSvgAsset();
if ($svgAsset !== '') {
Expand Down
51 changes: 51 additions & 0 deletions Tests/Functional/Frontend/SvgViewHelperCachedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace B13\Assetcollector\Tests\Functional\Functional;

/*
* This file is part of TYPO3 CMS-based extension "assetcollector" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

class SvgViewHelperCachedTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = ['typo3conf/ext/assetcollector'];
protected array $coreExtensionsToLoad = ['core', 'frontend'];
protected array $pathsToLinkInTestInstance = ['typo3conf/ext/assetcollector/Build/sites' => 'typo3conf/sites'];

protected array $configurationToUseInTestInstance = [
'SYS' => [
'caching' => [
'cacheConfigurations' => [
'pages' => [
'backend' => \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class,
],
],
],
],
];

/**
* @test
*/
public function scriptTagForInlineCssIsRendered(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/SvgViewHelper.csv');
$response = $this->executeFrontendSubRequest(new InternalRequest('http://localhost/'));
$expected = '<svg class="tx_assetcollector"';
$notExected = '</svg><svg class="tx_assetcollector"';
$bodyUncached = (string)$response->getBody();
self::assertStringContainsString($expected, $bodyUncached);
self::assertStringNotContainsString($notExected, $bodyUncached);
// cached
$response = $this->executeFrontendSubRequest(new InternalRequest('http://localhost/'));
$bodyCached = (string)$response->getBody();
self::assertSame($bodyUncached, $bodyCached);
}
}

0 comments on commit e14bdb8

Please sign in to comment.