annotate core/modules/big_pipe/src/Render/BigPipe.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\big_pipe\Render;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Crypt;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7 use Drupal\Core\Ajax\AjaxResponse;
Chris@0 8 use Drupal\Core\Ajax\ReplaceCommand;
Chris@0 9 use Drupal\Core\Asset\AttachedAssets;
Chris@0 10 use Drupal\Core\Asset\AttachedAssetsInterface;
Chris@0 11 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 12 use Drupal\Core\Render\HtmlResponse;
Chris@0 13 use Drupal\Core\Render\RendererInterface;
Chris@0 14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Chris@0 15 use Symfony\Component\HttpFoundation\Request;
Chris@0 16 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 17 use Symfony\Component\HttpFoundation\Response;
Chris@0 18 use Symfony\Component\HttpFoundation\Session\SessionInterface;
Chris@0 19 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
Chris@0 20 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 21 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Service for sending an HTML response in chunks (to get faster page loads).
Chris@0 25 *
Chris@0 26 * At a high level, BigPipe sends a HTML response in chunks:
Chris@0 27 * 1. one chunk: everything until just before </body> — this contains BigPipe
Chris@0 28 * placeholders for the personalized parts of the page. Hence this sends the
Chris@0 29 * non-personalized parts of the page. Let's call it The Skeleton.
Chris@0 30 * 2. N chunks: a <script> tag per BigPipe placeholder in The Skeleton.
Chris@0 31 * 3. one chunk: </body> and everything after it.
Chris@0 32 *
Chris@0 33 * This is conceptually identical to Facebook's BigPipe (hence the name).
Chris@0 34 *
Chris@0 35 * @see https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919
Chris@0 36 *
Chris@0 37 * The major way in which Drupal differs from Facebook's implementation (and
Chris@0 38 * others) is in its ability to automatically figure out which parts of the page
Chris@0 39 * can benefit from BigPipe-style delivery. Drupal's render system has the
Chris@0 40 * concept of "auto-placeholdering": content that is too dynamic is replaced
Chris@0 41 * with a placeholder that can then be rendered at a later time. On top of that,
Chris@0 42 * it also has the concept of "placeholder strategies": by default, placeholders
Chris@0 43 * are replaced on the server side and the response is blocked on all of them
Chris@0 44 * being replaced. But it's possible to add additional placeholder strategies.
Chris@0 45 * BigPipe is just another placeholder strategy. Others could be ESI, AJAX …
Chris@0 46 *
Chris@0 47 * @see https://www.drupal.org/developing/api/8/render/arrays/cacheability/auto-placeholdering
Chris@0 48 * @see \Drupal\Core\Render\PlaceholderGeneratorInterface::shouldAutomaticallyPlaceholder()
Chris@0 49 * @see \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface
Chris@0 50 * @see \Drupal\Core\Render\Placeholder\SingleFlushStrategy
Chris@0 51 * @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
Chris@0 52 *
Chris@0 53 * There is also one noteworthy technical addition that Drupal makes. BigPipe as
Chris@0 54 * described above, and as implemented by Facebook, can only work if JavaScript
Chris@0 55 * is enabled. The BigPipe module also makes it possible to replace placeholders
Chris@0 56 * using BigPipe in-situ, without JavaScript. This is not technically BigPipe at
Chris@0 57 * all; it's just the use of multiple flushes. Since it is able to reuse much of
Chris@0 58 * the logic though, we choose to call this "no-JS BigPipe".
Chris@0 59 *
Chris@0 60 * However, there is also a tangible benefit: some dynamic/expensive content is
Chris@0 61 * not HTML, but for example a HTML attribute value (or part thereof). It's not
Chris@0 62 * possible to efficiently replace such content using JavaScript, so "classic"
Chris@0 63 * BigPipe is out of the question. For example: CSRF tokens in URLs.
Chris@0 64 *
Chris@0 65 * This allows us to use both no-JS BigPipe and "classic" BigPipe in the same
Chris@0 66 * response to maximize the amount of content we can send as early as possible.
Chris@0 67 *
Chris@0 68 * Finally, a closer look at the implementation, and how it supports and reuses
Chris@0 69 * existing Drupal concepts:
Chris@0 70 * 1. BigPipe placeholders: 1 HtmlResponse + N embedded AjaxResponses.
Chris@0 71 * - Before a BigPipe response is sent, it is just a HTML response that
Chris@0 72 * contains BigPipe placeholders. Those placeholders look like
Chris@0 73 * <span data-big-pipe-placeholder-id="…"></span>. JavaScript is used to
Chris@0 74 * replace those placeholders.
Chris@0 75 * Therefore these placeholders are actually sent to the client.
Chris@0 76 * - The Skeleton of course has attachments, including most notably asset
Chris@0 77 * libraries. And those we track in drupalSettings.ajaxPageState.libraries —
Chris@0 78 * so that when we load new content through AJAX, we don't load the same
Chris@0 79 * asset libraries again. A HTML page can have multiple AJAX responses, each
Chris@0 80 * of which should take into account the combined AJAX page state of the
Chris@0 81 * HTML document and all preceding AJAX responses.
Chris@0 82 * - BigPipe does not make use of multiple AJAX requests/responses. It uses a
Chris@0 83 * single HTML response. But it is a more long-lived one: The Skeleton is
Chris@0 84 * sent first, the closing </body> tag is not yet sent, and the connection
Chris@0 85 * is kept open. Whenever another BigPipe Placeholder is rendered, Drupal
Chris@0 86 * sends (and so actually appends to the already-sent HTML) something like
Chris@0 87 * <script type="application/vnd.drupal-ajax">[{"command":"settings","settings":{…}}, {"command":…}.
Chris@0 88 * - So, for every BigPipe placeholder, we send such a <script
Chris@0 89 * type="application/vnd.drupal-ajax"> tag. And the contents of that tag is
Chris@0 90 * exactly like an AJAX response. The BigPipe module has JavaScript that
Chris@0 91 * listens for these and applies them. Let's call it an Embedded AJAX
Chris@0 92 * Response (since it is embedded in the HTML response). Now for the
Chris@0 93 * interesting bit: each of those Embedded AJAX Responses must also take
Chris@0 94 * into account the cumulative AJAX page state of the HTML document and all
Chris@0 95 * preceding Embedded AJAX responses.
Chris@0 96 * 2. No-JS BigPipe placeholders: 1 HtmlResponse + N embedded HtmlResponses.
Chris@0 97 * - Before a BigPipe response is sent, it is just a HTML response that
Chris@0 98 * contains no-JS BigPipe placeholders. Those placeholders can take two
Chris@0 99 * different forms:
Chris@0 100 * 1. <span data-big-pipe-nojs-placeholder-id="…"></span> if it's a
Chris@0 101 * placeholder that will be replaced by HTML
Chris@0 102 * 2. big_pipe_nojs_placeholder_attribute_safe:… if it's a placeholder
Chris@0 103 * inside a HTML attribute, in which 1. would be invalid (angle brackets
Chris@0 104 * are not allowed inside HTML attributes)
Chris@0 105 * No-JS BigPipe placeholders are not replaced using JavaScript, they must
Chris@0 106 * be replaced upon sending the BigPipe response. So, while the response is
Chris@0 107 * being sent, upon encountering these placeholders, their corresponding
Chris@0 108 * placeholder replacements are sent instead.
Chris@0 109 * Therefore these placeholders are never actually sent to the client.
Chris@0 110 * - See second bullet of point 1.
Chris@0 111 * - No-JS BigPipe does not use multiple AJAX requests/responses. It uses a
Chris@0 112 * single HTML response. But it is a more long-lived one: The Skeleton is
Chris@0 113 * split into multiple parts, the separators are where the no-JS BigPipe
Chris@0 114 * placeholders used to be. Whenever another no-JS BigPipe placeholder is
Chris@0 115 * rendered, Drupal sends (and so actually appends to the already-sent HTML)
Chris@0 116 * something like
Chris@0 117 * <link rel="stylesheet" …><script …><content>.
Chris@0 118 * - So, for every no-JS BigPipe placeholder, we send its associated CSS and
Chris@0 119 * header JS that has not already been sent (the bottom JS is not yet sent,
Chris@0 120 * so we can accumulate all of it and send it together at the end). This
Chris@0 121 * ensures that the markup is rendered as it was originally intended: its
Chris@0 122 * CSS and JS used to be blocking, and it still is. Let's call it an
Chris@0 123 * Embedded HTML response. Each of those Embedded HTML Responses must also
Chris@0 124 * take into account the cumulative AJAX page state of the HTML document and
Chris@0 125 * all preceding Embedded HTML responses.
Chris@0 126 * - Finally: any non-critical JavaScript associated with all Embedded HTML
Chris@0 127 * Responses, i.e. any footer/bottom/non-header JavaScript, is loaded after
Chris@0 128 * The Skeleton.
Chris@0 129 *
Chris@0 130 * Combining all of the above, when using both BigPipe placeholders and no-JS
Chris@0 131 * BigPipe placeholders, we therefore send: 1 HtmlResponse + M Embedded HTML
Chris@0 132 * Responses + N Embedded AJAX Responses. Schematically, we send these chunks:
Chris@0 133 * 1. Byte zero until 1st no-JS placeholder: headers + <html><head /><span>…</span>
Chris@0 134 * 2. 1st no-JS placeholder replacement: <link rel="stylesheet" …><script …><content>
Chris@0 135 * 3. Content until 2nd no-JS placeholder: <span>…</span>
Chris@0 136 * 4. 2nd no-JS placeholder replacement: <link rel="stylesheet" …><script …><content>
Chris@0 137 * 5. Content until 3rd no-JS placeholder: <span>…</span>
Chris@0 138 * 6. [… repeat until all no-JS placeholder replacements are sent …]
Chris@0 139 * 7. Send content after last no-JS placeholder.
Chris@0 140 * 8. Send script_bottom (markup to load bottom i.e. non-critical JS).
Chris@0 141 * 9. 1st placeholder replacement: <script type="application/vnd.drupal-ajax">[{"command":"settings","settings":{…}}, {"command":…}
Chris@0 142 * 10. 2nd placeholder replacement: <script type="application/vnd.drupal-ajax">[{"command":"settings","settings":{…}}, {"command":…}
Chris@0 143 * 11. [… repeat until all placeholder replacements are sent …]
Chris@0 144 * 12. Send </body> and everything after it.
Chris@0 145 * 13. Terminate request/response cycle.
Chris@0 146 *
Chris@0 147 * @see \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber
Chris@0 148 * @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
Chris@0 149 */
Chris@0 150 class BigPipe {
Chris@0 151
Chris@0 152 /**
Chris@0 153 * The BigPipe placeholder replacements start signal.
Chris@0 154 *
Chris@0 155 * @var string
Chris@0 156 */
Chris@0 157 const START_SIGNAL = '<script type="application/vnd.drupal-ajax" data-big-pipe-event="start"></script>';
Chris@0 158
Chris@0 159 /**
Chris@0 160 * The BigPipe placeholder replacements stop signal.
Chris@0 161 *
Chris@0 162 * @var string
Chris@0 163 */
Chris@0 164 const STOP_SIGNAL = '<script type="application/vnd.drupal-ajax" data-big-pipe-event="stop"></script>';
Chris@0 165
Chris@0 166 /**
Chris@0 167 * The renderer.
Chris@0 168 *
Chris@0 169 * @var \Drupal\Core\Render\RendererInterface
Chris@0 170 */
Chris@0 171 protected $renderer;
Chris@0 172
Chris@0 173 /**
Chris@0 174 * The session.
Chris@0 175 *
Chris@0 176 * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
Chris@0 177 */
Chris@0 178 protected $session;
Chris@0 179
Chris@0 180 /**
Chris@0 181 * The request stack.
Chris@0 182 *
Chris@0 183 * @var \Symfony\Component\HttpFoundation\RequestStack
Chris@0 184 */
Chris@0 185 protected $requestStack;
Chris@0 186
Chris@0 187 /**
Chris@0 188 * The HTTP kernel.
Chris@0 189 *
Chris@0 190 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
Chris@0 191 */
Chris@0 192 protected $httpKernel;
Chris@0 193
Chris@0 194 /**
Chris@0 195 * The event dispatcher.
Chris@0 196 *
Chris@0 197 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
Chris@0 198 */
Chris@0 199 protected $eventDispatcher;
Chris@0 200
Chris@0 201 /**
Chris@0 202 * The config factory.
Chris@0 203 *
Chris@0 204 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 205 */
Chris@0 206 protected $configFactory;
Chris@0 207
Chris@0 208 /**
Chris@0 209 * Constructs a new BigPipe class.
Chris@0 210 *
Chris@0 211 * @param \Drupal\Core\Render\RendererInterface $renderer
Chris@0 212 * The renderer.
Chris@0 213 * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
Chris@0 214 * The session.
Chris@0 215 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
Chris@0 216 * The request stack.
Chris@0 217 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
Chris@0 218 * The HTTP kernel.
Chris@0 219 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
Chris@0 220 * The event dispatcher.
Chris@0 221 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 222 * The config factory.
Chris@0 223 */
Chris@0 224 public function __construct(RendererInterface $renderer, SessionInterface $session, RequestStack $request_stack, HttpKernelInterface $http_kernel, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory) {
Chris@0 225 $this->renderer = $renderer;
Chris@0 226 $this->session = $session;
Chris@0 227 $this->requestStack = $request_stack;
Chris@0 228 $this->httpKernel = $http_kernel;
Chris@0 229 $this->eventDispatcher = $event_dispatcher;
Chris@0 230 $this->configFactory = $config_factory;
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * Performs tasks before sending content (and rendering placeholders).
Chris@0 235 */
Chris@0 236 protected function performPreSendTasks() {
Chris@0 237 // The content in the placeholders may depend on the session, and by the
Chris@0 238 // time the response is sent (see index.php), the session is already
Chris@0 239 // closed. Reopen it for the duration that we are rendering placeholders.
Chris@0 240 $this->session->start();
Chris@0 241 }
Chris@0 242
Chris@0 243 /**
Chris@0 244 * Performs tasks after sending content (and rendering placeholders).
Chris@0 245 */
Chris@0 246 protected function performPostSendTasks() {
Chris@0 247 // Close the session again.
Chris@0 248 $this->session->save();
Chris@0 249 }
Chris@0 250
Chris@0 251 /**
Chris@0 252 * Sends a chunk.
Chris@0 253 *
Chris@0 254 * @param string|\Drupal\Core\Render\HtmlResponse $chunk
Chris@0 255 * The string or response to append. String if there's no cacheability
Chris@0 256 * metadata or attachments to merge.
Chris@0 257 */
Chris@0 258 protected function sendChunk($chunk) {
Chris@0 259 assert(is_string($chunk) || $chunk instanceof HtmlResponse);
Chris@0 260 if ($chunk instanceof HtmlResponse) {
Chris@0 261 print $chunk->getContent();
Chris@0 262 }
Chris@0 263 else {
Chris@0 264 print $chunk;
Chris@0 265 }
Chris@0 266 flush();
Chris@0 267 }
Chris@0 268
Chris@0 269 /**
Chris@0 270 * Sends an HTML response in chunks using the BigPipe technique.
Chris@0 271 *
Chris@0 272 * @param \Drupal\big_pipe\Render\BigPipeResponse $response
Chris@0 273 * The BigPipe response to send.
Chris@0 274 *
Chris@0 275 * @internal
Chris@0 276 * This method should only be invoked by
Chris@0 277 * \Drupal\big_pipe\Render\BigPipeResponse, which is itself an internal
Chris@0 278 * class.
Chris@0 279 */
Chris@0 280 public function sendContent(BigPipeResponse $response) {
Chris@0 281 $content = $response->getContent();
Chris@0 282 $attachments = $response->getAttachments();
Chris@0 283
Chris@0 284 // First, gather the BigPipe placeholders that must be replaced.
Chris@0 285 $placeholders = isset($attachments['big_pipe_placeholders']) ? $attachments['big_pipe_placeholders'] : [];
Chris@0 286 $nojs_placeholders = isset($attachments['big_pipe_nojs_placeholders']) ? $attachments['big_pipe_nojs_placeholders'] : [];
Chris@0 287
Chris@0 288 // BigPipe sends responses using "Transfer-Encoding: chunked". To avoid
Chris@0 289 // sending already-sent assets, it is necessary to track cumulative assets
Chris@0 290 // from all previously rendered/sent chunks.
Chris@0 291 // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.41
Chris@0 292 $cumulative_assets = AttachedAssets::createFromRenderArray(['#attached' => $attachments]);
Chris@0 293 $cumulative_assets->setAlreadyLoadedLibraries($attachments['library']);
Chris@0 294
Chris@0 295 $this->performPreSendTasks();
Chris@0 296
Chris@0 297 // Find the closing </body> tag and get the strings before and after. But be
Chris@0 298 // careful to use the latest occurrence of the string "</body>", to ensure
Chris@0 299 // that strings in inline JavaScript or CDATA sections aren't used instead.
Chris@0 300 $parts = explode('</body>', $content);
Chris@0 301 $post_body = array_pop($parts);
Chris@0 302 $pre_body = implode('', $parts);
Chris@0 303
Chris@0 304 $this->sendPreBody($pre_body, $nojs_placeholders, $cumulative_assets);
Chris@0 305 $this->sendPlaceholders($placeholders, $this->getPlaceholderOrder($pre_body, $placeholders), $cumulative_assets);
Chris@0 306 $this->sendPostBody($post_body);
Chris@0 307
Chris@0 308 $this->performPostSendTasks();
Chris@0 309 }
Chris@0 310
Chris@0 311 /**
Chris@0 312 * Sends everything until just before </body>.
Chris@0 313 *
Chris@0 314 * @param string $pre_body
Chris@0 315 * The HTML response's content until the closing </body> tag.
Chris@0 316 * @param array $no_js_placeholders
Chris@0 317 * The no-JS BigPipe placeholders.
Chris@0 318 * @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets
Chris@0 319 * The cumulative assets sent so far; to be updated while rendering no-JS
Chris@0 320 * BigPipe placeholders.
Chris@0 321 */
Chris@0 322 protected function sendPreBody($pre_body, array $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) {
Chris@0 323 // If there are no no-JS BigPipe placeholders, we can send the pre-</body>
Chris@0 324 // part of the page immediately.
Chris@0 325 if (empty($no_js_placeholders)) {
Chris@0 326 $this->sendChunk($pre_body);
Chris@0 327 return;
Chris@0 328 }
Chris@0 329
Chris@0 330 // Extract the scripts_bottom markup: the no-JS BigPipe placeholders that we
Chris@0 331 // will render may attach additional asset libraries, and if so, it will be
Chris@0 332 // necessary to re-render scripts_bottom.
Chris@0 333 list($pre_scripts_bottom, $scripts_bottom, $post_scripts_bottom) = explode('<drupal-big-pipe-scripts-bottom-marker>', $pre_body, 3);
Chris@0 334 $cumulative_assets_initial = clone $cumulative_assets;
Chris@0 335
Chris@0 336 $this->sendNoJsPlaceholders($pre_scripts_bottom . $post_scripts_bottom, $no_js_placeholders, $cumulative_assets);
Chris@0 337
Chris@0 338 // If additional asset libraries or drupalSettings were attached by any of
Chris@0 339 // the placeholders, then we need to re-render scripts_bottom.
Chris@0 340 if ($cumulative_assets_initial != $cumulative_assets) {
Chris@0 341 // Create a new HtmlResponse. Ensure the CSS and (non-bottom) JS is sent
Chris@0 342 // before the HTML they're associated with.
Chris@0 343 // @see \Drupal\Core\Render\HtmlResponseSubscriber
Chris@0 344 // @see template_preprocess_html()
Chris@0 345 $js_bottom_placeholder = '<nojs-bigpipe-placeholder-scripts-bottom-placeholder token="' . Crypt::randomBytesBase64(55) . '">';
Chris@0 346
Chris@0 347 $html_response = new HtmlResponse();
Chris@0 348 $html_response->setContent([
Chris@0 349 '#markup' => BigPipeMarkup::create($js_bottom_placeholder),
Chris@0 350 '#attached' => [
Chris@0 351 'drupalSettings' => $cumulative_assets->getSettings(),
Chris@0 352 'library' => $cumulative_assets->getAlreadyLoadedLibraries(),
Chris@0 353 'html_response_attachment_placeholders' => [
Chris@0 354 'scripts_bottom' => $js_bottom_placeholder,
Chris@0 355 ],
Chris@0 356 ],
Chris@0 357 ]);
Chris@0 358 $html_response->getCacheableMetadata()->setCacheMaxAge(0);
Chris@0 359
Chris@0 360 // Push a fake request with the asset libraries loaded so far and dispatch
Chris@0 361 // KernelEvents::RESPONSE event. This results in the attachments for the
Chris@0 362 // HTML response being processed by HtmlResponseAttachmentsProcessor and
Chris@0 363 // hence the HTML to load the bottom JavaScript can be rendered.
Chris@0 364 $fake_request = $this->requestStack->getMasterRequest()->duplicate();
Chris@0 365 $html_response = $this->filterEmbeddedResponse($fake_request, $html_response);
Chris@0 366 $scripts_bottom = $html_response->getContent();
Chris@0 367 }
Chris@0 368
Chris@0 369 $this->sendChunk($scripts_bottom);
Chris@0 370 }
Chris@0 371
Chris@0 372 /**
Chris@0 373 * Sends no-JS BigPipe placeholders' replacements as embedded HTML responses.
Chris@0 374 *
Chris@0 375 * @param string $html
Chris@0 376 * HTML markup.
Chris@0 377 * @param array $no_js_placeholders
Chris@0 378 * Associative array; the no-JS BigPipe placeholders. Keys are the BigPipe
Chris@0 379 * selectors.
Chris@0 380 * @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets
Chris@0 381 * The cumulative assets sent so far; to be updated while rendering no-JS
Chris@0 382 * BigPipe placeholders.
Chris@0 383 *
Chris@0 384 * @throws \Exception
Chris@0 385 * If an exception is thrown during the rendering of a placeholder, it is
Chris@0 386 * caught to allow the other placeholders to still be replaced. But when
Chris@0 387 * error logging is configured to be verbose, the exception is rethrown to
Chris@0 388 * simplify debugging.
Chris@0 389 */
Chris@0 390 protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) {
Chris@0 391 // Split the HTML on every no-JS placeholder string.
Chris@0 392 $prepare_for_preg_split = function ($placeholder_string) {
Chris@0 393 return '(' . preg_quote($placeholder_string, '/') . ')';
Chris@0 394 };
Chris@0 395 $preg_placeholder_strings = array_map($prepare_for_preg_split, array_keys($no_js_placeholders));
Chris@0 396 $fragments = preg_split('/' . implode('|', $preg_placeholder_strings) . '/', $html, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
Chris@0 397
Chris@0 398 // Determine how many occurrences there are of each no-JS placeholder.
Chris@0 399 $placeholder_occurrences = array_count_values(array_intersect($fragments, array_keys($no_js_placeholders)));
Chris@0 400
Chris@0 401 // Set up a variable to store the content of placeholders that have multiple
Chris@0 402 // occurrences.
Chris@0 403 $multi_occurrence_placeholders_content = [];
Chris@0 404
Chris@0 405 foreach ($fragments as $fragment) {
Chris@0 406 // If the fragment isn't one of the no-JS placeholders, it is the HTML in
Chris@0 407 // between placeholders and it must be printed & flushed immediately. The
Chris@0 408 // rest of the logic in the loop handles the placeholders.
Chris@0 409 if (!isset($no_js_placeholders[$fragment])) {
Chris@0 410 $this->sendChunk($fragment);
Chris@0 411 continue;
Chris@0 412 }
Chris@0 413
Chris@0 414 // If there are multiple occurrences of this particular placeholder, and
Chris@0 415 // this is the second occurrence, we can skip all calculations and just
Chris@0 416 // send the same content.
Chris@0 417 if ($placeholder_occurrences[$fragment] > 1 && isset($multi_occurrence_placeholders_content[$fragment])) {
Chris@0 418 $this->sendChunk($multi_occurrence_placeholders_content[$fragment]);
Chris@0 419 continue;
Chris@0 420 }
Chris@0 421
Chris@0 422 $placeholder = $fragment;
Chris@14 423 assert(isset($no_js_placeholders[$placeholder]));
Chris@0 424 $token = Crypt::randomBytesBase64(55);
Chris@0 425
Chris@0 426 // Render the placeholder, but include the cumulative settings assets, so
Chris@0 427 // we can calculate the overall settings for the entire page.
Chris@0 428 $placeholder_plus_cumulative_settings = [
Chris@0 429 'placeholder' => $no_js_placeholders[$placeholder],
Chris@0 430 'cumulative_settings_' . $token => [
Chris@0 431 '#attached' => [
Chris@0 432 'drupalSettings' => $cumulative_assets->getSettings(),
Chris@0 433 ],
Chris@0 434 ],
Chris@0 435 ];
Chris@0 436 try {
Chris@0 437 $elements = $this->renderPlaceholder($placeholder, $placeholder_plus_cumulative_settings);
Chris@0 438 }
Chris@0 439 catch (\Exception $e) {
Chris@0 440 if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
Chris@0 441 throw $e;
Chris@0 442 }
Chris@0 443 else {
Chris@0 444 trigger_error($e, E_USER_ERROR);
Chris@0 445 continue;
Chris@0 446 }
Chris@0 447 }
Chris@0 448
Chris@0 449 // Create a new HtmlResponse. Ensure the CSS and (non-bottom) JS is sent
Chris@0 450 // before the HTML they're associated with. In other words: ensure the
Chris@0 451 // critical assets for this placeholder's markup are loaded first.
Chris@0 452 // @see \Drupal\Core\Render\HtmlResponseSubscriber
Chris@0 453 // @see template_preprocess_html()
Chris@0 454 $css_placeholder = '<nojs-bigpipe-placeholder-styles-placeholder token="' . $token . '">';
Chris@0 455 $js_placeholder = '<nojs-bigpipe-placeholder-scripts-placeholder token="' . $token . '">';
Chris@0 456 $elements['#markup'] = BigPipeMarkup::create($css_placeholder . $js_placeholder . (string) $elements['#markup']);
Chris@0 457 $elements['#attached']['html_response_attachment_placeholders']['styles'] = $css_placeholder;
Chris@0 458 $elements['#attached']['html_response_attachment_placeholders']['scripts'] = $js_placeholder;
Chris@0 459
Chris@0 460 $html_response = new HtmlResponse();
Chris@0 461 $html_response->setContent($elements);
Chris@0 462 $html_response->getCacheableMetadata()->setCacheMaxAge(0);
Chris@0 463
Chris@0 464 // Push a fake request with the asset libraries loaded so far and dispatch
Chris@0 465 // KernelEvents::RESPONSE event. This results in the attachments for the
Chris@0 466 // HTML response being processed by HtmlResponseAttachmentsProcessor and
Chris@0 467 // hence:
Chris@0 468 // - the HTML to load the CSS can be rendered.
Chris@0 469 // - the HTML to load the JS (at the top) can be rendered.
Chris@0 470 $fake_request = $this->requestStack->getMasterRequest()->duplicate();
Chris@0 471 $fake_request->request->set('ajax_page_state', ['libraries' => implode(',', $cumulative_assets->getAlreadyLoadedLibraries())]);
Chris@0 472 try {
Chris@0 473 $html_response = $this->filterEmbeddedResponse($fake_request, $html_response);
Chris@0 474 }
Chris@0 475 catch (\Exception $e) {
Chris@0 476 if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
Chris@0 477 throw $e;
Chris@0 478 }
Chris@0 479 else {
Chris@0 480 trigger_error($e, E_USER_ERROR);
Chris@0 481 continue;
Chris@0 482 }
Chris@0 483 }
Chris@0 484
Chris@0 485 // Send this embedded HTML response.
Chris@0 486 $this->sendChunk($html_response);
Chris@0 487
Chris@0 488 // Another placeholder was rendered and sent, track the set of asset
Chris@0 489 // libraries sent so far. Any new settings also need to be tracked, so
Chris@0 490 // they can be sent in ::sendPreBody().
Chris@0 491 $cumulative_assets->setAlreadyLoadedLibraries(array_merge($cumulative_assets->getAlreadyLoadedLibraries(), $html_response->getAttachments()['library']));
Chris@0 492 $cumulative_assets->setSettings($html_response->getAttachments()['drupalSettings']);
Chris@0 493
Chris@0 494 // If there are multiple occurrences of this particular placeholder, track
Chris@0 495 // the content that was sent, so we can skip all calculations for the next
Chris@0 496 // occurrence.
Chris@0 497 if ($placeholder_occurrences[$fragment] > 1) {
Chris@0 498 $multi_occurrence_placeholders_content[$fragment] = $html_response->getContent();
Chris@0 499 }
Chris@0 500 }
Chris@0 501 }
Chris@0 502
Chris@0 503 /**
Chris@0 504 * Sends BigPipe placeholders' replacements as embedded AJAX responses.
Chris@0 505 *
Chris@0 506 * @param array $placeholders
Chris@0 507 * Associative array; the BigPipe placeholders. Keys are the BigPipe
Chris@0 508 * placeholder IDs.
Chris@0 509 * @param array $placeholder_order
Chris@0 510 * Indexed array; the order in which the BigPipe placeholders must be sent.
Chris@0 511 * Values are the BigPipe placeholder IDs. (These values correspond to keys
Chris@0 512 * in $placeholders.)
Chris@0 513 * @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets
Chris@0 514 * The cumulative assets sent so far; to be updated while rendering BigPipe
Chris@0 515 * placeholders.
Chris@0 516 *
Chris@0 517 * @throws \Exception
Chris@0 518 * If an exception is thrown during the rendering of a placeholder, it is
Chris@0 519 * caught to allow the other placeholders to still be replaced. But when
Chris@0 520 * error logging is configured to be verbose, the exception is rethrown to
Chris@0 521 * simplify debugging.
Chris@0 522 */
Chris@0 523 protected function sendPlaceholders(array $placeholders, array $placeholder_order, AttachedAssetsInterface $cumulative_assets) {
Chris@0 524 // Return early if there are no BigPipe placeholders to send.
Chris@0 525 if (empty($placeholders)) {
Chris@0 526 return;
Chris@0 527 }
Chris@0 528
Chris@0 529 // Send the start signal.
Chris@0 530 $this->sendChunk("\n" . static::START_SIGNAL . "\n");
Chris@0 531
Chris@0 532 // A BigPipe response consists of a HTML response plus multiple embedded
Chris@0 533 // AJAX responses. To process the attachments of those AJAX responses, we
Chris@0 534 // need a fake request that is identical to the master request, but with
Chris@0 535 // one change: it must have the right Accept header, otherwise the work-
Chris@0 536 // around for a bug in IE9 will cause not JSON, but <textarea>-wrapped JSON
Chris@0 537 // to be returned.
Chris@0 538 // @see \Drupal\Core\EventSubscriber\AjaxResponseSubscriber::onResponse()
Chris@0 539 $fake_request = $this->requestStack->getMasterRequest()->duplicate();
Chris@0 540 $fake_request->headers->set('Accept', 'application/vnd.drupal-ajax');
Chris@0 541
Chris@0 542 foreach ($placeholder_order as $placeholder_id) {
Chris@0 543 if (!isset($placeholders[$placeholder_id])) {
Chris@0 544 continue;
Chris@0 545 }
Chris@0 546
Chris@0 547 // Render the placeholder.
Chris@0 548 $placeholder_render_array = $placeholders[$placeholder_id];
Chris@0 549 try {
Chris@0 550 $elements = $this->renderPlaceholder($placeholder_id, $placeholder_render_array);
Chris@0 551 }
Chris@0 552 catch (\Exception $e) {
Chris@0 553 if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
Chris@0 554 throw $e;
Chris@0 555 }
Chris@0 556 else {
Chris@0 557 trigger_error($e, E_USER_ERROR);
Chris@0 558 continue;
Chris@0 559 }
Chris@0 560 }
Chris@0 561
Chris@0 562 // Create a new AjaxResponse.
Chris@0 563 $ajax_response = new AjaxResponse();
Chris@0 564 // JavaScript's querySelector automatically decodes HTML entities in
Chris@0 565 // attributes, so we must decode the entities of the current BigPipe
Chris@0 566 // placeholder ID (which has HTML entities encoded since we use it to find
Chris@0 567 // the placeholders).
Chris@0 568 $big_pipe_js_placeholder_id = Html::decodeEntities($placeholder_id);
Chris@0 569 $ajax_response->addCommand(new ReplaceCommand(sprintf('[data-big-pipe-placeholder-id="%s"]', $big_pipe_js_placeholder_id), $elements['#markup']));
Chris@0 570 $ajax_response->setAttachments($elements['#attached']);
Chris@0 571
Chris@0 572 // Push a fake request with the asset libraries loaded so far and dispatch
Chris@0 573 // KernelEvents::RESPONSE event. This results in the attachments for the
Chris@0 574 // AJAX response being processed by AjaxResponseAttachmentsProcessor and
Chris@0 575 // hence:
Chris@0 576 // - the necessary AJAX commands to load the necessary missing asset
Chris@0 577 // libraries and updated AJAX page state are added to the AJAX response
Chris@0 578 // - the attachments associated with the response are finalized, which
Chris@0 579 // allows us to track the total set of asset libraries sent in the
Chris@0 580 // initial HTML response plus all embedded AJAX responses sent so far.
Chris@0 581 $fake_request->request->set('ajax_page_state', ['libraries' => implode(',', $cumulative_assets->getAlreadyLoadedLibraries())] + $cumulative_assets->getSettings()['ajaxPageState']);
Chris@0 582 try {
Chris@0 583 $ajax_response = $this->filterEmbeddedResponse($fake_request, $ajax_response);
Chris@0 584 }
Chris@0 585 catch (\Exception $e) {
Chris@0 586 if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
Chris@0 587 throw $e;
Chris@0 588 }
Chris@0 589 else {
Chris@0 590 trigger_error($e, E_USER_ERROR);
Chris@0 591 continue;
Chris@0 592 }
Chris@0 593 }
Chris@0 594
Chris@0 595 // Send this embedded AJAX response.
Chris@0 596 $json = $ajax_response->getContent();
Chris@0 597 $output = <<<EOF
Chris@0 598 <script type="application/vnd.drupal-ajax" data-big-pipe-replacement-for-placeholder-with-id="$placeholder_id">
Chris@0 599 $json
Chris@0 600 </script>
Chris@0 601 EOF;
Chris@0 602 $this->sendChunk($output);
Chris@0 603
Chris@0 604 // Another placeholder was rendered and sent, track the set of asset
Chris@0 605 // libraries sent so far. Any new settings are already sent; we don't need
Chris@0 606 // to track those.
Chris@0 607 if (isset($ajax_response->getAttachments()['drupalSettings']['ajaxPageState']['libraries'])) {
Chris@0 608 $cumulative_assets->setAlreadyLoadedLibraries(explode(',', $ajax_response->getAttachments()['drupalSettings']['ajaxPageState']['libraries']));
Chris@0 609 }
Chris@0 610 }
Chris@0 611
Chris@0 612 // Send the stop signal.
Chris@0 613 $this->sendChunk("\n" . static::STOP_SIGNAL . "\n");
Chris@0 614 }
Chris@0 615
Chris@0 616 /**
Chris@0 617 * Filters the given embedded response, using the cumulative AJAX page state.
Chris@0 618 *
Chris@0 619 * @param \Symfony\Component\HttpFoundation\Request $fake_request
Chris@0 620 * A fake subrequest that contains the cumulative AJAX page state of the
Chris@0 621 * HTML document and all preceding Embedded HTML or AJAX responses.
Chris@0 622 * @param \Symfony\Component\HttpFoundation\Response|\Drupal\Core\Render\HtmlResponse|\Drupal\Core\Ajax\AjaxResponse $embedded_response
Chris@0 623 * Either a HTML response or an AJAX response that will be embedded in the
Chris@0 624 * overall HTML response.
Chris@0 625 *
Chris@0 626 * @return \Symfony\Component\HttpFoundation\Response
Chris@0 627 * The filtered response, which will load only the assets that $fake_request
Chris@0 628 * did not indicate to already have been loaded, plus the updated cumulative
Chris@0 629 * AJAX page state.
Chris@0 630 */
Chris@0 631 protected function filterEmbeddedResponse(Request $fake_request, Response $embedded_response) {
Chris@14 632 assert($embedded_response instanceof HtmlResponse || $embedded_response instanceof AjaxResponse);
Chris@0 633 return $this->filterResponse($fake_request, HttpKernelInterface::SUB_REQUEST, $embedded_response);
Chris@0 634 }
Chris@0 635
Chris@0 636 /**
Chris@0 637 * Filters the given response.
Chris@0 638 *
Chris@0 639 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 640 * The request for which a response is being sent.
Chris@0 641 * @param int $request_type
Chris@0 642 * The request type. Can either be
Chris@0 643 * \Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST or
Chris@0 644 * \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST.
Chris@0 645 * @param \Symfony\Component\HttpFoundation\Response $response
Chris@0 646 * The response to filter.
Chris@0 647 *
Chris@0 648 * @return \Symfony\Component\HttpFoundation\Response
Chris@0 649 * The filtered response.
Chris@0 650 */
Chris@0 651 protected function filterResponse(Request $request, $request_type, Response $response) {
Chris@14 652 assert($request_type === HttpKernelInterface::MASTER_REQUEST || $request_type === HttpKernelInterface::SUB_REQUEST);
Chris@0 653 $this->requestStack->push($request);
Chris@0 654 $event = new FilterResponseEvent($this->httpKernel, $request, $request_type, $response);
Chris@0 655 $this->eventDispatcher->dispatch(KernelEvents::RESPONSE, $event);
Chris@0 656 $filtered_response = $event->getResponse();
Chris@0 657 $this->requestStack->pop();
Chris@0 658 return $filtered_response;
Chris@0 659 }
Chris@0 660
Chris@0 661 /**
Chris@0 662 * Sends </body> and everything after it.
Chris@0 663 *
Chris@0 664 * @param string $post_body
Chris@0 665 * The HTML response's content after the closing </body> tag.
Chris@0 666 */
Chris@0 667 protected function sendPostBody($post_body) {
Chris@0 668 $this->sendChunk('</body>' . $post_body);
Chris@0 669 }
Chris@0 670
Chris@0 671 /**
Chris@0 672 * Renders a placeholder, and just that placeholder.
Chris@0 673 *
Chris@0 674 * BigPipe renders placeholders independently of the rest of the content, so
Chris@0 675 * it needs to be able to render placeholders by themselves.
Chris@0 676 *
Chris@0 677 * @param string $placeholder
Chris@0 678 * The placeholder to render.
Chris@0 679 * @param array $placeholder_render_array
Chris@0 680 * The render array associated with that placeholder.
Chris@0 681 *
Chris@0 682 * @return array
Chris@0 683 * The render array representing the rendered placeholder.
Chris@0 684 *
Chris@0 685 * @see \Drupal\Core\Render\RendererInterface::renderPlaceholder()
Chris@0 686 */
Chris@0 687 protected function renderPlaceholder($placeholder, array $placeholder_render_array) {
Chris@0 688 $elements = [
Chris@0 689 '#markup' => $placeholder,
Chris@0 690 '#attached' => [
Chris@0 691 'placeholders' => [
Chris@0 692 $placeholder => $placeholder_render_array,
Chris@0 693 ],
Chris@0 694 ],
Chris@0 695 ];
Chris@0 696 return $this->renderer->renderPlaceholder($placeholder, $elements);
Chris@0 697 }
Chris@0 698
Chris@0 699 /**
Chris@0 700 * Gets the BigPipe placeholder order.
Chris@0 701 *
Chris@0 702 * Determines the order in which BigPipe placeholders must be replaced.
Chris@0 703 *
Chris@0 704 * @param string $html
Chris@0 705 * HTML markup.
Chris@0 706 * @param array $placeholders
Chris@0 707 * Associative array; the BigPipe placeholders. Keys are the BigPipe
Chris@0 708 * placeholder IDs.
Chris@0 709 *
Chris@0 710 * @return array
Chris@0 711 * Indexed array; the order in which the BigPipe placeholders must be sent.
Chris@0 712 * Values are the BigPipe placeholder IDs. Note that only unique
Chris@0 713 * placeholders are kept: if the same placeholder occurs multiple times, we
Chris@0 714 * only keep the first occurrence.
Chris@0 715 */
Chris@0 716 protected function getPlaceholderOrder($html, $placeholders) {
Chris@0 717 $fragments = explode('<span data-big-pipe-placeholder-id="', $html);
Chris@0 718 array_shift($fragments);
Chris@0 719 $placeholder_ids = [];
Chris@0 720
Chris@0 721 foreach ($fragments as $fragment) {
Chris@0 722 $t = explode('"></span>', $fragment, 2);
Chris@0 723 $placeholder_id = $t[0];
Chris@0 724 $placeholder_ids[] = $placeholder_id;
Chris@0 725 }
Chris@0 726 $placeholder_ids = array_unique($placeholder_ids);
Chris@0 727
Chris@0 728 // The 'status messages' placeholder needs to be special cased, because it
Chris@0 729 // depends on global state that can be modified when other placeholders are
Chris@0 730 // being rendered: any code can add messages to render.
Chris@0 731 // This violates the principle that each lazy builder must be able to render
Chris@0 732 // itself in isolation, and therefore in any order. However, we cannot
Chris@17 733 // change the way \Drupal\Core\Messenger\MessengerInterface::addMessage()
Chris@17 734 // works in the Drupal 8 cycle. So we have to accommodate its special needs.
Chris@0 735 // Allowing placeholders to be rendered in a particular order (in this case:
Chris@0 736 // last) would violate this isolation principle. Thus a monopoly is granted
Chris@0 737 // to this one special case, with this hard-coded solution.
Chris@0 738 // @see \Drupal\Core\Render\Element\StatusMessages
Chris@0 739 // @see \Drupal\Core\Render\Renderer::replacePlaceholders()
Chris@0 740 // @see https://www.drupal.org/node/2712935#comment-11368923
Chris@0 741 $message_placeholder_ids = [];
Chris@0 742 foreach ($placeholders as $placeholder_id => $placeholder_element) {
Chris@0 743 if (isset($placeholder_element['#lazy_builder']) && $placeholder_element['#lazy_builder'][0] === 'Drupal\Core\Render\Element\StatusMessages::renderMessages') {
Chris@0 744 $message_placeholder_ids[] = $placeholder_id;
Chris@0 745 }
Chris@0 746 }
Chris@0 747
Chris@0 748 // Return placeholder IDs in DOM order, but with the 'status messages'
Chris@0 749 // placeholders at the end, if they are present.
Chris@0 750 $ordered_placeholder_ids = array_merge(
Chris@0 751 array_diff($placeholder_ids, $message_placeholder_ids),
Chris@0 752 array_intersect($placeholder_ids, $message_placeholder_ids)
Chris@0 753 );
Chris@0 754 return $ordered_placeholder_ids;
Chris@0 755 }
Chris@0 756
Chris@0 757 }