You are here

public function HtmlResponseBigPipeSubscriber::onRespond in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/big_pipe/src/EventSubscriber/HtmlResponseBigPipeSubscriber.php \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond()

Transforms a HtmlResponse to a BigPipeResponse.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The event to process.

File

core/modules/big_pipe/src/EventSubscriber/HtmlResponseBigPipeSubscriber.php, line 69

Class

HtmlResponseBigPipeSubscriber
Response subscriber to replace the HtmlResponse with a BigPipeResponse.

Namespace

Drupal\big_pipe\EventSubscriber

Code

public function onRespond(FilterResponseEvent $event) {
  $response = $event
    ->getResponse();
  if (!$response instanceof HtmlResponse) {
    return;
  }
  $attachments = $response
    ->getAttachments();

  // If there are no no-JS BigPipe placeholders, unwrap the scripts_bottom
  // markup.
  // @see onRespondEarly()
  // @see \Drupal\big_pipe\Render\BigPipe::sendPreBody()
  if (empty($attachments['big_pipe_nojs_placeholders'])) {
    $content = $response
      ->getContent();
    $content = str_replace('<drupal-big-pipe-scripts-bottom-marker>', '', $content);
    $response
      ->setContent($content);
  }

  // If there are neither BigPipe placeholders nor no-JS BigPipe placeholders,
  // there isn't anything dynamic in this response, and we can return early:
  // there is no point in sending this response using BigPipe.
  if (empty($attachments['big_pipe_placeholders']) && empty($attachments['big_pipe_nojs_placeholders'])) {
    return;
  }
  $big_pipe_response = new BigPipeResponse($response);
  $big_pipe_response
    ->setBigPipeService($this
    ->getBigPipeService($event));
  $event
    ->setResponse($big_pipe_response);
}