diff --git a/lib/class-wp-block.php b/lib/class-wp-block.php index 6bd82378e39ac..ccc3f5f942307 100644 --- a/lib/class-wp-block.php +++ b/lib/class-wp-block.php @@ -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 ); } diff --git a/lib/compat.php b/lib/compat.php index 9dd224adb2f19..dd44b26ebf993 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -291,3 +291,12 @@ 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. + * + * @see WP_Block::render + */ +remove_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );