Skip to content

Commit

Permalink
Defer all block view scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jul 20, 2023
1 parent 26c06ab commit 86b8e36
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
34 changes: 34 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,40 @@ function gutenberg_reregister_core_block_types() {

add_action( 'init', 'gutenberg_reregister_core_block_types' );

/**
* Adds the defer loading strategy to all registered blocks.
*
* This function would not be part of core merge. Instead, the register_block_script_handle() function would be patched
* as follows.
*
* ```
* --- a/wp-includes/blocks.php
* +++ b/wp-includes/blocks.php
* @ @ -153,7 +153,8 @ @ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
* $script_handle,
* $script_uri,
* $script_dependencies,
* - isset( $script_asset['version'] ) ? $script_asset['version'] : false
* + isset( $script_asset['version'] ) ? $script_asset['version'] : false,
* + array( 'strategy' => 'defer' )
* );
* if ( ! $result ) {
* return false;
* ```
*
* @see register_block_script_handle()
*/
function gutenberg_defer_block_view_scripts() {
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
foreach ( $block_types as $block_type ) {
foreach ( $block_type->view_script_handles as $view_script_handle ) {
wp_script_add_data( $view_script_handle, 'strategy', 'defer' );
}
}
}

add_action( 'init', 'gutenberg_defer_block_view_scripts', 100 );

/**
* Deregisters the existing core block type and its assets.
*
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/file/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ function gutenberg_block_core_file_update_interactive_view_script( $metadata ) {
function render_block_core_file( $attributes, $content, $block ) {
$should_load_view_script = ! empty( $attributes['displayPreview'] );
$view_js_file = 'wp-block-file-view';
wp_script_add_data( $view_js_file, 'strategy', 'defer' ); // TODO: This should be able to be specified in block.json. See Core-54018.
// If the script already exists, there is no point in removing it from viewScript.
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,6 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$should_load_view_script = count( array_filter( $needed_script_map ) ) > 0;
} else {
foreach ( $needed_script_map as $view_script_handle => $is_view_script_needed ) {
wp_script_add_data( $view_script_handle, 'strategy', 'defer' ); // TODO: This should be able to be specified in block.json. See Core-54018.

// If the script already exists, there is no point in removing it from viewScript.
if ( wp_script_is( $view_script_handle ) ) {
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function render_block_core_search( $attributes, $content, $block ) {

// If the script already exists, there is no point in removing it from viewScript.
$view_js_file = 'wp-block-search-view';
wp_script_add_data( $view_js_file, 'strategy', 'defer' ); // TODO: This should be able to be specified in block.json. See Core-54018.
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;

Expand Down

0 comments on commit 86b8e36

Please sign in to comment.