WordPress.org

Making WordPress.org

Changeset 7593


Ignore:
Timestamp:
08/04/2018 05:54:11 AM (3 years ago)
Author:
dd32
Message:

Gutenberg: Override wp.domReady with a version which doesn't contain a race-condition, this fixes the whitescreen on frontendberg on first load.

wp.domReady() is incorrectly regisering callbacks (such as the Gutenberg init function) to be fired on DOMContentLoaded AFTER that event has already fired, causing the callbacks to never run.
This replaces it with a version which does work, calling the callback immediately when document.readyState is at interactive (which is what fires DOMContentLoaded) or complete (when styles/images/etc are loaded, which the Gutenberg wp.domReady does).

See #3703, #3735.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/gutenberg/functions.php

    r7592 r7593  
    158158            '_wpLoadGutenbergEditor.then( function() { wp.blocks.unregisterBlockType( "core/shortcode" ); } );'
    159159        );
     160
     161        // Fix domReady for frontendberg
     162        wp_add_inline_script(
     163            'wp-dom-ready',
     164            'wp.domReady = function( callback ) {
     165                // The included domReady fails to call callbacks which are registered while readyState is "interactive".
     166                // DOMContentLoaded fires at the start of interactive, but domReady only calls registered methods at "complete".
     167                // This caused Frontendberg to white screen as the Gutenberg initialize call happens in that timespan when SessionStorage is empty
     168                if (
     169                    document.readyState === "complete" || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
     170                    document.readyState === "interactive" // DOMContentLoaded fires at this point, so we call directly.
     171                ) {
     172                    return callback();
     173                }
     174
     175                // DOMContentLoaded has not fired yet, delay callback until then.
     176                document.addEventListener("DOMContentLoaded", callback);
     177            };'
     178        );
     179
    160180    }, 11 );
    161181    add_action( 'wp_enqueue_scripts', 'gutenberg_editor_scripts_and_styles' );
Note: See TracChangeset for help on using the changeset viewer.