You are here

public function BigPipe::sendContent in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/big_pipe/src/Render/BigPipe.php \Drupal\big_pipe\Render\BigPipe::sendContent()
  2. 9 core/modules/big_pipe/src/Render/BigPipe.php \Drupal\big_pipe\Render\BigPipe::sendContent()

Sends an HTML response in chunks using the BigPipe technique.

@internal This method should only be invoked by \Drupal\big_pipe\Render\BigPipeResponse, which is itself an internal class.

Parameters

\Drupal\big_pipe\Render\BigPipeResponse $response: The BigPipe response to send.

File

core/modules/big_pipe/src/Render/BigPipe.php, line 280

Class

BigPipe
Service for sending an HTML response in chunks (to get faster page loads).

Namespace

Drupal\big_pipe\Render

Code

public function sendContent(BigPipeResponse $response) {
  $content = $response
    ->getContent();
  $attachments = $response
    ->getAttachments();

  // First, gather the BigPipe placeholders that must be replaced.
  $placeholders = $attachments['big_pipe_placeholders'] ?? [];
  $nojs_placeholders = $attachments['big_pipe_nojs_placeholders'] ?? [];

  // BigPipe sends responses using "Transfer-Encoding: chunked". To avoid
  // sending already-sent assets, it is necessary to track cumulative assets
  // from all previously rendered/sent chunks.
  // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41
  $cumulative_assets = AttachedAssets::createFromRenderArray([
    '#attached' => $attachments,
  ]);
  $cumulative_assets
    ->setAlreadyLoadedLibraries($attachments['library']);
  $this
    ->performPreSendTasks();

  // Find the closing </body> tag and get the strings before and after. But be
  // careful to use the latest occurrence of the string "</body>", to ensure
  // that strings in inline JavaScript or CDATA sections aren't used instead.
  $parts = explode('</body>', $content);
  $post_body = array_pop($parts);
  $pre_body = implode('', $parts);
  $this
    ->sendPreBody($pre_body, $nojs_placeholders, $cumulative_assets);
  $this
    ->sendPlaceholders($placeholders, $this
    ->getPlaceholderOrder($pre_body, $placeholders), $cumulative_assets);
  $this
    ->sendPostBody($post_body);
  $this
    ->performPostSendTasks();
}