Feature: #97862 - New PSR-14 events for manipulating frontend page generation and cache behaviour

See forge#97862

Description

Two new PSR-14 events have been added:

  • TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent
  • TYPO3\CMS\Frontend\Event\AfterCachedPageIsPersistedEvent

They are added in favor of the removed hooks:

  • $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-cached']
  • $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all']
  • $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['usePageCache']
  • $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['insertPageIncache']

Both events are called when the content of a page has been generated in the TYPO3 Frontend.

Example

Registration of the AfterCacheableContentIsGeneratedEvent in your extension's Services.yaml:

MyVendor\MyPackage\Backend\MyEventListener:
  tags:
    - name: event.listener
      identifier: 'my-package/content-modifier'
Copied!

The corresponding event listener class:

use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent;

class MyEventListener {

    public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void
    {
        // Only do this when caching is enabled
        if (!$event->isCachingEnabled()) {
            return;
        }
        $event->getController()->content = str_replace('foo', 'bar', $event->getController()->content);
    }
}
Copied!

Impact

The event AfterCacheableContentIsGeneratedEvent can be used to decide if a page should be stored in cache and is executed right after all cacheable content is generated. It can also be used to manipulate the content before it is stored in TYPO3's page cache. The event is used in indexed search to index cacheable content.

The AfterCacheableContentIsGeneratedEvent contains the information if a generated page is able to store in cache via the $event->isCachingEnabled() method. This can be used to differentiate between the previous hooks contentPostProc-cached and contentPostProc-all (do something regardless if caching is enabled or not).

The AfterCachedPageIsPersistedEvent is commonly used to generate a static file cache. This event is only called if the page was actually stored in TYPO3's page cache.