Feature: #103356 - PSR-14 event BeforeCacheBustingVersionIsAddedEvent 

See forge#103356

Description 

URIs to public resources of extensions, for example assets added through the AssetCollector , contain a version for cache busting, which is the modification time of the file. Deployments that build new containers or releases change the modification time of every file, even if its content is unchanged, so browsers download all assets again after each deployment.

The new PSR-14 event \TYPO3\CMS\Core\SystemResource\Event\BeforeCacheBustingVersionIsAddedEvent is dispatched before the version is added to the URI of a public extension resource, whenever such a URI is generated with cache busting enabled. Listeners can replace the version. The event provides the following properties and methods:

  • $resource : The public resource the URI is generated for (read-only). Its getHash() method returns a hash of the file content.
  • $request : The current request, if any (read-only).
  • getVersion() / setVersion() : The version added to the URI, the modification time of the file by default. An empty version omits cache busting for the resource.

The version is added as query string, or to the file name if $GLOBALS['TYPO3_CONF_VARS']['FE']['versionNumberInFilename'] or $GLOBALS['TYPO3_CONF_VARS']['BE']['versionNumberInFilename'] is enabled.

Example 

Use a hash of the file content, so the version only changes when the content does:

EXT:my_extension/Classes/EventListener/ContentHashCacheBustingVersion.php
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\SystemResource\Event\BeforeCacheBustingVersionIsAddedEvent;

#[AsEventListener(identifier: 'my-extension/content-hash-cache-busting-version')]
final readonly class ContentHashCacheBustingVersion
{
    public function __invoke(BeforeCacheBustingVersionIsAddedEvent $event): void
    {
        $event->setVersion(substr($event->resource->getHash(), 0, 12));
    }
}
Copied!

The event is not dispatched in the Install Tool, which keeps using the modification time.

Impact 

The version used for cache busting of public extension resources can now be based on the file content or any other source, so assets stay cached in browsers across deployments as long as they do not change.