.../src/EventSubscriber/SmartCacheSubscriber.php | 59 ++++++++++------------ .../RequestPolicy/DenyNonHtmlRequests.php | 3 +- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php b/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php index 056ea88..3678a34 100644 --- a/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php +++ b/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php @@ -179,6 +179,13 @@ public function onResponse(FilterResponseEvent $event) { return; } + // There's no work left to be done if this is an uncacheable HTML response. + if ($response->getCacheableMetadata()->getCacheMaxAge() === 0) { + // The HTML response is uncacheable, mark it as such. + $response->headers->set('X-Drupal-SmartCache', 'UNCACHEABLE'); + return; + } + // Don't cache the HTML response if SmartCache's request subscriber did not // fire, because that means it's impossible to have a SmartCache cache hit. // (This can happen when the master request is for example a 403 or 404, in @@ -202,43 +209,33 @@ public function onResponse(FilterResponseEvent $event) { return; } - // Get the cacheability metadata. + // Calculate the cacheability of this HTML response's SmartCache cache item. $html_cacheability = CacheableMetadata::createFromObject($response->getCacheableMetadata()) ->addCacheContexts(['route']) ->addCacheTags(['rendered']); - - // SmartCache only caches cacheable HTML responses, i.e. with a max-age > 0. - if ($html_cacheability->getCacheMaxAge() !== 0) { - $this->normalizeCacheability($html_cacheability); - - // Get the contexts by which the current route's response must be varied. - $stored_cache_contexts = $request->attributes->get(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE); - - // If the set of cache contexts is different, store the union of the - // already stored cache contexts and the contexts for this request. - if ($html_cacheability->getCacheContexts() !== $stored_cache_contexts) { - if (is_array($stored_cache_contexts)) { - $html_cacheability->addCacheContexts($stored_cache_contexts); - $this->normalizeCacheability($html_cacheability); - } - $this->smartContextsCache->set($request->attributes->get(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE_CID), $html_cacheability->getCacheContexts()); + $this->normalizeCacheability($html_cacheability); + + // If the set of cache contexts of this HTML response is different from the + // already stored cache contexts, store the union of both. + $stored_cache_contexts = $request->attributes->get(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE); + if ($html_cacheability->getCacheContexts() !== $stored_cache_contexts) { + if (is_array($stored_cache_contexts)) { + $html_cacheability->addCacheContexts($stored_cache_contexts); + $this->normalizeCacheability($html_cacheability); } + $this->smartContextsCache->set($request->attributes->get(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE_CID), $html_cacheability->getCacheContexts()); + } - // Finally, cache the HTML response by those contexts. - $context_cache_keys = $this->cacheContextsManager->convertTokensToKeys($html_cacheability->getCacheContexts()); - $cid = implode(':', $context_cache_keys->getKeys()); - $html_cacheability = $html_cacheability->merge($context_cache_keys); - $expire = ($html_cacheability->getCacheMaxAge() === Cache::PERMANENT) ? Cache::PERMANENT : (int) $request->server->get('REQUEST_TIME') + $html_cacheability->getCacheMaxAge(); - $this->smartHtmlCache->set($cid, $response, $expire, $html_cacheability->getCacheTags()); + // Finally, cache the HTML response by those contexts. + $context_cache_keys = $this->cacheContextsManager->convertTokensToKeys($html_cacheability->getCacheContexts()); + $cid = implode(':', $context_cache_keys->getKeys()); + $html_cacheability = $html_cacheability->merge($context_cache_keys); + $expire = ($html_cacheability->getCacheMaxAge() === Cache::PERMANENT) ? Cache::PERMANENT : (int) $request->server->get('REQUEST_TIME') + $html_cacheability->getCacheMaxAge(); + $this->smartHtmlCache->set($cid, $response, $expire, $html_cacheability->getCacheTags()); - // The HTML response was generated, mark the response as a cache miss. The - // next time, it will be a cache hit. - $response->headers->set('X-Drupal-SmartCache', 'MISS'); - } - else { - // The HTML response is uncacheable, mark it as such. - $response->headers->set('X-Drupal-SmartCache', 'UNCACHEABLE'); - } + // The HTML response was generated, mark the response as a cache miss. The + // next time, it will be a cache hit. + $response->headers->set('X-Drupal-SmartCache', 'MISS'); } /** diff --git a/core/modules/smart_cache/src/PageCache/RequestPolicy/DenyNonHtmlRequests.php b/core/modules/smart_cache/src/PageCache/RequestPolicy/DenyNonHtmlRequests.php index dfc5445..7a9e7a4 100644 --- a/core/modules/smart_cache/src/PageCache/RequestPolicy/DenyNonHtmlRequests.php +++ b/core/modules/smart_cache/src/PageCache/RequestPolicy/DenyNonHtmlRequests.php @@ -15,7 +15,8 @@ * Deny non-HTML requests. * * The policy denies caching if the request has a request format other than HTML - * or when that is HTML, but additionally a wrapper format is specified. + * or when that is HTML, but additionally a wrapper format is specified. For + * example, 'drupal_ajax', 'drupal_modal' are wrapper formats. * * @see \Drupal\smart_cache\EventSubscriber\SmartCacheSubscriber */