core/modules/big_pipe/src/Render/BigPipe.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/modules/big_pipe/src/Render/BigPipe.php b/core/modules/big_pipe/src/Render/BigPipe.php index e7f5377..5e3cc12 100644 --- a/core/modules/big_pipe/src/Render/BigPipe.php +++ b/core/modules/big_pipe/src/Render/BigPipe.php @@ -226,6 +226,13 @@ protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAsse $preg_placeholder_strings = array_map($prepare_for_preg_split, array_keys($no_js_placeholders)); $fragments = preg_split('/' . implode('|', $preg_placeholder_strings) . '/', $html, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); + // Determine how many occurrences there are of each no-JS placeholder. + $placeholder_occurrences = array_count_values(array_intersect($fragments, array_keys($no_js_placeholders))); + + // 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 @@ -236,6 +243,15 @@ protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAsse 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])) { + print $multi_occurrence_placeholders_content[$fragment]; + flush(); + continue; + } + $placeholder = $fragment; assert('isset($no_js_placeholders[$placeholder])'); $token = Crypt::randomBytesBase64(55); @@ -310,6 +326,13 @@ protected function sendNoJsPlaceholders($html, $no_js_placeholders, AttachedAsse // 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(); + } } }