core/core.services.yml | 7 ++++- .../Core/EventSubscriber/SmartCacheSubscriber.php | 32 +-------------------- .../RequestPolicy/DenyNonHtmlRequests.php | 33 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index cb72fd4..a8edbe4 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -231,6 +231,11 @@ services: public: false tags: - { name: smart_cache_response_policy } + smart_cache_non_html_requests: + class: Drupal\Core\PageCache\RequestPolicy\DenyNonHtmlRequests + public: false + tags: + - { name: smart_cache_request_policy } config.manager: class: Drupal\Core\Config\ConfigManager arguments: ['@entity.manager', '@config.factory', '@config.typed', '@string_translation', '@config.storage', '@event_dispatcher'] @@ -951,7 +956,7 @@ services: lazy: true smart_cache_subscriber: class: Drupal\Core\EventSubscriber\SmartCacheSubscriber - arguments: ['@current_route_match', '@cache_contexts_manager', '@cache.smart_cache_contexts', '@cache.smart_cache_html', '@smart_cache_request_policy', '@smart_cache_response_policy', '%renderer.config%'] + arguments: ['@cache_contexts_manager', '@cache.smart_cache_contexts', '@cache.smart_cache_html', '@smart_cache_request_policy', '@smart_cache_response_policy'] tags: - { name: event_subscriber } diff --git a/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php index 6c1534d..e186aa7 100644 --- a/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/SmartCacheSubscriber.php @@ -15,7 +15,6 @@ use Drupal\Core\PageCache\ResponsePolicyInterface; use Drupal\Core\Render\Element; use Drupal\Core\Render\HtmlResponse; -use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent; @@ -29,13 +28,6 @@ class SmartCacheSubscriber implements EventSubscriberInterface { /** - * The current route match. - * - * @var \Drupal\Core\Routing\RouteMatchInterface - */ - protected $routeMatch; - - /** * The cache contexts manager. * * @var \Drupal\Core\Cache\Context\CacheContextsManager @@ -71,17 +63,8 @@ class SmartCacheSubscriber implements EventSubscriberInterface { protected $responsePolicy; /** - * The renderer configuration array. - * - * @var array - */ - protected $rendererConfig; - - /** * Constructs a new SmartCacheSubscriber object. * - * @param \Drupal\Core\Routing\RouteMatchInterface $route_match - * The current route match. * @param \Drupal\Core\Cache\Context\CacheContextsManager $cache_contexts_manager * The cache contexts service. * @param \Drupal\Core\Cache\CacheBackendInterface $contexts_cache @@ -92,17 +75,13 @@ class SmartCacheSubscriber implements EventSubscriberInterface { * A policy rule determining the cacheability of a request. * @param \Drupal\Core\PageCache\ResponsePolicyInterface $response_policy * A policy rule determining the cacheability of the response. - * @param array $renderer_config - * The renderer configuration array. */ - public function __construct(RouteMatchInterface $route_match, CacheContextsManager $cache_contexts_manager, CacheBackendInterface $contexts_cache, CacheBackendInterface $html_cache, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, array $renderer_config) { - $this->routeMatch = $route_match; + public function __construct(CacheContextsManager $cache_contexts_manager, CacheBackendInterface $contexts_cache, CacheBackendInterface $html_cache, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy) { $this->cacheContextsManager = $cache_contexts_manager; $this->smartContextsCache = $contexts_cache; $this->smartHtmlCache = $html_cache; $this->requestPolicy = $request_policy; $this->responsePolicy = $response_policy; - $this->rendererConfig = $renderer_config; } /** @@ -112,12 +91,6 @@ public function __construct(RouteMatchInterface $route_match, CacheContextsManag * The event to process. */ public function onRouteMatch(GetResponseEvent $event) { - // SmartCache only supports master requests that are safe, ask for HTML, and - // don't specify a HTML wrapper format. - if (!$event->isMasterRequest() || !$event->getRequest()->isMethodSafe() || $event->getRequest()->getRequestFormat() !== 'html' || $event->getRequest()->query->has(MainContentViewSubscriber::WRAPPER_FORMAT)) { - return; - } - // Don't cache the HTML response if the SmartCache request policies are not // met. if ($this->requestPolicy->check($event->getRequest()) === RequestPolicyInterface::DENY) { @@ -175,10 +148,7 @@ public function onResponse(FilterResponseEvent $event) { // Get the cacheability metadata. $html_cacheability = CacheableMetadata::createFromObject($response->getCacheableMetadata()) - // SmartCache caches per route. ->addCacheContexts(['route']) - // SmartCache also respects the Renderer's required cache contexts. - ->addCacheContexts($this->rendererConfig['required_cache_contexts']) ->addCacheTags(['rendered']); // @todo DEBUG DEBUG DEBUG PROFILING PROFILING PROFILING — Until only the diff --git a/core/lib/Drupal/Core/PageCache/RequestPolicy/DenyNonHtmlRequests.php b/core/lib/Drupal/Core/PageCache/RequestPolicy/DenyNonHtmlRequests.php new file mode 100644 index 0000000..3880cf1 --- /dev/null +++ b/core/lib/Drupal/Core/PageCache/RequestPolicy/DenyNonHtmlRequests.php @@ -0,0 +1,33 @@ +getRequestFormat() !== 'html' || $request->query->has(MainContentViewSubscriber::WRAPPER_FORMAT)) { + return static::DENY; + } + } + +}