summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorcatch <6915-catch@users.noreply.drupalcode.org>2025-03-25 09:05:10 +0000
committercatch <6915-catch@users.noreply.drupalcode.org>2025-03-25 09:05:10 +0000
commit17a05cf21b95a8e483614c77a9135901b7fe8fe6 (patch)
tree94cc8ff54011a20fe6c85501fad28c694fba077f
parentbb03ed5ca79b5b57b9d800969aaa0eab55519172 (diff)
downloaddrupal-17a05cf21b95a8e483614c77a9135901b7fe8fe6.tar.gz
drupal-17a05cf21b95a8e483614c77a9135901b7fe8fe6.zip
Issue #3281955 by prudloff: Add a way to debug big_pipe cache tags
-rw-r--r--core/modules/big_pipe/big_pipe.services.yml2
-rw-r--r--core/modules/big_pipe/src/Render/BigPipe.php15
-rw-r--r--core/modules/big_pipe/tests/src/Functional/BigPipeTest.php25
3 files changed, 41 insertions, 1 deletions
diff --git a/core/modules/big_pipe/big_pipe.services.yml b/core/modules/big_pipe/big_pipe.services.yml
index 22d937a3d8ff..49e921d525f5 100644
--- a/core/modules/big_pipe/big_pipe.services.yml
+++ b/core/modules/big_pipe/big_pipe.services.yml
@@ -11,7 +11,7 @@ services:
- { name: placeholder_strategy, priority: 0 }
big_pipe:
class: Drupal\big_pipe\Render\BigPipe
- arguments: ['@renderer', '@session', '@request_stack', '@http_kernel', '@event_dispatcher', '@config.factory', '@messenger', '@router.request_context', '@logger.channel.php']
+ arguments: ['@renderer', '@session', '@request_stack', '@http_kernel', '@event_dispatcher', '@config.factory', '@messenger', '@router.request_context', '@logger.channel.php', '%http.response.debug_cacheability_headers%']
Drupal\big_pipe\Render\BigPipe: '@big_pipe'
html_response.attachments_processor.big_pipe:
public: false
diff --git a/core/modules/big_pipe/src/Render/BigPipe.php b/core/modules/big_pipe/src/Render/BigPipe.php
index cefa77542f3f..1f24c409b76a 100644
--- a/core/modules/big_pipe/src/Render/BigPipe.php
+++ b/core/modules/big_pipe/src/Render/BigPipe.php
@@ -11,6 +11,7 @@ use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Asset\AttachedAssetsInterface;
+use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\EnforcedResponseException;
use Drupal\Core\Messenger\MessengerInterface;
@@ -188,6 +189,7 @@ class BigPipe {
protected MessengerInterface $messenger,
protected RequestContext $requestContext,
protected LoggerInterface $logger,
+ protected bool $debugCacheabilityHeaders = FALSE,
) {
}
@@ -487,6 +489,9 @@ class BigPipe {
// Create a Fiber for each placeholder.
$fibers = [];
+
+ $cacheable_metadata = new CacheableMetadata();
+
foreach ($placeholder_order as $placeholder_id) {
if (!isset($placeholders[$placeholder_id])) {
continue;
@@ -520,6 +525,11 @@ class BigPipe {
}
$elements = $fiber->getReturn();
unset($fibers[$placeholder_id]);
+
+ if ($this->debugCacheabilityHeaders) {
+ $cacheable_metadata->addCacheableDependency(CacheableMetadata::createFromRenderArray($elements));
+ }
+
// Create a new AjaxResponse.
$ajax_response = new AjaxResponse();
// JavaScript's querySelector automatically decodes HTML entities in
@@ -628,6 +638,11 @@ EOF;
$iterations++;
}
+ if ($this->debugCacheabilityHeaders) {
+ $this->sendChunk("\n<!-- big_pipe cache tags: " . implode(' ', $cacheable_metadata->getCacheTags()) . " -->\n");
+ $this->sendChunk("\n<!-- big_pipe cache contexts: " . implode(' ', $cacheable_metadata->getCacheContexts()) . " -->\n");
+ }
+
// Send the stop signal.
$this->sendChunk("\n" . static::STOP_SIGNAL . "\n");
}
diff --git a/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php b/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
index e6e2784fc11e..26e92fc14982 100644
--- a/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
+++ b/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
@@ -556,4 +556,29 @@ class BigPipeTest extends BrowserTestBase {
$this->assertSession()->responseNotContains('<noscript><meta http-equiv="Refresh" content="0; URL=');
}
+ /**
+ * Tests that response contains cacheability debug comments.
+ */
+ public function testDebugCacheability(): void {
+ $this->drupalLogin($this->rootUser);
+ $this->assertSessionCookieExists('1');
+ $this->assertBigPipeNoJsCookieExists('0');
+
+ // With debug_cacheability_headers enabled.
+ $this->drupalGet(Url::fromRoute('<front>'));
+ $this->assertBigPipeResponseHeadersPresent();
+ $this->assertSession()->responseContains('<!-- big_pipe cache tags: -->');
+ $this->assertSession()
+ ->responseContains('<!-- big_pipe cache contexts: languages:language_interface theme user.permissions -->');
+
+ // With debug_cacheability_headers disabled.
+ $this->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
+ $this->rebuildContainer();
+ $this->resetAll();
+ $this->drupalGet(Url::fromRoute('<front>'));
+ $this->assertSession()->responseNotContains('<!-- big_pipe cache tags:');
+ $this->assertSession()
+ ->responseNotContains('<!-- big_pipe cache contexts:');
+ }
+
}