class BigPipeStrategy implements PlaceholderStrategyInterface (View source)

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 tag, chunks 2 to (N-1) are replacement values for the placeholders, chunk N is 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.

Constants

NOJS_COOKIE

BigPipe no-JS cookie name.

Properties

protected SessionConfigurationInterface $sessionConfiguration

The session configuration.

protected RequestStack $requestStack

The request stack.

protected RouteMatchInterface $routeMatch

The current route match.

Methods

__construct(SessionConfigurationInterface $session_configuration, RequestStack $request_stack, RouteMatchInterface $route_match)

Constructs a new BigPipeStrategy class.

array
processPlaceholders(array $placeholders)

Processes placeholders to render them with different strategies.

array
doProcessPlaceholders(array $placeholders)

Transforms placeholders to BigPipe placeholders, either no-JS or JS.

static bool
placeholderIsAttributeSafe(string $placeholder)

Determines whether the given placeholder is attribute-safe or not.

static array
createBigPipeJsPlaceholder(string $original_placeholder, array $placeholder_render_array)

Creates a BigPipe JS placeholder.

static array
createBigPipeNoJsPlaceholder(string $original_placeholder, array $placeholder_render_array, bool $placeholder_must_be_attribute_safe = FALSE)

Creates a BigPipe no-JS placeholder.

static string
generateBigPipePlaceholderId(string $original_placeholder, array $placeholder_render_array)

Generates a BigPipe placeholder ID.

Details

__construct(SessionConfigurationInterface $session_configuration, RequestStack $request_stack, RouteMatchInterface $route_match)

Constructs a new BigPipeStrategy class.

Parameters

SessionConfigurationInterface $session_configuration

The session configuration.

RequestStack $request_stack

The request stack.

RouteMatchInterface $route_match

The current route match.

array processPlaceholders(array $placeholders)

Processes placeholders to render them with different strategies.

Parameters

array $placeholders

The placeholders to process, with the keys being the markup for the placeholders and the values the corresponding render array describing the data to be rendered.

Return Value

array

The resulting placeholders, with a subset of the keys of $placeholders (and those being the markup for the placeholders) but with the corresponding render array being potentially modified to render e.g. an ESI or BigPipe placeholder.

protected array doProcessPlaceholders(array $placeholders)

Transforms placeholders to BigPipe placeholders, either no-JS or JS.

Parameters

array $placeholders

The placeholders to process.

Return Value

array

The BigPipe placeholders.

static protected bool placeholderIsAttributeSafe(string $placeholder)

Determines whether the given placeholder is attribute-safe or not.

Parameters

string $placeholder

A placeholder.

Return Value

bool

Whether the placeholder is safe for use in a HTML attribute (in case it's a placeholder for a HTML attribute value or a subset of it).

static protected array createBigPipeJsPlaceholder(string $original_placeholder, array $placeholder_render_array)

Creates a BigPipe JS placeholder.

Parameters

string $original_placeholder

The original placeholder.

array $placeholder_render_array

The render array for a placeholder.

Return Value

array

The resulting BigPipe JS placeholder render array.

static protected array createBigPipeNoJsPlaceholder(string $original_placeholder, array $placeholder_render_array, bool $placeholder_must_be_attribute_safe = FALSE)

Creates a BigPipe no-JS placeholder.

Parameters

string $original_placeholder

The original placeholder.

array $placeholder_render_array

The render array for a placeholder.

bool $placeholder_must_be_attribute_safe

Whether the placeholder must be safe for use in a HTML attribute (in case it's a placeholder for a HTML attribute value or a subset of it).

Return Value

array

The resulting BigPipe no-JS placeholder render array.

static protected string generateBigPipePlaceholderId(string $original_placeholder, array $placeholder_render_array)

Generates a BigPipe placeholder ID.

Parameters

string $original_placeholder

The original placeholder.

array $placeholder_render_array

The render array for a placeholder.

Return Value

string

The generated BigPipe placeholder ID.