3 * Renders BigPipe placeholders using Drupal's Ajax system.
6 (function($, Drupal, drupalSettings) {
8 * Maps textContent of <script type="application/vnd.drupal-ajax"> to an AJAX response.
10 * @param {string} content
11 * The text content of a <script type="application/vnd.drupal-ajax"> DOM node.
12 * @return {Array|boolean}
13 * The parsed Ajax response containing an array of Ajax commands, or false in
14 * case the DOM node hasn't fully arrived yet.
16 function mapTextContentToAjaxResponse(content) {
22 return JSON.parse(content);
29 * Executes Ajax commands in <script type="application/vnd.drupal-ajax"> tag.
31 * These Ajax commands replace placeholders with HTML and load missing CSS/JS.
33 * @param {number} index
35 * @param {HTMLScriptElement} placeholderReplacement
36 * Script tag created by BigPipe.
38 function bigPipeProcessPlaceholderReplacement(index, placeholderReplacement) {
39 const placeholderId = placeholderReplacement.getAttribute(
40 'data-big-pipe-replacement-for-placeholder-with-id',
42 const content = this.textContent.trim();
43 // Ignore any placeholders that are not in the known placeholder list. Used
44 // to avoid someone trying to XSS the site via the placeholdering mechanism.
46 typeof drupalSettings.bigPipePlaceholderIds[placeholderId] !== 'undefined'
48 const response = mapTextContentToAjaxResponse(content);
49 // If we try to parse the content too early (when the JSON containing Ajax
50 // commands is still arriving), textContent will be empty or incomplete.
51 if (response === false) {
53 * Mark as unprocessed so this will be retried later.
54 * @see bigPipeProcessDocument()
56 $(this).removeOnce('big-pipe');
58 // Create a Drupal.Ajax object without associating an element, a
59 // progress indicator or a URL.
60 const ajaxObject = Drupal.ajax({
66 // Then, simulate an AJAX response having arrived, and let the Ajax
68 ajaxObject.success(response, 'success');
73 // The frequency with which to check for newly arrived BigPipe placeholders.
74 // Hence 50 ms means we check 20 times per second. Setting this to 100 ms or
75 // more would cause the user to see content appear noticeably slower.
76 const interval = drupalSettings.bigPipeInterval || 50;
78 // The internal ID to contain the watcher service.
82 * Processes a streamed HTML document receiving placeholder replacements.
84 * @param {HTMLDocument} context
85 * The HTML document containing <script type="application/vnd.drupal-ajax">
86 * tags generated by BigPipe.
89 * Returns true when processing has been finished and a stop signal has been
92 function bigPipeProcessDocument(context) {
93 // Make sure we have BigPipe-related scripts before processing further.
94 if (!context.querySelector('script[data-big-pipe-event="start"]')) {
99 .find('script[data-big-pipe-replacement-for-placeholder-with-id]')
101 .each(bigPipeProcessPlaceholderReplacement);
103 // If we see the stop signal, clear the timeout: all placeholder
104 // replacements are guaranteed to be received and processed.
105 if (context.querySelector('script[data-big-pipe-event="stop"]')) {
107 clearTimeout(timeoutID);
115 function bigPipeProcess() {
116 timeoutID = setTimeout(() => {
117 if (!bigPipeProcessDocument(document)) {
125 // If something goes wrong, make sure everything is cleaned up and has had a
126 // chance to be processed with everything loaded.
127 $(window).on('load', () => {
129 clearTimeout(timeoutID);
131 bigPipeProcessDocument(document);
133 })(jQuery, Drupal, drupalSettings);