Skip to content
Snippets Groups Projects
Unverified Commit 488870ef authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3089957 by dww, Krzysztof Domański, Wim Leers, mr.baileys: Set...

Issue #3089957 by dww, Krzysztof Domański, Wim Leers, mr.baileys: Set X-Drupal-Cache-Max-Age to aid in debugging #cache fun
parent 72cb9b6f
No related branches found
No related tags found
No related merge requests found
......@@ -118,7 +118,8 @@ parameters:
# Cacheability debugging:
#
# Responses with cacheability metadata (CacheableResponseInterface instances)
# get X-Drupal-Cache-Tags and X-Drupal-Cache-Contexts headers.
# get X-Drupal-Cache-Tags, X-Drupal-Cache-Contexts and X-Drupal-Cache-Max-Age
# headers.
#
# For more information about debugging cacheable responses, see
# https://www.drupal.org/developing/api/8/response/cacheable-response-interface
......
......@@ -156,6 +156,14 @@ public function onRespond(ResponseEvent $event) {
$response_cacheability = $response->getCacheableMetadata();
$response->headers->set('X-Drupal-Cache-Tags', implode(' ', $response_cacheability->getCacheTags()));
$response->headers->set('X-Drupal-Cache-Contexts', implode(' ', $this->cacheContextsManager->optimizeTokens($response_cacheability->getCacheContexts())));
$max_age_message = $response_cacheability->getCacheMaxAge();
if ($max_age_message === 0) {
$max_age_message = '0 (Uncacheable)';
}
elseif ($max_age_message === -1) {
$max_age_message = '-1 (Permanent)';
}
$response->headers->set('X-Drupal-Cache-Max-Age', $max_age_message);
}
$is_cacheable = ($this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) && ($this->responsePolicy->check($response, $request) !== ResponsePolicyInterface::DENY);
......
......@@ -54,6 +54,7 @@ public function testFinishResponseSubscriber() {
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], [implode(' ', $expected_cache_contexts)]);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['config:user.role.anonymous http_response rendered']);
$this->assertEqual($headers['X-Drupal-Cache-Max-Age'], ['-1 (Permanent)']);
// Confirm that the page wrapping is being added, so we're not getting a
// raw body returned.
$this->assertRaw('</html>', 'Page markup was found.');
......@@ -68,6 +69,7 @@ public function testFinishResponseSubscriber() {
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], [implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url']))]);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['config:user.role.anonymous foo http_response rendered']);
$this->assertEqual($headers['X-Drupal-Cache-Max-Age'], ['60']);
// 2. controller result: render array, per-role cacheable route access.
$this->drupalGet('router_test/test19');
$headers = $session->getResponseHeaders();
......@@ -78,11 +80,13 @@ public function testFinishResponseSubscriber() {
$headers = $session->getResponseHeaders();
$this->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
$this->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
// 4. controller result: Response object, per-role cacheable route access.
$this->drupalGet('router_test/test20');
$headers = $session->getResponseHeaders();
$this->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
$this->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
// 5. controller result: CacheableResponse object, globally cacheable route access.
$this->drupalGet('router_test/test21');
$headers = $session->getResponseHeaders();
......@@ -100,6 +104,7 @@ public function testFinishResponseSubscriber() {
$headers = $session->getResponseHeaders();
$this->assertTrue(isset($headers['X-Drupal-Cache-Contexts']));
$this->assertTrue(isset($headers['X-Drupal-Cache-Tags']));
$this->assertTrue(isset($headers['X-Drupal-Cache-Max-Age']));
$this->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
$this->rebuildContainer();
$this->resetAll();
......@@ -107,6 +112,7 @@ public function testFinishResponseSubscriber() {
$headers = $session->getResponseHeaders();
$this->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
$this->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
}
/**
......
......@@ -118,7 +118,8 @@ parameters:
# Cacheability debugging:
#
# Responses with cacheability metadata (CacheableResponseInterface instances)
# get X-Drupal-Cache-Tags and X-Drupal-Cache-Contexts headers.
# get X-Drupal-Cache-Tags, X-Drupal-Cache-Contexts and X-Drupal-Cache-Max-Age
# headers.
#
# For more information about debugging cacheable responses, see
# https://www.drupal.org/developing/api/8/response/cacheable-response-interface
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment