Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] cached inline xml #25

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Build/phpunit/FunctionalTestsBootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the TYPO3 CMS project.
*
Expand Down
1 change: 1 addition & 0 deletions Build/phpunit/UnitTestsBootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the TYPO3 CMS project.
*
Expand Down
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);
}
}
Loading