Unverified Commit eb51f3b6 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3554414 by catch, berdir: User #placeholder_strategy_denylist for...

Issue #3554414 by catch, berdir: User #placeholder_strategy_denylist for CacheOptionalInterface blocks to prevent them being rendered by BigPipe

(cherry picked from commit 6209c292)
parent 74f8df5f
Loading
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\block\Entity\Block;
use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
use Drupal\Core\Security\TrustedCallbackInterface;

/**
@@ -75,6 +76,16 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod
      if (!$plugin instanceof CacheOptionalInterface) {
        $build[$entity_id]['#cache']['keys'] = ['entity_view', 'block', $entity->id()];
      }
      else {
        // When a block implements CacheOptionalInterface, it will be excluded
        // from the dynamic render cache. Since it is also cheap to render,
        // prevent it being placeholdered by BigPipe. This avoids loading
        // BigPipe's JavaScript if this block is the only placeholder on the
        // page, which is likely to be the case on dynamic page cache hits.
        $build[$entity_id]['#placeholder_strategy_denylist'] = [
          BigPipeStrategy::class => TRUE,
        ];
      }

      // Allow altering of cacheability metadata or setting #create_placeholder.
      $this->moduleHandler->alter(['block_build', "block_build_" . $plugin->getBaseId()], $build[$entity_id], $plugin);
+5 −6
Original line number Diff line number Diff line
@@ -89,16 +89,15 @@ public function testLanguageBlock(): void {
    $this->doTestLanguageBlockAnonymous($block->label());
    $this->doTestLanguageBlock404($block->label(), 'system/404');

    // Test 404s with big_pipe where the behavior is different for logged-in
    // users.
    // Confirm that enabling big_pipe doesn't change the behavior of the block.
    // Note that this is the case because the language switcher block deny lists
    // the big_pipe placeholder strategy, otherwise it would be subject to the
    // bug described in https://www.drupal.org/project/drupal/issues/3349201
    \Drupal::service('module_installer')->install(['big_pipe']);
    $this->rebuildAll();
    $this->doTestLanguageBlock404($block->label(), 'system/404');
    $this->drupalLogin($this->drupalCreateUser());
    // @todo This is testing the current behavior with the big_pipe module
    //   enabled. This behavior is a bug will be fixed in
    //   https://www.drupal.org/project/drupal/issues/3349201.
    $this->doTestLanguageBlock404($block->label(), '<front>');
    $this->doTestLanguageBlock404($block->label(), 'system/404');
  }

  /**
+1 −1
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ protected function doTestFrontPageAuthenticatedWarmCache(): void {
      'CacheTagInvalidationCount' => 0,
      'CacheTagLookupQueryCount' => 5,
      'ScriptCount' => 1,
      'ScriptBytes' => 73031,
      'ScriptBytes' => 13150,
      'StylesheetCount' => 2,
      'StylesheetBytes' => 39163,
    ];