function DynamicPageCacheSubscriber::onResponse
Same name in other branches
- 9 core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::onResponse()
- 8.9.x core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::onResponse()
- 11.x core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::onResponse()
Stores a response in case of a Dynamic Page Cache miss, if cacheable.
Parameters
\Symfony\Component\HttpKernel\Event\ResponseEvent $event: The event to process.
File
-
core/
modules/ dynamic_page_cache/ src/ EventSubscriber/ DynamicPageCacheSubscriber.php, line 156
Class
- DynamicPageCacheSubscriber
- Returns cached responses as early and avoiding as much work as possible.
Namespace
Drupal\dynamic_page_cache\EventSubscriberCode
public function onResponse(ResponseEvent $event) {
$response = $event->getResponse();
// Dynamic Page Cache only works with cacheable responses. It does not work
// with plain Response objects. (Dynamic Page Cache needs to be able to
// access and modify the cacheability metadata associated with the
// response.)
if (!$response instanceof CacheableResponseInterface) {
return;
}
// There's no work left to be done if this is a Dynamic Page Cache hit.
if ($response->headers
->get(self::HEADER) === 'HIT') {
return;
}
// There's no work left to be done if this is an uncacheable response.
if (!$this->shouldCacheResponse($response)) {
// The response is uncacheable, mark it as such.
$response->headers
->set(self::HEADER, 'UNCACHEABLE');
return;
}
// Don't cache the response if Dynamic Page Cache's request subscriber did
// not fire, because that means it is impossible to have a Dynamic Page
// Cache hit. This can happen when the master request is for example a 403
// or 404, in which case a subrequest is performed by the router. In that
// case, it is the subrequest's response that is cached by Dynamic Page
// Cache, because the routing happens in a request subscriber earlier than
// Dynamic Page Cache's and immediately sets a response, i.e. the one
// returned by the subrequest, and thus causes Dynamic Page Cache's request
// subscriber to not fire for the master request.
// @see \Drupal\Core\Routing\AccessAwareRouter::checkAccess()
// @see \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber::on403()
$request = $event->getRequest();
if (!isset($this->requestPolicyResults[$request])) {
return;
}
// Don't cache the response if the Dynamic Page Cache request & response
// policies are not met.
// @see onRequest()
if ($this->requestPolicyResults[$request] === RequestPolicyInterface::DENY || $this->responsePolicy
->check($response, $request) === ResponsePolicyInterface::DENY) {
return;
}
$cacheable_metadata = CacheableMetadata::createFromObject($response->getCacheableMetadata());
$this->cache
->set([
'response',
], $response, $cacheable_metadata->addCacheContexts($this->cacheContexts), (new CacheableMetadata())->setCacheContexts($this->cacheContexts));
// The response was generated, mark the response as a cache miss. The next
// time, it will be a cache hit.
$response->headers
->set(self::HEADER, 'MISS');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.