You are here

class BigPipeSessionlessStrategy in Sessionless BigPipe 8

Same name and namespace in other branches
  1. 2.x src/Render/Placeholder/BigPipeSessionlessStrategy.php \Drupal\big_pipe_sessionless\Render\Placeholder\BigPipeSessionlessStrategy

Defines the BigPipe sessionless placeholder strategy, to send HTML in chunks.

To avoid a potential no-JS redirect, no-session requests always use no-JS BigPipe placeholders.

This is reusing almost everything of BigPipeStrategy. The only differences: 1. In ::processPlaceholders(), we invert the session-related logic: instead of ignoring requests without a session, we ignore requests with a session. We also only act on GET requests, because Symfony sets response's content to NULL on HEAD requests. 2. ::doProcessPlaceholders() is made a lot simpler: since the goal is to accelerate Page Cache misses (and to hence cause Page Cache hits for subsequent requests), we can only use no-JS BigPipe placeholders. Otherwise we would not be able to create a HTML response to store in Page Cache. 3. This placeholder strategy service has a priority that is LOWER than that of BigPipeStrategy. This ensures that BigPipeStrategy runs first. But it ignores sessionless requests, and since this handles only sessionless requests, they each have their own clear responsibility.

Hierarchy

Expanded class hierarchy of BigPipeSessionlessStrategy

See also

\Drupal\big_pipe\Render\Placeholder\BigPipeStrategy

placeholder_strategy.big_pipe_sessionless

big_pipe_sessionless.services.yml

1 file declares its use of BigPipeSessionlessStrategy
BigPipeSessionlessStrategyTest.php in tests/src/Unit/Render/Placeholder/BigPipeSessionlessStrategyTest.php
1 string reference to 'BigPipeSessionlessStrategy'
big_pipe_sessionless.services.yml in ./big_pipe_sessionless.services.yml
big_pipe_sessionless.services.yml
1 service uses BigPipeSessionlessStrategy
placeholder_strategy.big_pipe_sessionless in ./big_pipe_sessionless.services.yml
Drupal\big_pipe_sessionless\Render\Placeholder\BigPipeSessionlessStrategy

File

src/Render/Placeholder/BigPipeSessionlessStrategy.php, line 33

Namespace

Drupal\big_pipe_sessionless\Render\Placeholder
View source
class BigPipeSessionlessStrategy extends BigPipeStrategy {

  /**
   * {@inheritdoc}
   */
  public function processPlaceholders(array $placeholders) {
    $request = $this->requestStack
      ->getCurrentRequest();

    // Sessionless BigPipe only acts on GET requests. It cannot act on HEAD
    // requests, because \Symfony\Component\HttpFoundation\Response::prepare()
    // sets a response's content to NULL for HEAD requests, which means
    // \Drupal\big_pipe_sessionless\Render\BigPipeSessionless::sendContent() has
    // no content to prime the Page Cache with.
    if (!$request
      ->isMethod('GET')) {
      return [];
    }

    // Routes can opt out from using the BigPipe HTML delivery technique.
    if ($this->routeMatch
      ->getRouteObject()
      ->getOption('_no_big_pipe')) {
      return [];
    }

    // @NOTE: We do exactly the opposite of parent::processPlaceholders().
    // everything else in this method is identical to the parent method.
    if ($this->sessionConfiguration
      ->hasSession($request)) {
      return [];
    }
    return $this
      ->doProcessPlaceholders($placeholders);
  }

  /**
   * Transforms placeholders to BigPipe placeholders, only no-JS.
   *
   * Only no-JS placeholders to allow BigPipe to accelerate Page Cache misses.
   *
   * @param array $placeholders
   *   The placeholders to process.
   *
   * @return array
   *   The BigPipe placeholders.
   */
  protected function doProcessPlaceholders(array $placeholders) {
    $overridden_placeholders = [];
    foreach ($placeholders as $placeholder => $placeholder_elements) {
      $overridden_placeholders[$placeholder] = static::createBigPipeNoJsPlaceholder($placeholder, $placeholder_elements, static::placeholderIsAttributeSafe($placeholder));
    }
    return $overridden_placeholders;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BigPipeSessionlessStrategy::doProcessPlaceholders protected function Transforms placeholders to BigPipe placeholders, only no-JS. Overrides BigPipeStrategy::doProcessPlaceholders
BigPipeSessionlessStrategy::processPlaceholders public function Processes placeholders to render them with different strategies. Overrides BigPipeStrategy::processPlaceholders
BigPipeStrategy::$requestStack protected property The request stack.
BigPipeStrategy::$routeMatch protected property The current route match.
BigPipeStrategy::$sessionConfiguration protected property The session configuration.
BigPipeStrategy::createBigPipeJsPlaceholder protected static function Creates a BigPipe JS placeholder.
BigPipeStrategy::createBigPipeNoJsPlaceholder protected static function Creates a BigPipe no-JS placeholder.
BigPipeStrategy::generateBigPipePlaceholderId protected static function Generates a BigPipe placeholder ID.
BigPipeStrategy::NOJS_COOKIE constant BigPipe no-JS cookie name.
BigPipeStrategy::placeholderIsAttributeSafe protected static function Determines whether the given placeholder is attribute-safe or not.
BigPipeStrategy::__construct public function Constructs a new BigPipeStrategy class.