Skip to content
Snippets Groups Projects

Issue #2916705: Page cache isn't invalidated

1 unresolved thread
Files
2
@@ -7,6 +7,8 @@ namespace Drupal\cache_control_override\EventSubscriber;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Utility\Error;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -28,9 +30,12 @@ final class CacheControlOverrideSubscriber implements EventSubscriberInterface {
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerChannelFactory
* The logger channel factory.
*/
public function __construct(
protected ConfigFactoryInterface $configFactory,
protected LoggerChannelFactoryInterface $loggerChannelFactory,
) {
}
@@ -82,7 +87,19 @@ final class CacheControlOverrideSubscriber implements EventSubscriberInterface {
$maxAge = min($maximum, $maxAge);
}
}
$response->headers->set('Cache-Control', 'public, max-age=' . $maxAge);
try {
// This is needed because page_cache uses this header to invalidate
// the page cache.
$response->setExpires(new \DateTime('+' . $maxAge . ' seconds'));
}
catch (\Exception $exception) {
// Could not set Expires header because max-age is invalid.
Please register or sign in to reply
$logger = $this->loggerChannelFactory->get('cache_control_override');
Error::logException($logger, $exception);
}
}
}
Loading