Feature: #103356 - PSR-14 event BeforeCacheBustingVersionIsAddedEvent
See forge#103356
Description
URIs to public resources of extensions, for example assets added through the
Asset, 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\
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). Itsgetmethod returns a hash of the file content.Hash () $request: The current request, if any (read-only).get/Version () set: The version added to the URI, the modification time of the file by default. An empty version omits cache busting for the resource.Version ()
The version is added as query string, or to the file name if
$GLOBALS or
$GLOBALS is enabled.
Example
Use a hash of the file content, so the version only changes when the content does:
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));
}
}
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.