You are here

protected function BigPipeSessionless::primePageCache in Sessionless BigPipe 8

Same name and namespace in other branches
  1. 2.x src/Render/BigPipeSessionless.php \Drupal\big_pipe_sessionless\Render\BigPipeSessionless::primePageCache()

Primes the Page Cache based on the streamed response.

Parameters

\Drupal\big_pipe\Render\BigPipeResponse $response: The BigPipe response that was sent.

1 call to BigPipeSessionless::primePageCache()
BigPipeSessionless::sendContent in src/Render/BigPipeSessionless.php
Sends an HTML response in chunks using the BigPipe technique.

File

src/Render/BigPipeSessionless.php, line 97

Class

BigPipeSessionless
The sessionless BigPipe service.

Namespace

Drupal\big_pipe_sessionless\Render

Code

protected function primePageCache(BigPipeResponse $response) {

  // Start with the original HTML response, so we have the appropriate meta-
  // data like headers, HTTP version, and so on.
  $streamed_response = $response
    ->getOriginalHtmlResponse();

  // Override content with final HTML content (with replaced placeholders).
  $streamed_response
    ->setContent($this->finalHtmlResponse
    ->getContent());

  // Add any additional cacheability metadata for rendered placeholders.
  $streamed_response
    ->addCacheableDependency($this->finalHtmlResponse
    ->getCacheableMetadata());

  // Overwrite with final attachments (overwrite, not add, because attachments
  // need to be processed, and once the response is sent, they are processed;
  // if we would not overwrite, then we'd reprocess them).
  // @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface
  $streamed_response
    ->setAttachments($this->finalHtmlResponse
    ->getAttachments());

  // Dispatch the KernelEvents::RESPONSE event, to let those event subscribers
  // do what they need to do. This is f.e. necessary for the cache tags header
  // for debugging purposes and for modules that integrate with reverse
  // proxies that support cache tags.
  $fake_request = $this->requestStack
    ->getMasterRequest()
    ->duplicate();
  $streamed_response = $this
    ->filterResponse($fake_request, HttpKernelInterface::MASTER_REQUEST, $streamed_response);

  // Prime Page Cache.
  // @see \Drupal\big_pipe_sessionless\StackMiddleware\BigPipeSessionlessPageCache
  $this->pageCacheMiddleware
    ->_storeResponse($this->requestStack
    ->getCurrentRequest(), $streamed_response);
}