Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enqueue assets for rendered blocks only #22754

Merged
merged 2 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ public function render( $options = array() ) {
$post = $global_post;
}

if ( ! empty( $this->block_type->script ) ) {
wp_enqueue_script( $this->block_type->script );
}

if ( ! empty( $this->block_type->style ) ) {
wp_enqueue_style( $this->block_type->style );
}

/** This filter is documented in src/wp-includes/blocks.php */
return apply_filters( 'render_block', $block_content, $this->parsed_block );
}
Expand Down
14 changes: 14 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,17 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse
return $block->render();
}
add_filter( 'pre_render_block', 'gutenberg_render_block_with_assigned_block_context', 9, 2 );

/**
* Avoid enqueueing block assets of all registered blocks for all posts, instead
* deferring to block render mechanics to enqueue scripts, thereby ensuring only
* blocks of the content have their assets enqueued.
*
* This can be removed once minimum support for the plugin is outside the range
* of the version associated with closure of the following ticket.
*
* @see https://core.trac.wordpress.org/ticket/50328
*
* @see WP_Block::render
*/
remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
Copy link
Member

@gziolo gziolo Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a deprecation hint in the plugin for the line that removes wp_enqueue_registered_block_scripts_and_styles action?
I'm not entirely sure what you mean by this.

Should we remove it after the logic is backported into WP Core? At the time of or after WP 5.5?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. Most everything in this file should be associated with a ticket for removal (per initial file comment). Based on my previous comment, this is yet to be created, but I can put a "TBD" for now.