clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php
namespace Drupal\big_pipe\Controller;
use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Routing\LocalRedirectResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* Returns responses for BigPipe module routes.
*/
class BigPipeController {
/**
* Sets a BigPipe no-JS cookie, redirects back to the original location.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return \Drupal\Core\Routing\LocalRedirectResponse
* A response that sets the no-JS cookie and redirects back to the original
* location.
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* Thrown when the no-JS cookie is already set or when there is no session.
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Thrown when the original location is missing, i.e. when no 'destination'
* query argument is set.
*
* @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
*/
public function setNoJsCookie(Request $request) {
// This controller may only be accessed when the browser does not support
// JavaScript. It is accessed automatically when that's the case thanks to
// big_pipe_page_attachments(). When this controller is executed, deny
// access when either:
// - the no-JS cookie is already set: this indicates a redirect loop, since
// the cookie was already set, yet the user is executing this controller;
// - there is no session, in which case BigPipe is not enabled anyway, so it
// is pointless to set this cookie.
if ($request->cookies->has(BigPipeStrategy::NOJS_COOKIE)) {
throw new AccessDeniedHttpException();
}
if (!$request->query->has('destination')) {
throw new HttpException(400, 'The original location is missing.');
}
$response = new LocalRedirectResponse($request->query->get('destination'));
// Set cookie without httpOnly, so that JavaScript can delete it.
$response->headers->setCookie(new Cookie(BigPipeStrategy::NOJS_COOKIE, TRUE, 0, '/', NULL, FALSE, FALSE, FALSE, NULL));
$response->addCacheableDependency((new CacheableMetadata())->addCacheContexts(['cookies:' . BigPipeStrategy::NOJS_COOKIE, 'session.exists']));
return $response;
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace Drupal\big_pipe\EventSubscriber;
use Drupal\Core\Render\HtmlResponse;
use Drupal\big_pipe\Render\BigPipe;
use Drupal\big_pipe\Render\BigPipeResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Response subscriber to replace the HtmlResponse with a BigPipeResponse.
*
* @see \Drupal\big_pipe\Render\BigPipe
*
* @todo Refactor once https://www.drupal.org/node/2577631 lands.
*/
class HtmlResponseBigPipeSubscriber implements EventSubscriberInterface {
/**
* The BigPipe service.
*
* @var \Drupal\big_pipe\Render\BigPipe
*/
protected $bigPipe;
/**
* Constructs a HtmlResponseBigPipeSubscriber object.
*
* @param \Drupal\big_pipe\Render\BigPipe $big_pipe
* The BigPipe service.
*/
public function __construct(BigPipe $big_pipe) {
$this->bigPipe = $big_pipe;
}
/**
* Adds markers to the response necessary for the BigPipe render strategy.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The event to process.
*/
public function onRespondEarly(ResponseEvent $event) {
$response = $event->getResponse();
if (!$response instanceof HtmlResponse) {
return;
}
// Wrap the scripts_bottom placeholder with a marker before and after,
// because \Drupal\big_pipe\Render\BigPipe needs to be able to extract that
// markup if there are no-JS BigPipe placeholders.
// @see \Drupal\big_pipe\Render\BigPipe::sendPreBody()
$attachments = $response->getAttachments();
if (isset($attachments['html_response_attachment_placeholders']['scripts_bottom'])) {
$scripts_bottom_placeholder = $attachments['html_response_attachment_placeholders']['scripts_bottom'];
$content = $response->getContent();
$content = str_replace($scripts_bottom_placeholder, '<drupal-big-pipe-scripts-bottom-marker>' . $scripts_bottom_placeholder . '<drupal-big-pipe-scripts-bottom-marker>', $content);
$response->setContent($content);
}
}
/**
* Transforms a HtmlResponse to a BigPipeResponse.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The event to process.
*/
public function onRespond(ResponseEvent $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);
}
/**
* Returns the BigPipe service to use to send the current response.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* A response event.
*
* @return \Drupal\big_pipe\Render\BigPipe
* The BigPipe service.
*/
protected function getBigPipeService(ResponseEvent $event) {
return $this->bigPipe;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
// Run after HtmlResponsePlaceholderStrategySubscriber (priority 5), i.e.
// after BigPipeStrategy has been applied, but before normal (priority 0)
// response subscribers have been applied, because by then it'll be too late
// to transform it into a BigPipeResponse.
$events[KernelEvents::RESPONSE][] = ['onRespondEarly', 3];
// Run as the last possible subscriber.
$events[KernelEvents::RESPONSE][] = ['onRespond', -10000];
return $events;
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Drupal\big_pipe\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteBuildEvent;
/**
* Sets the '_no_big_pipe' option on select routes.
*/
class NoBigPipeRouteAlterSubscriber implements EventSubscriberInterface {
/**
* Alters select routes to have the '_no_big_pipe' option.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The event to process.
*/
public function onRoutingRouteAlterSetNoBigPipe(RouteBuildEvent $event) {
$no_big_pipe_routes = [
// The batch system uses a <meta> refresh to work without JavaScript.
'system.batch_page.html',
// When a user would install the BigPipe module using a browser and with
// JavaScript disabled, the first response contains the status messages
// for installing a module, but then the BigPipe no-JS redirect occurs,
// which then causes the user to not see those status messages.
// @see https://www.drupal.org/node/2469431#comment-10901944
'system.modules_list',
];
$route_collection = $event->getRouteCollection();
foreach ($no_big_pipe_routes as $excluded_route) {
if ($route = $route_collection->get($excluded_route)) {
$route->setOption('_no_big_pipe', TRUE);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events[RoutingEvents::ALTER][] = ['onRoutingRouteAlterSetNoBigPipe'];
return $events;
}
}

View File

@@ -0,0 +1,764 @@
<?php
namespace Drupal\big_pipe\Render;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\MessageCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\RendererInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Service for sending an HTML response in chunks (to get faster page loads).
*
* At a high level, BigPipe sends an HTML response in chunks:
* 1. one chunk: everything until just before </body> this contains BigPipe
* placeholders for the personalized parts of the page. Hence this sends the
* non-personalized parts of the page. Let's call it The Skeleton.
* 2. N chunks: a <script> tag per BigPipe placeholder in The Skeleton.
* 3. one chunk: </body> and everything after it.
*
* This is conceptually identical to Facebook's BigPipe (hence the name).
*
* @see https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919
*
* The major way in which Drupal differs from Facebook's implementation (and
* others) is in its ability to automatically figure out which parts of the page
* can benefit from BigPipe-style delivery. Drupal's render system has the
* concept of "auto-placeholdering": content that is too dynamic is replaced
* with a placeholder that can then be rendered at a later time. On top of that,
* it also has the concept of "placeholder strategies": by default, placeholders
* are replaced on the server side and the response is blocked on all of them
* being replaced. But it's possible to add additional placeholder strategies.
* BigPipe is just another placeholder strategy. Others could be ESI, AJAX
*
* @see https://www.drupal.org/developing/api/8/render/arrays/cacheability/auto-placeholdering
* @see \Drupal\Core\Render\PlaceholderGeneratorInterface::shouldAutomaticallyPlaceholder()
* @see \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface
* @see \Drupal\Core\Render\Placeholder\SingleFlushStrategy
* @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
*
* There is also one noteworthy technical addition that Drupal makes. BigPipe as
* described above, and as implemented by Facebook, can only work if JavaScript
* is enabled. The BigPipe module also makes it possible to replace placeholders
* using BigPipe in-situ, without JavaScript. This is not technically BigPipe at
* all; it's just the use of multiple flushes. Since it is able to reuse much of
* the logic though, we choose to call this "no-JS BigPipe".
*
* However, there is also a tangible benefit: some dynamic/expensive content is
* not HTML, but for example an HTML attribute value (or part thereof). It's not
* possible to efficiently replace such content using JavaScript, so "classic"
* BigPipe is out of the question. For example: CSRF tokens in URLs.
*
* This allows us to use both no-JS BigPipe and "classic" BigPipe in the same
* response to maximize the amount of content we can send as early as possible.
*
* Finally, a closer look at the implementation, and how it supports and reuses
* existing Drupal concepts:
* 1. BigPipe placeholders: 1 HtmlResponse + N embedded AjaxResponses.
* - Before a BigPipe response is sent, it is just an HTML response that
* contains BigPipe placeholders. Those placeholders look like
* <span data-big-pipe-placeholder-id=""></span>. JavaScript is used to
* replace those placeholders.
* Therefore these placeholders are actually sent to the client.
* - The Skeleton of course has attachments, including most notably asset
* libraries. And those we track in drupalSettings.ajaxPageState.libraries
* so that when we load new content through AJAX, we don't load the same
* asset libraries again. An HTML page can have multiple AJAX responses,
* each of which should take into account the combined AJAX page state of
* the HTML document and all preceding AJAX responses.
* - BigPipe does not make use of multiple AJAX requests/responses. It uses a
* single HTML response. But it is a more long-lived one: The Skeleton is
* sent first, the closing </body> tag is not yet sent, and the connection
* is kept open. Whenever another BigPipe Placeholder is rendered, Drupal
* sends (and so actually appends to the already-sent HTML) something like
* <script type="application/vnd.drupal-ajax">[{"command":"settings","settings":{}}, {"command":}.
* - So, for every BigPipe placeholder, we send such a <script
* type="application/vnd.drupal-ajax"> tag. And the contents of that tag is
* exactly like an AJAX response. The BigPipe module has JavaScript that
* listens for these and applies them. Let's call it an Embedded AJAX
* Response (since it is embedded in the HTML response). Now for the
* interesting bit: each of those Embedded AJAX Responses must also take
* into account the cumulative AJAX page state of the HTML document and all
* preceding Embedded AJAX responses.
* 2. No-JS BigPipe placeholders: 1 HtmlResponse + N embedded HtmlResponses.
* - Before a BigPipe response is sent, it is just a HTML response that
* contains no-JS BigPipe placeholders. Those placeholders can take two
* different forms:
* 1. <span data-big-pipe-nojs-placeholder-id=""></span> if it's a
* placeholder that will be replaced by HTML
* 2. big_pipe_nojs_placeholder_attribute_safe: if it's a placeholder
* inside an HTML attribute, in which 1. would be invalid (angle brackets
* are not allowed inside HTML attributes)
* No-JS BigPipe placeholders are not replaced using JavaScript, they must
* be replaced upon sending the BigPipe response. So, while the response is
* being sent, upon encountering these placeholders, their corresponding
* placeholder replacements are sent instead.
* Therefore these placeholders are never actually sent to the client.
* - See second bullet of point 1.
* - No-JS BigPipe does not use multiple AJAX requests/responses. It uses a
* single HTML response. But it is a more long-lived one: The Skeleton is
* split into multiple parts, the separators are where the no-JS BigPipe
* placeholders used to be. Whenever another no-JS BigPipe placeholder is
* rendered, Drupal sends (and so actually appends to the already-sent HTML)
* something like
* <link rel="stylesheet" ><script ><content>.
* - So, for every no-JS BigPipe placeholder, we send its associated CSS and
* header JS that has not already been sent (the bottom JS is not yet sent,
* so we can accumulate all of it and send it together at the end). This
* ensures that the markup is rendered as it was originally intended: its
* CSS and JS used to be blocking, and it still is. Let's call it an
* Embedded HTML response. Each of those Embedded HTML Responses must also
* take into account the cumulative AJAX page state of the HTML document and
* all preceding Embedded HTML responses.
* - Finally: any non-critical JavaScript associated with all Embedded HTML
* Responses, i.e. any footer/bottom/non-header JavaScript, is loaded after
* The Skeleton.
*
* Combining all of the above, when using both BigPipe placeholders and no-JS
* BigPipe placeholders, we therefore send: 1 HtmlResponse + M Embedded HTML
* Responses + N Embedded AJAX Responses. Schematically, we send these chunks:
* 1. Byte zero until 1st no-JS placeholder: headers + <html><head /><span></span>
* 2. 1st no-JS placeholder replacement: <link rel="stylesheet" ><script ><content>
* 3. Content until 2nd no-JS placeholder: <span></span>
* 4. 2nd no-JS placeholder replacement: <link rel="stylesheet" ><script ><content>
* 5. Content until 3rd no-JS placeholder: <span></span>
* 6. [ repeat until all no-JS placeholder replacements are sent ]
* 7. Send content after last no-JS placeholder.
* 8. Send script_bottom (markup to load bottom i.e. non-critical JS).
* 9. 1st placeholder replacement: <script type="application/vnd.drupal-ajax">[{"command":"settings","settings":{}}, {"command":}
* 10. 2nd placeholder replacement: <script type="application/vnd.drupal-ajax">[{"command":"settings","settings":{}}, {"command":}
* 11. [ repeat until all placeholder replacements are sent ]
* 12. Send </body> and everything after it.
* 13. Terminate request/response cycle.
*
* @see \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber
* @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
*/
class BigPipe {
/**
* The BigPipe placeholder replacements start signal.
*
* @var string
*/
const START_SIGNAL = '<script type="application/vnd.drupal-ajax" data-big-pipe-event="start"></script>';
/**
* The BigPipe placeholder replacements stop signal.
*
* @var string
*/
const STOP_SIGNAL = '<script type="application/vnd.drupal-ajax" data-big-pipe-event="stop"></script>';
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The session.
*
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The HTTP kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* The event dispatcher.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
public function __construct(RendererInterface $renderer, SessionInterface $session, RequestStack $request_stack, HttpKernelInterface $http_kernel, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory, protected ?MessengerInterface $messenger = NULL) {
$this->renderer = $renderer;
$this->session = $session;
$this->requestStack = $request_stack;
$this->httpKernel = $http_kernel;
$this->eventDispatcher = $event_dispatcher;
$this->configFactory = $config_factory;
if (!isset($this->messenger)) {
@trigger_error('Calling ' . __CLASS__ . '::_construct() without the $messenger argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3343754', E_USER_DEPRECATED);
$this->messenger = \Drupal::messenger();
}
}
/**
* Performs tasks after sending content (and rendering placeholders).
*/
protected function performPostSendTasks() {
// Close the session again.
$this->session->save();
}
/**
* Sends a chunk.
*
* @param string|\Drupal\Core\Render\HtmlResponse $chunk
* The string or response to append. String if there's no cacheability
* metadata or attachments to merge.
*/
protected function sendChunk($chunk) {
assert(is_string($chunk) || $chunk instanceof HtmlResponse);
if ($chunk instanceof HtmlResponse) {
print $chunk->getContent();
}
else {
print $chunk;
}
flush();
}
/**
* Sends an HTML response in chunks using the BigPipe technique.
*
* @param \Drupal\big_pipe\Render\BigPipeResponse $response
* The BigPipe response to send.
*
* @internal
* This method should only be invoked by
* \Drupal\big_pipe\Render\BigPipeResponse, which is itself an internal
* class.
*/
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']);
// 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();
}
/**
* Sends everything until just before </body>.
*
* @param string $pre_body
* The HTML response's content until the closing </body> tag.
* @param array $no_js_placeholders
* The no-JS BigPipe placeholders.
* @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets
* The cumulative assets sent so far; to be updated while rendering no-JS
* BigPipe placeholders.
*/
protected function sendPreBody($pre_body, array $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) {
// If there are no no-JS BigPipe placeholders, we can send the pre-</body>
// part of the page immediately.
if (empty($no_js_placeholders)) {
$this->sendChunk($pre_body);
return;
}
// Extract the scripts_bottom markup: the no-JS BigPipe placeholders that we
// will render may attach additional asset libraries, and if so, it will be
// necessary to re-render scripts_bottom.
[$pre_scripts_bottom, $scripts_bottom, $post_scripts_bottom] = explode('<drupal-big-pipe-scripts-bottom-marker>', $pre_body, 3);
$cumulative_assets_initial = clone $cumulative_assets;
$this->sendNoJsPlaceholders($pre_scripts_bottom . $post_scripts_bottom, $no_js_placeholders, $cumulative_assets);
// If additional asset libraries or drupalSettings were attached by any of
// the placeholders, then we need to re-render scripts_bottom.
if ($cumulative_assets_initial != $cumulative_assets) {
// Create a new HtmlResponse. Ensure the CSS and (non-bottom) JS is sent
// before the HTML they're associated with.
// @see \Drupal\Core\Render\HtmlResponseSubscriber
// @see template_preprocess_html()
$js_bottom_placeholder = '<nojs-bigpipe-placeholder-scripts-bottom-placeholder token="' . Crypt::randomBytesBase64(55) . '">';
$html_response = new HtmlResponse();
$html_response->setContent([
'#markup' => BigPipeMarkup::create($js_bottom_placeholder),
'#attached' => [
'drupalSettings' => $cumulative_assets->getSettings(),
'library' => $cumulative_assets->getAlreadyLoadedLibraries(),
'html_response_attachment_placeholders' => [
'scripts_bottom' => $js_bottom_placeholder,
],
],
]);
$html_response->getCacheableMetadata()->setCacheMaxAge(0);
// Push a fake request with the asset libraries loaded so far and dispatch
// KernelEvents::RESPONSE event. This results in the attachments for the
// HTML response being processed by HtmlResponseAttachmentsProcessor and
// hence the HTML to load the bottom JavaScript can be rendered.
$fake_request = $this->requestStack->getMainRequest()->duplicate();
$html_response = $this->filterEmbeddedResponse($fake_request, $html_response);
$scripts_bottom = $html_response->getContent();
}
$this->sendChunk($scripts_bottom);
}
/**
* Sends no-JS BigPipe placeholders' replacements as embedded HTML responses.
*
* @param string $html
* HTML markup.
* @param array $no_js_placeholders
* Associative array; the no-JS BigPipe placeholders. Keys are the BigPipe
* selectors.
* @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets
* The cumulative assets sent so far; to be updated while rendering no-JS
* BigPipe placeholders.
*
* @throws \Exception
* If an exception is thrown during the rendering of a placeholder, it is
* caught to allow the other placeholders to still be replaced. But when
* error logging is configured to be verbose, the exception is rethrown to
* simplify debugging.
*/
protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAssetsInterface $cumulative_assets) {
// Split the HTML on every no-JS placeholder string.
$placeholder_strings = array_keys($no_js_placeholders);
$fragments = static::splitHtmlOnPlaceholders($html, $placeholder_strings);
// Determine how many occurrences there are of each no-JS placeholder.
$placeholder_occurrences = array_count_values(array_intersect($fragments, $placeholder_strings));
// Set up a variable to store the content of placeholders that have multiple
// occurrences.
$multi_occurrence_placeholders_content = [];
foreach ($fragments as $fragment) {
// If the fragment isn't one of the no-JS placeholders, it is the HTML in
// between placeholders and it must be printed & flushed immediately. The
// rest of the logic in the loop handles the placeholders.
if (!isset($no_js_placeholders[$fragment])) {
$this->sendChunk($fragment);
continue;
}
// If there are multiple occurrences of this particular placeholder, and
// this is the second occurrence, we can skip all calculations and just
// send the same content.
if ($placeholder_occurrences[$fragment] > 1 && isset($multi_occurrence_placeholders_content[$fragment])) {
$this->sendChunk($multi_occurrence_placeholders_content[$fragment]);
continue;
}
$placeholder = $fragment;
assert(isset($no_js_placeholders[$placeholder]));
$token = Crypt::randomBytesBase64(55);
// Render the placeholder, but include the cumulative settings assets, so
// we can calculate the overall settings for the entire page.
$placeholder_plus_cumulative_settings = [
'placeholder' => $no_js_placeholders[$placeholder],
'cumulative_settings_' . $token => [
'#attached' => [
'drupalSettings' => $cumulative_assets->getSettings(),
],
],
];
try {
$elements = $this->renderPlaceholder($placeholder, $placeholder_plus_cumulative_settings);
}
catch (\Exception $e) {
if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
throw $e;
}
else {
trigger_error($e, E_USER_ERROR);
continue;
}
}
// Create a new HtmlResponse. Ensure the CSS and (non-bottom) JS is sent
// before the HTML they're associated with. In other words: ensure the
// critical assets for this placeholder's markup are loaded first.
// @see \Drupal\Core\Render\HtmlResponseSubscriber
// @see template_preprocess_html()
$css_placeholder = '<nojs-bigpipe-placeholder-styles-placeholder token="' . $token . '">';
$js_placeholder = '<nojs-bigpipe-placeholder-scripts-placeholder token="' . $token . '">';
$elements['#markup'] = BigPipeMarkup::create($css_placeholder . $js_placeholder . (string) $elements['#markup']);
$elements['#attached']['html_response_attachment_placeholders']['styles'] = $css_placeholder;
$elements['#attached']['html_response_attachment_placeholders']['scripts'] = $js_placeholder;
$html_response = new HtmlResponse();
$html_response->setContent($elements);
$html_response->getCacheableMetadata()->setCacheMaxAge(0);
// Push a fake request with the asset libraries loaded so far and dispatch
// KernelEvents::RESPONSE event. This results in the attachments for the
// HTML response being processed by HtmlResponseAttachmentsProcessor and
// hence:
// - the HTML to load the CSS can be rendered.
// - the HTML to load the JS (at the top) can be rendered.
$fake_request = $this->requestStack->getMainRequest()->duplicate();
$fake_request->query->set('ajax_page_state', ['libraries' => implode(',', $cumulative_assets->getAlreadyLoadedLibraries())]);
try {
$html_response = $this->filterEmbeddedResponse($fake_request, $html_response);
}
catch (\Exception $e) {
if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
throw $e;
}
else {
trigger_error($e, E_USER_ERROR);
continue;
}
}
// Send this embedded HTML response.
$this->sendChunk($html_response);
// Another placeholder was rendered and sent, track the set of asset
// libraries sent so far. Any new settings also need to be tracked, so
// they can be sent in ::sendPreBody().
$cumulative_assets->setAlreadyLoadedLibraries(array_merge($cumulative_assets->getAlreadyLoadedLibraries(), $html_response->getAttachments()['library']));
$cumulative_assets->setSettings($html_response->getAttachments()['drupalSettings']);
// If there are multiple occurrences of this particular placeholder, track
// the content that was sent, so we can skip all calculations for the next
// occurrence.
if ($placeholder_occurrences[$fragment] > 1) {
$multi_occurrence_placeholders_content[$fragment] = $html_response->getContent();
}
}
}
/**
* Sends BigPipe placeholders' replacements as embedded AJAX responses.
*
* @param array $placeholders
* Associative array; the BigPipe placeholders. Keys are the BigPipe
* placeholder IDs.
* @param array $placeholder_order
* Indexed array; the order in which the BigPipe placeholders must be sent.
* Values are the BigPipe placeholder IDs. (These values correspond to keys
* in $placeholders.)
* @param \Drupal\Core\Asset\AttachedAssetsInterface $cumulative_assets
* The cumulative assets sent so far; to be updated while rendering BigPipe
* placeholders.
*
* @throws \Exception
* If an exception is thrown during the rendering of a placeholder, it is
* caught to allow the other placeholders to still be replaced. But when
* error logging is configured to be verbose, the exception is rethrown to
* simplify debugging.
*/
protected function sendPlaceholders(array $placeholders, array $placeholder_order, AttachedAssetsInterface $cumulative_assets) {
// Return early if there are no BigPipe placeholders to send.
if (empty($placeholders)) {
return;
}
// Send the start signal.
$this->sendChunk("\n" . static::START_SIGNAL . "\n");
// A BigPipe response consists of an HTML response plus multiple embedded
// AJAX responses. To process the attachments of those AJAX responses, we
// need a fake request that is identical to the main request, but with
// one change: it must have the right Accept header, otherwise the work-
// around for a bug in IE9 will cause not JSON, but <textarea>-wrapped JSON
// to be returned.
// @see \Drupal\Core\EventSubscriber\AjaxResponseSubscriber::onResponse()
$fake_request = $this->requestStack->getMainRequest()->duplicate();
$fake_request->headers->set('Accept', 'application/vnd.drupal-ajax');
// Create a Fiber for each placeholder.
$fibers = [];
foreach ($placeholder_order as $placeholder_id) {
if (!isset($placeholders[$placeholder_id])) {
continue;
}
$placeholder_render_array = $placeholders[$placeholder_id];
$fibers[$placeholder_id] = new \Fiber(fn() => $this->renderPlaceholder($placeholder_id, $placeholder_render_array));
}
$iterations = 0;
while (count($fibers) > 0) {
foreach ($fibers as $placeholder_id => $fiber) {
try {
if (!$fiber->isStarted()) {
$fiber->start();
}
elseif ($fiber->isSuspended()) {
$fiber->resume();
}
// If the Fiber hasn't terminated by this point, move onto the next
// placeholder, we'll resume this Fiber again when we get back here.
if (!$fiber->isTerminated()) {
// If we've gone through the placeholders once already, and they're
// still not finished, then start to allow code higher up the stack
// to get on with something else.
if ($iterations) {
$fiber = \Fiber::getCurrent();
if ($fiber !== NULL) {
$fiber->suspend();
}
}
continue;
}
$elements = $fiber->getReturn();
unset($fibers[$placeholder_id]);
// Create a new AjaxResponse.
$ajax_response = new AjaxResponse();
// JavaScript's querySelector automatically decodes HTML entities in
// attributes, so we must decode the entities of the current BigPipe
// placeholder ID (which has HTML entities encoded since we use it to
// find the placeholders).
$big_pipe_js_placeholder_id = Html::decodeEntities($placeholder_id);
$ajax_response->addCommand(new ReplaceCommand(sprintf('[data-big-pipe-placeholder-id="%s"]', $big_pipe_js_placeholder_id), $elements['#markup']));
$ajax_response->setAttachments($elements['#attached']);
// Delete all messages that were generated during the rendering of this
// placeholder, to render them in a BigPipe-optimized way.
$messages = $this->messenger->deleteAll();
foreach ($messages as $type => $type_messages) {
foreach ($type_messages as $message) {
$ajax_response->addCommand(new MessageCommand($message, NULL, ['type' => $type], FALSE));
}
}
// Push a fake request with the asset libraries loaded so far and
// dispatch KernelEvents::RESPONSE event. This results in the
// attachments for the AJAX response being processed by
// AjaxResponseAttachmentsProcessor and hence:
// - the necessary AJAX commands to load the necessary missing asset
// libraries and updated AJAX page state are added to the AJAX
// response
// - the attachments associated with the response are finalized,
// which allows us to track the total set of asset libraries sent in
// the initial HTML response plus all embedded AJAX responses sent so
// far.
$fake_request->query->set('ajax_page_state', ['libraries' => implode(',', $cumulative_assets->getAlreadyLoadedLibraries())] + $cumulative_assets->getSettings()['ajaxPageState']);
$ajax_response = $this->filterEmbeddedResponse($fake_request, $ajax_response);
// Send this embedded AJAX response.
$json = $ajax_response->getContent();
$output = <<<EOF
<script type="application/vnd.drupal-ajax" data-big-pipe-replacement-for-placeholder-with-id="$placeholder_id">
$json
</script>
EOF;
$this->sendChunk($output);
// Another placeholder was rendered and sent, track the set of asset
// libraries sent so far. Any new settings are already sent; we
// don't need to track those.
if (isset($ajax_response->getAttachments()['drupalSettings']['ajaxPageState']['libraries'])) {
$cumulative_assets->setAlreadyLoadedLibraries(explode(',', $ajax_response->getAttachments()['drupalSettings']['ajaxPageState']['libraries']));
}
}
catch (\Exception $e) {
unset($fibers[$placeholder_id]);
if ($this->configFactory->get('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE) {
throw $e;
}
else {
trigger_error($e, E_USER_ERROR);
}
}
}
$iterations++;
}
// Send the stop signal.
$this->sendChunk("\n" . static::STOP_SIGNAL . "\n");
}
/**
* Filters the given embedded response, using the cumulative AJAX page state.
*
* @param \Symfony\Component\HttpFoundation\Request $fake_request
* A fake subrequest that contains the cumulative AJAX page state of the
* HTML document and all preceding Embedded HTML or AJAX responses.
* @param \Symfony\Component\HttpFoundation\Response|\Drupal\Core\Render\HtmlResponse|\Drupal\Core\Ajax\AjaxResponse $embedded_response
* Either an HTML response or an AJAX response that will be embedded in the
* overall HTML response.
*
* @return \Symfony\Component\HttpFoundation\Response
* The filtered response, which will load only the assets that $fake_request
* did not indicate to already have been loaded, plus the updated cumulative
* AJAX page state.
*/
protected function filterEmbeddedResponse(Request $fake_request, Response $embedded_response) {
assert($embedded_response instanceof HtmlResponse || $embedded_response instanceof AjaxResponse);
return $this->filterResponse($fake_request, HttpKernelInterface::SUB_REQUEST, $embedded_response);
}
/**
* Filters the given response.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request for which a response is being sent.
* @param int $request_type
* The request type. Can either be
* \Symfony\Component\HttpKernel\HttpKernelInterface::MAIN_REQUEST or
* \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST.
* @param \Symfony\Component\HttpFoundation\Response $response
* The response to filter.
*
* @return \Symfony\Component\HttpFoundation\Response
* The filtered response.
*/
protected function filterResponse(Request $request, $request_type, Response $response) {
assert($request_type === HttpKernelInterface::MAIN_REQUEST || $request_type === HttpKernelInterface::SUB_REQUEST);
$this->requestStack->push($request);
$event = new ResponseEvent($this->httpKernel, $request, $request_type, $response);
$this->eventDispatcher->dispatch($event, KernelEvents::RESPONSE);
$filtered_response = $event->getResponse();
$this->requestStack->pop();
return $filtered_response;
}
/**
* Sends </body> and everything after it.
*
* @param string $post_body
* The HTML response's content after the closing </body> tag.
*/
protected function sendPostBody($post_body) {
$this->sendChunk('</body>' . $post_body);
}
/**
* Renders a placeholder, and just that placeholder.
*
* BigPipe renders placeholders independently of the rest of the content, so
* it needs to be able to render placeholders by themselves.
*
* @param string $placeholder
* The placeholder to render.
* @param array $placeholder_render_array
* The render array associated with that placeholder.
*
* @return array
* The render array representing the rendered placeholder.
*
* @see \Drupal\Core\Render\RendererInterface::renderPlaceholder()
*/
protected function renderPlaceholder($placeholder, array $placeholder_render_array) {
$elements = [
'#markup' => $placeholder,
'#attached' => [
'placeholders' => [
$placeholder => $placeholder_render_array,
],
],
];
return $this->renderer->renderPlaceholder($placeholder, $elements);
}
/**
* Gets the BigPipe placeholder order.
*
* Determines the order in which BigPipe placeholders are executed. It is
* safe to use a regular expression here as the HTML is statically created in
* \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy::createBigPipeJsPlaceholder().
*
* @param string $html
* HTML markup.
* @param array $placeholders
* Associative array; the BigPipe placeholders. Keys are the BigPipe
* placeholder IDs.
*
* @return array
* Indexed array; the order in which the BigPipe placeholders will start
* execution. Placeholders begin execution in DOM order. Note that due to
* the Fibers implementation of BigPipe, although placeholders will start
* executing in DOM order, they may finish and render in any order. Values
* are the BigPipe placeholder IDs. Note that only unique placeholders are
* kept: if the same placeholder occurs multiple times, we only keep the
* first occurrence.
*/
protected function getPlaceholderOrder($html, $placeholders) {
if (preg_match_all('/<span data-big-pipe-placeholder-id="([^"]*)">/', $html, $matches)) {
return array_unique($matches[1]);
}
return [];
}
/**
* Splits an HTML string into fragments.
*
* Creates an array of HTML fragments, separated by placeholders. The result
* includes the placeholders themselves. The original order is respected.
*
* @param string $html_string
* The HTML to split.
* @param string[] $html_placeholders
* The HTML placeholders to split on.
*
* @return string[]
* The resulting HTML fragments.
*/
private static function splitHtmlOnPlaceholders($html_string, array $html_placeholders) {
$prepare_for_preg_split = function ($placeholder_string) {
return '(' . preg_quote($placeholder_string, '/') . ')';
};
$preg_placeholder_strings = array_map($prepare_for_preg_split, $html_placeholders);
$pattern = '/' . implode('|', $preg_placeholder_strings) . '/';
if (strlen($pattern) < 31000) {
// Only small (<31K characters) patterns can be handled by preg_split().
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$result = preg_split($pattern, $html_string, 0, $flags);
}
else {
// For large amounts of placeholders we use a simpler but slower approach.
foreach ($html_placeholders as $placeholder) {
$html_string = str_replace($placeholder, "\x1F" . $placeholder . "\x1F", $html_string);
}
$result = array_filter(explode("\x1F", $html_string));
}
return $result;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Drupal\big_pipe\Render;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Render\MarkupTrait;
/**
* Defines an object that passes safe strings through BigPipe's render pipeline.
*
* This object should only be constructed with a known safe string. If there is
* any risk that the string contains user-entered data that has not been
* filtered first, it must not be used.
*
* @internal
* This object is marked as internal because it should only be used in the
* BigPipe render pipeline.
*
* @see \Drupal\Core\Render\Markup
*/
final class BigPipeMarkup implements MarkupInterface, \Countable {
use MarkupTrait;
}

View File

@@ -0,0 +1,123 @@
<?php
namespace Drupal\big_pipe\Render;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Session\ResponseKeepSessionOpenInterface;
/**
* A response that is sent in chunks by the BigPipe service.
*
* Note we cannot use \Symfony\Component\HttpFoundation\StreamedResponse because
* it makes the content inaccessible (hidden behind a callback), which means no
* middlewares are able to modify the content anymore.
*
* @see \Drupal\big_pipe\Render\BigPipe
*
* @internal
* This is a temporary solution until a generic response emitter interface is
* created in https://www.drupal.org/node/2577631. Only code internal to
* BigPipe should instantiate or type hint to this class.
*/
class BigPipeResponse extends HtmlResponse implements ResponseKeepSessionOpenInterface {
/**
* The BigPipe service.
*
* @var \Drupal\big_pipe\Render\BigPipe
*/
protected $bigPipe;
/**
* The original HTML response.
*
* Still contains placeholders. Its cacheability metadata and attachments are
* for everything except the placeholders (since those are not yet rendered).
*
* @see \Drupal\Core\Render\StreamedResponseInterface
* @see ::getStreamedResponse()
*
* @var \Drupal\Core\Render\HtmlResponse
*/
protected $originalHtmlResponse;
/**
* Constructs a new BigPipeResponse.
*
* @param \Drupal\Core\Render\HtmlResponse $response
* The original HTML response.
*/
public function __construct(HtmlResponse $response) {
parent::__construct('', $response->getStatusCode(), []);
$this->originalHtmlResponse = $response;
$this->populateBasedOnOriginalHtmlResponse();
}
/**
* Returns the original HTML response.
*
* @return \Drupal\Core\Render\HtmlResponse
* The original HTML response.
*/
public function getOriginalHtmlResponse() {
return $this->originalHtmlResponse;
}
/**
* Populates this BigPipeResponse object based on the original HTML response.
*/
protected function populateBasedOnOriginalHtmlResponse() {
// Clone the HtmlResponse's data into the new BigPipeResponse.
$this->headers = clone $this->originalHtmlResponse->headers;
$this
->setStatusCode($this->originalHtmlResponse->getStatusCode())
->setContent($this->originalHtmlResponse->getContent())
->setAttachments($this->originalHtmlResponse->getAttachments())
->addCacheableDependency($this->originalHtmlResponse->getCacheableMetadata());
// A BigPipe response can never be cached, because it is intended for a
// single user.
// @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1
$this->setPrivate();
// Inform surrogates how they should handle BigPipe responses:
// - "no-store" specifies that the response should not be stored in cache;
// it is only to be used for the original request
// - "content" identifies what processing surrogates should perform on the
// response before forwarding it. We send, "BigPipe/1.0", which surrogates
// should not process at all, and in fact, they should not even buffer it
// at all.
// @see http://www.w3.org/TR/edge-arch/
$this->headers->set('Surrogate-Control', 'no-store, content="BigPipe/1.0"');
// Add header to support streaming on NGINX + php-fpm (nginx >= 1.5.6).
$this->headers->set('X-Accel-Buffering', 'no');
}
/**
* Sets the BigPipe service to use.
*
* @param \Drupal\big_pipe\Render\BigPipe $big_pipe
* The BigPipe service.
*/
public function setBigPipeService(BigPipe $big_pipe) {
$this->bigPipe = $big_pipe;
}
/**
* {@inheritdoc}
*/
public function sendContent(): static {
$this->bigPipe->sendContent($this);
// All BigPipe placeholders are processed, so update this response's
// attachments.
unset($this->attachments['big_pipe_placeholders']);
unset($this->attachments['big_pipe_nojs_placeholders']);
return $this;
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace Drupal\big_pipe\Render;
use Drupal\Core\Asset\AssetCollectionRendererInterface;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\EnforcedResponseException;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\AttachmentsInterface;
use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Processes attachments of HTML responses with BigPipe enabled.
*
* @see \Drupal\Core\Render\HtmlResponseAttachmentsProcessor
* @see \Drupal\big_pipe\Render\BigPipe
*/
class BigPipeResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {
/**
* The HTML response attachments processor service.
*
* @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
*/
protected $htmlResponseAttachmentsProcessor;
/**
* Constructs a BigPipeResponseAttachmentsProcessor object.
*
* @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
* The HTML response attachments processor service.
* @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
* An asset resolver.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A config factory for retrieving required config objects.
* @param \Drupal\Core\Asset\AssetCollectionRendererInterface $css_collection_renderer
* The CSS asset collection renderer.
* @param \Drupal\Core\Asset\AssetCollectionRendererInterface $js_collection_renderer
* The JS asset collection renderer.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(AttachmentsResponseProcessorInterface $html_response_attachments_processor, AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
$this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
parent::__construct($asset_resolver, $config_factory, $css_collection_renderer, $js_collection_renderer, $request_stack, $renderer, $module_handler, $language_manager);
}
/**
* {@inheritdoc}
*/
public function processAttachments(AttachmentsInterface $response) {
assert($response instanceof HtmlResponse);
// First, render the actual placeholders; this will cause the BigPipe
// placeholder strategy to generate BigPipe placeholders. We need those to
// exist already so that we can extract BigPipe placeholders. This is hence
// a bit of unfortunate but necessary duplication.
// @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
// (Note this is copied verbatim from
// \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments)
try {
$response = $this->renderPlaceholders($response);
}
catch (EnforcedResponseException $e) {
return $e->getResponse();
}
// Extract BigPipe placeholders; HtmlResponseAttachmentsProcessor does not
// know (nor need to know) how to process those.
$attachments = $response->getAttachments();
$big_pipe_placeholders = [];
$big_pipe_nojs_placeholders = [];
if (isset($attachments['big_pipe_placeholders'])) {
$big_pipe_placeholders = $attachments['big_pipe_placeholders'];
unset($attachments['big_pipe_placeholders']);
}
if (isset($attachments['big_pipe_nojs_placeholders'])) {
$big_pipe_nojs_placeholders = $attachments['big_pipe_nojs_placeholders'];
unset($attachments['big_pipe_nojs_placeholders']);
}
$html_response = clone $response;
$html_response->setAttachments($attachments);
// Call HtmlResponseAttachmentsProcessor to process all other attachments.
$processed_html_response = $this->htmlResponseAttachmentsProcessor->processAttachments($html_response);
// Restore BigPipe placeholders.
$attachments = $processed_html_response->getAttachments();
$big_pipe_response = clone $processed_html_response;
if (count($big_pipe_placeholders)) {
$attachments['big_pipe_placeholders'] = $big_pipe_placeholders;
}
if (count($big_pipe_nojs_placeholders)) {
$attachments['big_pipe_nojs_placeholders'] = $big_pipe_nojs_placeholders;
}
$big_pipe_response->setAttachments($attachments);
return $big_pipe_response;
}
}

View File

@@ -0,0 +1,320 @@
<?php
namespace Drupal\big_pipe\Render\Placeholder;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\SessionConfigurationInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Defines the BigPipe placeholder strategy, to send HTML in chunks.
*
* First: the BigPipe placeholder strategy only activates if the current request
* is associated with a session. Without a session, it is assumed this response
* is not actually dynamic: if none of the placeholders show session-dependent
* information, then none of the placeholders are uncacheable or poorly
* cacheable, which means the Page Cache (for anonymous users) can deal with it.
* In other words: BigPipe works for all authenticated users and for anonymous
* users that have a session (typical example: a shopping cart).
*
* (This is the default, and other modules can subclass this placeholder
* strategy to have different rules for enabling BigPipe.)
*
* The BigPipe placeholder strategy actually consists of two substrategies,
* depending on whether the current session is in a browser with JavaScript
* enabled or not:
* 1. with JavaScript enabled: #attached[big_pipe_js_placeholders]. Their
* replacements are streamed at the end of the page: chunk 1 is the entire
* page until the closing </body> tag, chunks 2 to (N-1) are replacement
* values for the placeholders, chunk N is </body> and everything after it.
* 2. with JavaScript disabled: #attached[big_pipe_nojs_placeholders]. Their
* replacements are streamed in situ: chunk 1 is the entire page until the
* first no-JS BigPipe placeholder, chunk 2 is the replacement for that
* placeholder, chunk 3 is the chunk from after that placeholder until the
* next no-JS BigPipe placeholder, et cetera.
*
* JS BigPipe placeholders are preferred because they result in better perceived
* performance: the entire page can be sent, minus the placeholders. But it
* requires JavaScript.
*
* No-JS BigPipe placeholders result in more visible blocking: only the part of
* the page can be sent until the first placeholder, after it is rendered until
* the second, et cetera. (In essence: multiple flushes.)
*
* Finally, both of those substrategies can also be combined: some placeholders
* live in places that cannot be efficiently replaced by JavaScript, for example
* CSRF tokens in URLs. Using no-JS BigPipe placeholders in those cases allows
* the first part of the page (until the first no-JS BigPipe placeholder) to be
* sent sooner than when they would be replaced using SingleFlushStrategy, which
* would prevent anything from being sent until all those non-HTML placeholders
* would have been replaced.
*
* See \Drupal\big_pipe\Render\BigPipe for detailed documentation on how those
* different placeholders are actually replaced.
*
* @see \Drupal\big_pipe\Render\BigPipe
*/
class BigPipeStrategy implements PlaceholderStrategyInterface {
/**
* BigPipe no-JS cookie name.
*/
const NOJS_COOKIE = 'big_pipe_nojs';
/**
* The session configuration.
*
* @var \Drupal\Core\Session\SessionConfigurationInterface
*/
protected $sessionConfiguration;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a new BigPipeStrategy class.
*
* @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
* The session configuration.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(SessionConfigurationInterface $session_configuration, RequestStack $request_stack, RouteMatchInterface $route_match) {
$this->sessionConfiguration = $session_configuration;
$this->requestStack = $request_stack;
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public function processPlaceholders(array $placeholders) {
$request = $this->requestStack->getCurrentRequest();
// Prevent placeholders from being processed by BigPipe on uncacheable
// request methods. For example, a form rendered inside a placeholder will
// be rendered as soon as possible before any headers are sent, so that it
// can be detected, submitted, and redirected immediately.
// @todo https://www.drupal.org/node/2367555
if (!$request->isMethodCacheable()) {
return [];
}
// Routes can opt out from using the BigPipe HTML delivery technique.
if ($this->routeMatch->getRouteObject()->getOption('_no_big_pipe')) {
return [];
}
if (!$this->sessionConfiguration->hasSession($request)) {
return [];
}
return $this->doProcessPlaceholders($placeholders);
}
/**
* Transforms placeholders to BigPipe placeholders, either no-JS or JS.
*
* @param array $placeholders
* The placeholders to process.
*
* @return array
* The BigPipe placeholders.
*/
protected function doProcessPlaceholders(array $placeholders) {
$overridden_placeholders = [];
foreach ($placeholders as $placeholder => $placeholder_elements) {
// BigPipe uses JavaScript and the DOM to find the placeholder to replace.
// This means finding the placeholder to replace must be efficient. Most
// placeholders are HTML, which we can find efficiently thanks to the
// querySelector API. But some placeholders are HTML attribute values or
// parts thereof, and potentially even plain text in DOM text nodes. For
// BigPipe's JavaScript to find those placeholders, it would need to
// iterate over all DOM text nodes. This is highly inefficient. Therefore,
// the BigPipe placeholder strategy only converts HTML placeholders into
// BigPipe placeholders. The other placeholders need to be replaced on the
// server, not via BigPipe.
// @see \Drupal\Core\Access\RouteProcessorCsrf::renderPlaceholderCsrfToken()
// @see \Drupal\Core\Form\FormBuilder::renderFormTokenPlaceholder()
// @see \Drupal\Core\Form\FormBuilder::renderPlaceholderFormAction()
if (static::placeholderIsAttributeSafe($placeholder)) {
$overridden_placeholders[$placeholder] = static::createBigPipeNoJsPlaceholder($placeholder, $placeholder_elements, TRUE);
}
else {
// If the current request/session doesn't have JavaScript, fall back to
// no-JS BigPipe.
if ($this->requestStack->getCurrentRequest()->cookies->has(static::NOJS_COOKIE)) {
$overridden_placeholders[$placeholder] = static::createBigPipeNoJsPlaceholder($placeholder, $placeholder_elements, FALSE);
}
else {
$overridden_placeholders[$placeholder] = static::createBigPipeJsPlaceholder($placeholder, $placeholder_elements);
}
$overridden_placeholders[$placeholder]['#cache']['contexts'][] = 'cookies:' . static::NOJS_COOKIE;
}
}
return $overridden_placeholders;
}
/**
* Determines whether the given placeholder is attribute-safe or not.
*
* @param string $placeholder
* A placeholder.
*
* @return bool
* Whether the placeholder is safe for use in an HTML attribute (in case
* it's a placeholder for an HTML attribute value or a subset of it).
*/
protected static function placeholderIsAttributeSafe($placeholder) {
assert(is_string($placeholder));
return $placeholder[0] !== '<' || $placeholder !== Html::normalize($placeholder);
}
/**
* Creates a BigPipe JS placeholder.
*
* @param string $original_placeholder
* The original placeholder.
* @param array $placeholder_render_array
* The render array for a placeholder.
*
* @return array
* The resulting BigPipe JS placeholder render array.
*/
protected static function createBigPipeJsPlaceholder($original_placeholder, array $placeholder_render_array) {
$big_pipe_placeholder_id = static::generateBigPipePlaceholderId($original_placeholder, $placeholder_render_array);
$interface_preview = [];
if (isset($placeholder_render_array['#lazy_builder'])) {
$interface_preview = [
'#theme' => 'big_pipe_interface_preview',
'#callback' => $placeholder_render_array['#lazy_builder'][0],
'#arguments' => $placeholder_render_array['#lazy_builder'][1],
];
if (isset($placeholder_render_array['#preview'])) {
$interface_preview['#preview'] = $placeholder_render_array['#preview'];
unset($placeholder_render_array['#preview']);
}
}
return [
'#prefix' => '<span data-big-pipe-placeholder-id="' . Html::escape($big_pipe_placeholder_id) . '">',
'interface_preview' => $interface_preview,
'#suffix' => '</span>',
'#cache' => [
'max-age' => 0,
'contexts' => [
'session.exists',
],
],
'#attached' => [
'library' => [
'big_pipe/big_pipe',
],
// Inform BigPipe' JavaScript known BigPipe placeholder IDs.
'drupalSettings' => [
'bigPipePlaceholderIds' => [$big_pipe_placeholder_id => TRUE],
],
'big_pipe_placeholders' => [
Html::escape($big_pipe_placeholder_id) => $placeholder_render_array,
],
],
];
}
/**
* Creates a BigPipe no-JS placeholder.
*
* @param string $original_placeholder
* The original placeholder.
* @param array $placeholder_render_array
* The render array for a placeholder.
* @param bool $placeholder_must_be_attribute_safe
* Whether the placeholder must be safe for use in an HTML attribute (in
* case it's a placeholder for an HTML attribute value or a subset of it).
*
* @return array
* The resulting BigPipe no-JS placeholder render array.
*/
protected static function createBigPipeNoJsPlaceholder($original_placeholder, array $placeholder_render_array, $placeholder_must_be_attribute_safe = FALSE) {
if (!$placeholder_must_be_attribute_safe) {
$big_pipe_placeholder = '<span data-big-pipe-nojs-placeholder-id="' . Html::escape(static::generateBigPipePlaceholderId($original_placeholder, $placeholder_render_array)) . '"></span>';
}
else {
$big_pipe_placeholder = 'big_pipe_nojs_placeholder_attribute_safe:' . Html::escape($original_placeholder);
}
return [
'#markup' => $big_pipe_placeholder,
'#cache' => [
'max-age' => 0,
'contexts' => [
'session.exists',
],
],
'#attached' => [
'big_pipe_nojs_placeholders' => [
$big_pipe_placeholder => $placeholder_render_array,
],
],
];
}
/**
* Generates a BigPipe placeholder ID.
*
* @param string $original_placeholder
* The original placeholder.
* @param array $placeholder_render_array
* The render array for a placeholder.
*
* @return string
* The generated BigPipe placeholder ID.
*/
protected static function generateBigPipePlaceholderId($original_placeholder, array $placeholder_render_array) {
// Generate a BigPipe placeholder ID (to be used by BigPipe's JavaScript).
// @see \Drupal\Core\Render\PlaceholderGenerator::createPlaceholder()
if (isset($placeholder_render_array['#lazy_builder'])) {
// Be sure cache contexts and tags are sorted before serializing them and
// making hash. Issue #3225328 removes sort from contexts and tags arrays
// for performances reasons.
if (isset($placeholder_render_array['#cache']['contexts'])) {
sort($placeholder_render_array['#cache']['contexts']);
}
if (isset($placeholder_render_array['#cache']['tags'])) {
sort($placeholder_render_array['#cache']['tags']);
}
$callback = $placeholder_render_array['#lazy_builder'][0];
$arguments = $placeholder_render_array['#lazy_builder'][1];
$token = Crypt::hashBase64(serialize($placeholder_render_array));
return UrlHelper::buildQuery(['callback' => $callback, 'args' => $arguments, 'token' => $token]);
}
// When the placeholder's render array is not using a #lazy_builder,
// anything could be in there: only #lazy_builder has a strict contract that
// allows us to create a more sane selector. Therefore, simply the original
// placeholder into a usable placeholder ID, at the cost of it being obtuse.
else {
return Html::getId($original_placeholder);
}
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Drupal\big_pipe\StackMiddleware;
use Drupal\big_pipe\Render\BigPipeResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Defines a big pipe middleware that removes Content-Length headers.
*/
final class ContentLength implements HttpKernelInterface {
/**
* Constructs a new ContentLength instance.
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
* The wrapped HTTP kernel.
*/
public function __construct(
protected readonly HttpKernelInterface $httpKernel,
) {
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
$response = $this->httpKernel->handle($request, $type, $catch);
if (!$response instanceof BigPipeResponse) {
return $response;
}
$response->headers->remove('Content-Length');
return $response;
}
}