3 namespace Drupal\big_pipe\Render;
5 use Drupal\Core\Render\HtmlResponse;
8 * A response that is sent in chunks by the BigPipe service.
10 * Note we cannot use \Symfony\Component\HttpFoundation\StreamedResponse because
11 * it makes the content inaccessible (hidden behind a callback), which means no
12 * middlewares are able to modify the content anymore.
14 * @see \Drupal\big_pipe\Render\BigPipe
17 * This is a temporary solution until a generic response emitter interface is
18 * created in https://www.drupal.org/node/2577631. Only code internal to
19 * BigPipe should instantiate or type hint to this class.
21 class BigPipeResponse extends HtmlResponse {
24 * The BigPipe service.
26 * @var \Drupal\big_pipe\Render\BigPipe
31 * The original HTML response.
33 * Still contains placeholders. Its cacheability metadata and attachments are
34 * for everything except the placeholders (since those are not yet rendered).
36 * @see \Drupal\Core\Render\StreamedResponseInterface
37 * @see ::getStreamedResponse()
39 * @var \Drupal\Core\Render\HtmlResponse
41 protected $originalHtmlResponse;
44 * Constructs a new BigPipeResponse.
46 * @param \Drupal\Core\Render\HtmlResponse $response
47 * The original HTML response.
49 public function __construct(HtmlResponse $response) {
50 parent::__construct('', $response->getStatusCode(), []);
52 $this->originalHtmlResponse = $response;
54 $this->populateBasedOnOriginalHtmlResponse();
58 * Returns the original HTML response.
60 * @return \Drupal\Core\Render\HtmlResponse
61 * The original HTML response.
63 public function getOriginalHtmlResponse() {
64 return $this->originalHtmlResponse;
68 * Populates this BigPipeResponse object based on the original HTML response.
70 protected function populateBasedOnOriginalHtmlResponse() {
71 // Clone the HtmlResponse's data into the new BigPipeResponse.
72 $this->headers = clone $this->originalHtmlResponse->headers;
74 ->setStatusCode($this->originalHtmlResponse->getStatusCode())
75 ->setContent($this->originalHtmlResponse->getContent())
76 ->setAttachments($this->originalHtmlResponse->getAttachments())
77 ->addCacheableDependency($this->originalHtmlResponse->getCacheableMetadata());
79 // A BigPipe response can never be cached, because it is intended for a
81 // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1
84 // Inform surrogates how they should handle BigPipe responses:
85 // - "no-store" specifies that the response should not be stored in cache;
86 // it is only to be used for the original request
87 // - "content" identifies what processing surrogates should perform on the
88 // response before forwarding it. We send, "BigPipe/1.0", which surrogates
89 // should not process at all, and in fact, they should not even buffer it
91 // @see http://www.w3.org/TR/edge-arch/
92 $this->headers->set('Surrogate-Control', 'no-store, content="BigPipe/1.0"');
94 // Add header to support streaming on NGINX + php-fpm (nginx >= 1.5.6).
95 $this->headers->set('X-Accel-Buffering', 'no');
99 * Sets the BigPipe service to use.
101 * @param \Drupal\big_pipe\Render\BigPipe $big_pipe
102 * The BigPipe service.
104 public function setBigPipeService(BigPipe $big_pipe) {
105 $this->bigPipe = $big_pipe;
111 public function sendContent() {
112 $this->bigPipe->sendContent($this);
114 // All BigPipe placeholders are processed, so update this response's
116 unset($this->attachments['big_pipe_placeholders']);
117 unset($this->attachments['big_pipe_nojs_placeholders']);