isMainRequest()) { return; } $response = $event->getResponse(); if (!$response instanceof CacheableResponseInterface) { return; } if ($response->isClientError()) { $http_4xx_response_cacheability = new CacheableMetadata(); $http_4xx_response_cacheability->setCacheTags(['4xx-response']); $response->addCacheableDependency($http_4xx_response_cacheability); } } /** * {@inheritdoc} */ public static function getSubscribedEvents(): array { // Priority 10, so that it runs before FinishResponseSubscriber, which will // expose the cacheability metadata in the form of headers. $events[KernelEvents::RESPONSE][] = ['onRespond', 10]; return $events; } }