b13 / assetcollector

Add CSS / SVG / JS to Content templates, and load them only once
GNU General Public License v2.0
8 stars 0 forks source link

Not working in v13 with INT objects #24

Open m-kappenberg opened 3 months ago

m-kappenberg commented 3 months ago

Steps to reproduce:

  1. Add a simple INT object to the Page
  2. Clear page cache
  3. Make sure you are not logged in backend, delete BE login cookie to be sure
  4. View page in frontend, SVG with 'defs' is added
  5. Reload the page with the INT object several times
  6. The SVG with the 'defs' is only added the first time, after reloads it is gone

My solution: I've completely removed the InlineSvgInjector Middleware, and added the SVG asset collector as footer data:

in _extlocalconf.php add:

        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postProcess'][]
            = \B13\Assetcollector\Hooks\PageRenderer\PostProcessHook::class . '->execute';

add Classes/Hooks/PageRenderer/PostProcessHook.php :

<?php

declare(strict_types=1);

namespace B13\Assetcollector\Hooks\PageRenderer;

use B13\Assetcollector\AssetCollector;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
 * PostProcessHook, this is referenced in ext_localconf.php
 * and should be executed once after all page rendering
 */
class PostProcessHook
{
    /**
     * @param array $params  The already used JS and CSS files and the header and footer data
     * @param PageRenderer $pageRenderer  The back reference to the TYPO3\CMS\Core\Page\PageRenderer class
     */
    public function execute(array &$params, PageRenderer &$pageRenderer): void
    {
        if (!($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface ||
            !ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
            return;
        }

        $assetCollector = GeneralUtility::makeInstance(AssetCollector::class);
// seems always empty, solution will follow
//        $cached = $this->getTypoScriptFrontendController()->config['b13/assetcollector'] ?? [];
//        if (!empty($cached['xmlFiles'] ?? null) && is_array($cached['xmlFiles'])) {
//            $assetCollector->mergeXmlFiles($cached['xmlFiles']);
//        }
        $svgReferences = $assetCollector->buildInlineXmlTag();
        $pageRenderer->addFooterData('<!-- SVG asset renderer -->' . PHP_EOL . $svgReferences);
        // not working with INT objects...
        //  $params['bodyContent'] = $params['bodyContent'] . '<!-- SVG asset renderer -->' . PHP_EOL . $svgReferences;
    }
// not used, see WIP comment above
    protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
    {
        return $GLOBALS['TSFE'] ?? null;
    }
}

Maybe you can check this solution?

I'm working on a solution for the "set SVG defs via TypoScript", too.

Best regards Matthias