[BUGFIX] Cache generated meta tags 61/58261/9
authorRichard Haeser <richard@maxserv.com>
Wed, 12 Sep 2018 19:17:44 +0000 (21:17 +0200)
committerSusanne Moog <susanne.moog@typo3.org>
Wed, 19 Sep 2018 21:01:09 +0000 (23:01 +0200)
Cache generated meta tags to prevent losing meta tags when a
non-cached content element is on the page.

Resolves: #86234
Releases: master
Change-Id: I6d48feee40716fb07c694b787a329940d2740ad6
Reviewed-on: https://review.typo3.org/58261
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Guido Schmechel <guido.schmechel@brandung.de>
Tested-by: Guido Schmechel <guido.schmechel@brandung.de>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/core/Classes/MetaTag/MetaTagManagerRegistry.php
typo3/sysext/core/Classes/Page/PageRenderer.php
typo3/sysext/core/Tests/Unit/MetaTag/MetaTagManagerRegistryTest.php
typo3/sysext/core/Tests/Unit/Page/PageRendererTest.php

index d736b78..b542c60 100644 (file)
@@ -86,9 +86,9 @@ class MetaTagManagerRegistry implements SingletonInterface
         );
 
         $managers = [];
-        foreach ($orderedManagers as $managerConfiguration) {
+        foreach ($orderedManagers as $manager => $managerConfiguration) {
             if (class_exists($managerConfiguration['module'])) {
-                $managers[] = GeneralUtility::makeInstance($managerConfiguration['module']);
+                $managers[$manager] = GeneralUtility::makeInstance($managerConfiguration['module']);
             }
         }
 
index 4019235..4560431 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Core\Page;
 use TYPO3\CMS\Backend\Routing\Router;
 use TYPO3\CMS\Backend\Routing\UriBuilder;
 use TYPO3\CMS\Core\Cache\CacheManager;
+use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Core\Environment;
 use TYPO3\CMS\Core\Localization\LocalizationFactory;
@@ -1660,9 +1661,32 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface
     {
         $metaTags = [];
         $metaTagManagers = $this->metaTagRegistry->getAllManagers();
-        foreach ($metaTagManagers as $manager) {
-            if ($properties = $manager->renderAllProperties()) {
+        try {
+            $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_pages');
+        } catch (NoSuchCacheException $e) {
+            $cache = null;
+        }
+
+        foreach ($metaTagManagers as $manager => $managerObject) {
+            $cacheIdentifier =  $this->getTypoScriptFrontendController()->newHash . '-metatag-' . $manager;
+
+            $existingCacheEntry = false;
+            if ($cache instanceof FrontendInterface && $properties = $cache->get($cacheIdentifier)) {
+                $existingCacheEntry = true;
+            } else {
+                $properties = $managerObject->renderAllProperties();
+            }
+
+            if (!empty($properties)) {
                 $metaTags[] = $properties;
+
+                if ($cache instanceof FrontendInterface && !$existingCacheEntry) {
+                    $cache->set(
+                        $cacheIdentifier,
+                        $properties,
+                        ['pageId_' . $this->getTypoScriptFrontendController()->page['uid']]
+                    );
+                }
             }
         }
         return $metaTags;
index bd34937..9ad31e8 100644 (file)
@@ -108,8 +108,8 @@ class MetaTagManagerRegistryTest extends UnitTestCase
                     ]
                 ],
                 [
-                    new OpenGraphMetaTagManager(),
-                    new GenericMetaTagManager()
+                    'opengraph' => new OpenGraphMetaTagManager(),
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
             [
@@ -128,8 +128,8 @@ class MetaTagManagerRegistryTest extends UnitTestCase
                     ],
                 ],
                 [
-                    new OpenGraphMetaTagManager(),
-                    new GenericMetaTagManager()
+                    'opengraph' => new OpenGraphMetaTagManager(),
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
             [
@@ -148,9 +148,9 @@ class MetaTagManagerRegistryTest extends UnitTestCase
                     ],
                 ],
                 [
-                    new Html5MetaTagManager(),
-                    new OpenGraphMetaTagManager(),
-                    new GenericMetaTagManager()
+                    'html5' => new Html5MetaTagManager(),
+                    'opengraph' => new OpenGraphMetaTagManager(),
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
             [
@@ -169,9 +169,9 @@ class MetaTagManagerRegistryTest extends UnitTestCase
                     ],
                 ],
                 [
-                    new OpenGraphMetaTagManager(),
-                    new Html5MetaTagManager(),
-                    new GenericMetaTagManager()
+                    'opengraph' => new OpenGraphMetaTagManager(),
+                    'html5' => new Html5MetaTagManager(),
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
             [
@@ -190,9 +190,9 @@ class MetaTagManagerRegistryTest extends UnitTestCase
                     ],
                 ],
                 [
-                    new OpenGraphMetaTagManager(),
-                    new Html5MetaTagManager(),
-                    new GenericMetaTagManager()
+                    'opengraph' => new OpenGraphMetaTagManager(),
+                    'html5' => new Html5MetaTagManager(),
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
             [
@@ -217,16 +217,16 @@ class MetaTagManagerRegistryTest extends UnitTestCase
                     ],
                 ],
                 [
-                    new OpenGraphMetaTagManager(),
-                    new TwitterCardMetaTagManager(),
-                    new Html5MetaTagManager(),
-                    new GenericMetaTagManager()
+                    'opengraph' => new OpenGraphMetaTagManager(),
+                    'twitter' => new TwitterCardMetaTagManager(),
+                    'html5' => new Html5MetaTagManager(),
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
             [
                 [],
                 [
-                    new GenericMetaTagManager()
+                    'generic' => new GenericMetaTagManager()
                 ]
             ],
         ];
index 337f7f3..d40a05e 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Core\Tests\Unit\Page;
  */
 
 use TYPO3\CMS\Core\Page\PageRenderer;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -28,9 +29,12 @@ class PageRendererTest extends UnitTestCase
     public function renderMethodCallsResetInAnyCase()
     {
         $this->resetSingletonInstances = true;
+        $tsfe = $this->prophesize(TypoScriptFrontendController::class);
+
         $pageRenderer = $this->getMockBuilder(PageRenderer::class)
-            ->setMethods(['reset', 'prepareRendering', 'renderJavaScriptAndCss', 'getPreparedMarkerArray', 'getTemplateForPart'])
+            ->setMethods(['reset', 'prepareRendering', 'renderJavaScriptAndCss', 'getPreparedMarkerArray', 'getTemplateForPart', 'getTypoScriptFrontendController'])
             ->getMock();
+        $pageRenderer->expects($this->any())->method('getTypoScriptFrontendController')->willReturn($tsfe->reveal());
         $pageRenderer->expects($this->exactly(3))->method('reset');
 
         $pageRenderer->render(PageRenderer::PART_COMPLETE);