CacheableBinaryFileResponseSubscriber
Makes file responses cacheable by reverse proxies and CDNs.
Drupal's internal page caches never store these responses: the Page Cache skips file responses, and DenyCacheableBinaryFileResponse keeps them out of the Dynamic Page Cache. However, they will be cacheable by external reverse proxies like Varnish and CDNs, with the page cache maximum age from the performance settings.
Like core's FileDownloadController, only files on the public scheme, or one listed in the file_additional_public_schemes setting, are made cacheable. Private and temporary files, including image style derivatives of private files, and files without a scheme are never converted.
- Full name:
\Drupal\helper\EventSubscriber\CacheableBinaryFileResponseSubscriber - This class is marked as final and can't be subclassed
- This class implements:
EventSubscriberInterface - This class is a Final class
Properties
configFactory
protected \Drupal\Core\Config\ConfigFactoryInterface $configFactory
Methods
__construct
Constructs a new CacheableBinaryFileResponseSubscriber.
public __construct(\Drupal\Core\Config\ConfigFactoryInterface $configFactory): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$configFactory |
\Drupal\Core\Config\ConfigFactoryInterface | |
| The configuration factory. |
convertBinaryFileResponse
Convert a BinaryFileResponse into a CacheableBinaryFileResponse.
public convertBinaryFileResponse(\Symfony\Component\HttpKernel\Event\ResponseEvent $event): void
This method entirely replaces the response object since we need one that implements \Drupal\Core\Cache\CacheableResponseInterface for Drupal's FinishResponseSubscriber to actually add the cache headers. Subclasses of BinaryFileResponse are left alone, since they may handle caching themselves, as are files that aren't public (see \Drupal\helper\File::isPublicUri()). Request attributes with cacheability metadata, such as the route's entities, are added as cacheable dependencies, along with the helper's settings, so that turning the helper off purges cached files from reverse proxies and CDNs that purge by cache tag.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$event |
\Symfony\Component\HttpKernel\Event\ResponseEvent | |
| The event that contains the HTTP response. |
getSubscribedEvents
{@inheritdoc}
public static getSubscribedEvents(): array
- This method is static.