core/core.services.yml | 6 +++ .../EventSubscriber/FinishResponseSubscriber.php | 28 +----------- .../PageCache/ResponsePolicy/DenyNoCacheRoutes.php | 50 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 7c48b9c..42d66e2 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -210,6 +210,12 @@ services: class: Drupal\Core\PageCache\ResponsePolicy\KillSwitch tags: - { name: page_cache_response_policy } + page_cache_no_cache_routes: + class: Drupal\Core\PageCache\ResponsePolicy\DenyNoCacheRoutes + arguments: ['@current_route_match'] + public: false + tags: + - { name: page_cache_response_policy } page_cache_no_server_error: class: Drupal\Core\PageCache\ResponsePolicy\NoServerError public: false diff --git a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php index 8370ae1..48ee4fd 100644 --- a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php @@ -15,10 +15,8 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\PageCache\RequestPolicyInterface; -use Drupal\Core\PageCache\ResponsePolicy\KillSwitch; use Drupal\Core\PageCache\ResponsePolicyInterface; use Drupal\Core\Routing\AccessAwareRouterInterface; -use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Site\Settings; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Request; @@ -70,20 +68,6 @@ class FinishResponseSubscriber implements EventSubscriberInterface { protected $cacheContexts; /** - * The route match. - * - * @var \Drupal\Core\Routing\RouteMatchInterface - */ - protected $routeMatch; - - /** - * The kill switch. - * - * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch - */ - protected $killSwitch; - - /** * Constructs a FinishResponseSubscriber object. * * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager @@ -96,19 +80,14 @@ class FinishResponseSubscriber implements EventSubscriberInterface { * A policy rule determining the cacheability of a response. * @param \Drupal\Core\Cache\CacheContexts $cache_contexts * The cache contexts - * @param \Drupal\Core\Routing\RouteMatchInterface $route_match - * The route match. - * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $kill_switch - * The kill switch. */ - public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContexts $cache_contexts, RouteMatchInterface $route_match, KillSwitch $kill_switch) { + public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContexts $cache_contexts) { $this->languageManager = $language_manager; $this->config = $config_factory->get('system.performance'); $this->requestPolicy = $request_policy; $this->responsePolicy = $response_policy; $this->cacheContexts = $cache_contexts; $this->routeMatch = $route_match; - $this->killSwitch = $kill_switch; } /** @@ -125,11 +104,6 @@ public function onRespond(FilterResponseEvent $event) { $request = $event->getRequest(); $response = $event->getResponse(); - // Mark the response as not cacheable if the route was configured like that. - if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('no_cache')) { - $this->killSwitch->trigger(); - } - // Set the X-UA-Compatible HTTP header to force IE to use the most recent // rendering engine. $response->headers->set('X-UA-Compatible', 'IE=edge', FALSE); diff --git a/core/lib/Drupal/Core/PageCache/ResponsePolicy/DenyNoCacheRoutes.php b/core/lib/Drupal/Core/PageCache/ResponsePolicy/DenyNoCacheRoutes.php new file mode 100644 index 0000000..cbe4dc7 --- /dev/null +++ b/core/lib/Drupal/Core/PageCache/ResponsePolicy/DenyNoCacheRoutes.php @@ -0,0 +1,50 @@ +routeMatch = $route_match; + } + + /** + * {@inheritdoc} + */ + public function check(Response $response, Request $request) { + if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('no_cache')) { + return static::DENY; + } + } + +} +